io_utils.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #ifndef BASICC_IO_UTILS_IO_UTILS_H_
  2. #define BASICC_IO_UTILS_IO_UTILS_H_
  3. #include <stdio.h>
  4. #include <limits.h>
  5. #include <errno.h>
  6. #include <string.h>
  7. void PrintBinary(unsigned int value);
  8. //#define PRINT_METADATA
  9. #ifdef PRINT_METADATA
  10. # define PRINTLNF(format, ...) printf("("__FILE__":%d) %s: "format"\n", __LINE__, __FUNCTION__ , ##__VA_ARGS__)
  11. #else
  12. # define PRINTLNF(format, ...) printf(format"\n", ##__VA_ARGS__)
  13. #endif
  14. #define PRINT_CHAR(char_value) PRINTLNF(#char_value": %c", char_value)
  15. #define PRINT_WCHAR(char_value) PRINTLNF(#char_value": %lc", char_value)
  16. #define PRINT_INT(int_value) PRINTLNF(#int_value": %d", int_value)
  17. #define PRINT_LONG(long_value) PRINTLNF(#long_value": %ld", long_value)
  18. #define PRINT_LLONG(long_value) PRINTLNF(#long_value": %lld", long_value)
  19. #define PRINT_BINARY(int_value) PrintBinary((unsigned int) int_value);
  20. #define PRINT_HEX(int_value) PRINTLNF(#int_value": %#x", int_value)
  21. #define PRINT_BOOL(bool_value) PRINTLNF(#bool_value": %s", bool_value ? "true" : "false")
  22. #define PRINT_DOUBLE(double_value) PRINTLNF(#double_value": %g", double_value)
  23. #define PRINT_STRING(string_value) PRINTLNF(#string_value": %s", string_value)
  24. #define PRINT_ARRAY(format, array, length) \
  25. { int array_index; \
  26. for (array_index = 0; array_index < length; ++array_index) { \
  27. printf(format, array[array_index]); \
  28. };\
  29. printf("\n"); }
  30. #define PRINT_INT_ARRAY_LN(array, length) \
  31. { int i; \
  32. for (i = 0; i < length; ++i) { \
  33. PRINTLNF(#array"[%d]: %d", i, array[i]); \
  34. }}
  35. #define PRINT_INT_ARRAY(array, length) PRINT_ARRAY("%d, ", array, length)
  36. #define PRINT_CHAR_ARRAY(array, length) PRINT_ARRAY("%c, ", array, length)
  37. #define PRINT_DOUBLE_ARRAY(array, length) PRINT_ARRAY("%g, ", array, length)
  38. #define PRINT_IF_ERROR(format, ...) \
  39. if (errno != 0) { \
  40. fprintf(stderr, format, ##__VA_ARGS__); \
  41. fprintf(stderr, ": %s\n", strerror(errno)); \
  42. }
  43. #endif //BASICC_IO_UTILS_IO_UTILS_H_