cmd_print.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. *******************************************************************************
  3. \file cmd_print.c
  4. \brief Command-line interface to Bee2: print to terminal
  5. \project bee2/cmd
  6. \created 2023.06.06
  7. \version 2023.06.06
  8. \copyright The Bee2 authors
  9. \license Licensed under the Apache License, Version 2.0 (see LICENSE.txt).
  10. *******************************************************************************
  11. */
  12. #include "../cmd.h"
  13. #include <bee2/core/err.h>
  14. #include <bee2/core/hex.h>
  15. #include <bee2/core/mem.h>
  16. #include <bee2/core/tm.h>
  17. #include <bee2/core/util.h>
  18. #include <stdio.h>
  19. /*
  20. *******************************************************************************
  21. Память
  22. *******************************************************************************
  23. */
  24. err_t cmdPrintMem(const void* buf, size_t count)
  25. {
  26. char* hex;
  27. // pre
  28. ASSERT(memIsValid(buf, count));
  29. // выделить память
  30. hex = (char*)blobCreate(32);
  31. if (!hex)
  32. return ERR_OUTOFMEMORY;
  33. // печатать
  34. while (count > 14)
  35. {
  36. hexFrom(hex, buf, 14);
  37. printf("%s", hex);
  38. buf = (const octet*)buf + 14, count -= 14;
  39. }
  40. hexFrom(hex, buf, count);
  41. printf("%s", hex);
  42. // завершить
  43. blobClose(hex);
  44. return ERR_OK;
  45. }
  46. err_t cmdPrintMem2(const void* buf, size_t count)
  47. {
  48. char* hex;
  49. // pre
  50. ASSERT(memIsValid(buf, count));
  51. // выделить память
  52. hex = (char*)blobCreate(32);
  53. if (!hex)
  54. return ERR_OUTOFMEMORY;
  55. // печатать
  56. if (count > 14)
  57. {
  58. hexFrom(hex, buf, 12);
  59. hex[24] = hex[25] = hex[26] = '.';
  60. hexFrom(hex + 27, (const octet*)buf + count - 2, 2);
  61. printf("%s (%u)", hex, (unsigned)count);
  62. }
  63. else
  64. {
  65. hexFrom(hex, buf, count);
  66. printf("%s", hex);
  67. }
  68. // завершить
  69. blobClose(hex);
  70. return ERR_OK;
  71. }
  72. /*
  73. *******************************************************************************
  74. Дата
  75. *******************************************************************************
  76. */
  77. err_t cmdPrintDate(const octet date[6])
  78. {
  79. if (!tmDateIsValid2(date))
  80. return ERR_BAD_DATE;
  81. printf("%c%c%c%c%c%c",
  82. date[0] + '0', date[1] + '0', date[2] + '0',
  83. date[3] + '0', date[4] + '0', date[5] + '0');
  84. return ERR_OK;
  85. }