Advertisement
kaenan

C template file (useful stuff)

Jan 15th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.69 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #include <limits.h>
  6.  
  7.  
  8. /* Utitlites. TO BE IGNORED. */
  9. /* ======================================================================== */
  10. #define NEWLINE puts("");
  11. #define dd printf("HERE %d\n", __LINE__); // Used for debugging.
  12.  
  13. #define NELEMS(x) (sizeof(x)/sizeof(x[0]))
  14. #define max(x,y) (((x) > (y)) ? (x) : (y))
  15. #define min(x,y) (((x) > (y)) ? (y) : (x))
  16.  
  17. /* For itr = [start, end]. */
  18. #define forIn(itr, start, end) \
  19.     for (unsigned int itr = (start); (itr) < (end); ++(itr))
  20.  
  21. /* Assert expr. If not true print err_msg and exe err_expr. */
  22. #define assert(expr, err_msg, err_expr) \
  23.     if (!(expr)) {                      \
  24.         printf(err_msg);                \
  25.         err_expr;                       \
  26.     }
  27.  
  28. /* Array concatenation. */
  29. #define arrcat(t, t_nelems, s, s_nelems) {      \
  30.     forIn(s_index, 0, s_nelems) {               \
  31.             t[t_nelems + s_index] = s[s_index]; \
  32.     } }
  33.  
  34. /* Error codes. */
  35. enum errcode {
  36.     SUCCESS,
  37.     ERROR,
  38.     NULL_INPUT = -'I',
  39.     DOMAIN_ERR,  // Input does not belong to the domanin of the function.
  40.     NOT_FOUND = 404,
  41.     FOUND = -404,
  42.     NIL = '0',
  43.     INSUFFICENT_INPUT,
  44.     RESOURCE_ACQUISITION_FAILED = 7
  45. };
  46. typedef enum errcode errcode;
  47.  
  48. int errno_ = SUCCESS;
  49.  
  50. /* Error checked malloc, realloc and calloc. */
  51. void *ec_malloc(size_t);
  52. void *ec_realloc(void *, size_t);
  53. void *ec_calloc(size_t, size_t);
  54.  
  55. /* Allocate memory for an array of =type=. */
  56. #define mem_arr(type, ptr, size) \
  57.     (ptr) = (type *) ec_malloc((size) * sizeof(type))
  58. #define mem_arr_calloc(type, ptr, size) \
  59.     (ptr) = (type *) ec_calloc((size), sizeof(type));
  60.  
  61. /* Contigous matrix allocation. */
  62. #define mem_mat(type, matrix, rows, cols)           \
  63.     mem_arr(type*, matrix, rows * cols + rows);     \
  64.     type *data_index = matrix + rows;               \
  65.     forIn(row, 0, rows) {                           \
  66.         matrix[row] = data_index + (row * cols);    \
  67.     }
  68.  
  69. /* Allocate matrix. */
  70. #define mem_matrix(type, matrix, rows, cols)    \
  71.     mem_arr(type*, matrix, rows);               \
  72.     forIn (i, 0, rows) {                        \
  73.         mem_arr(type, matrix[i], cols);         \
  74.     }
  75. /* Deallocate matrix. */
  76. #define free_matrix(matrix, rows)   \
  77.     forIn (i, 0, rows) {            \
  78.         free(matrix[i]);            \
  79.     }                               \
  80.     free(matrix);
  81.  
  82. /* Variable length TAB. */
  83. #define TAB(length) printf("%" #length "c", 0)
  84.  
  85. /* Strtol wrapper. */
  86. #define strtol_(str, identifier, calling_fn, err_expr)                      \
  87.     identifier = strtol(str, &err, 10);                                     \
  88.     if (*err) {                                                             \
  89.         printf("[Error] " #calling_fn ": \"%s\" is not a number.\n", str);  \
  90.         err_expr;                                                           \
  91.     }
  92.  
  93. typedef struct {
  94.     char *s;
  95.     unsigned int len;
  96. } String;
  97.  
  98. /* Return a token from a file descriptor. */
  99. String ftok(FILE *fd, const char *del);
  100.  
  101. /* Get a token from a file descriptor and assert it's not null. */
  102. #define assert_ftok(fd, token, del, err_expr) \
  103.         assert((token = ftok(fd, del)).len, "", err_expr);
  104.  
  105. /* Erorr checked fopen(). */
  106. FILE *ec_fopen(const char *file_path, const char *mode);
  107.  
  108. /* Assert file at =file_path= is not empty. */
  109. #define assert_not_empty(file_path, eof_expr) do {      \
  110.     FILE *fd_test = ec_fopen(file_path, "a+");          \
  111.     if(fgetc(fd_test) == EOF || feof(fd_test))  {       \
  112.         printf("File %s is empty.\n", file_path);       \
  113.         fclose(fd_test);                                \
  114.         eof_expr; break;                                \
  115.     }                                                   \
  116.     fclose(fd_test);                                    \
  117.     } while(0)
  118.  
  119. /* Flexible read of an array. */
  120. #define freadarr_va(fd, format_specifier, arr, nelems) do { \
  121.     forIn (i, 0, nelems) {                                  \
  122.         if (EOF == fscanf(fd, format_specifier, arr + i)) { \
  123.             nelems = i; break;                              \
  124.         }                                                   \
  125.     }                                                       \
  126.     } while(0)
  127. /* Error checked read of an array. */
  128. #define freadarr(fd, format_specifier, arr, nelems)         \
  129.     forIn (i, 0, nelems) {                                  \
  130.         if (EOF == fscanf(fd, format_specifier, arr + i)) { \
  131.             puts("[Error] Not enough input.\n");            \
  132.             exit(INSUFFICENT_INPUT);                        \
  133.         }                                                   \
  134.     }
  135. /* Print array. */
  136. #define fprintarr(fd, format_specifier, arr, nelems)    \
  137.     forIn (i, 0, nelems) {                              \
  138.         fprintf(fd, format_specifier, *(arr + i));      \
  139.     }                                                   \
  140.     NEWLINE;
  141.  
  142. /* Error checked read of a matrix. */
  143. #define freadmat(fd, format_specifier, matrix, rows,  cols)                             \
  144.     forIn (ln_index, 0, rows) {                                                         \
  145.         forIn (col_index, 0, cols) {                                                    \
  146.             if ( EOF == fscanf(fd, format_specifier,matrix[ln_index] + col_index)) {    \
  147.                 puts("[Error] Not enough input.\n");                                    \
  148.                 exit(INSUFFICENT_INPUT);                                                \
  149.             }                                                                           \
  150.         }                                                                               \
  151.     }
  152. /* Print matrix. */   /* NEWLINE is printed to stdout only. */
  153. #define fprintmat(fd, format_specifier, matrix, rows,  cols)                \
  154.     NEWLINE;                                                                \
  155.     forIn (ln_index, 0, rows) {                                             \
  156.         forIn (col_index, 0, cols) {                                        \
  157.             fprintf(fd, format_specifier " ",matrix[ln_index][col_index]);  \
  158.         }                                                                   \
  159.         NEWLINE;                                                            \
  160.     }                                                                       \
  161.     NEWLINE;
  162. /* ======================================================================== */
  163.  
  164.  
  165. /* BEGIN */
  166. #define INIT(input_file) do {                           \
  167.         assert_not_empty(input_file, exit(NULL_INPUT)); \
  168.         FILE *input = ec_fopen(input_file, "r");        \
  169.                                                         \
  170.                                                         \
  171.                                                         \
  172.         fclose(input);                                  \
  173.     } while(0)
  174.  
  175. #define INPUT_FILE "" // <- FILL
  176.  
  177. int main(int argc, char **argv)
  178. {
  179.     INIT(INPUT_FILE);
  180.  
  181.     return 0;
  182. }
  183. /* END */
  184.  
  185.  
  186.  
  187. /* Utilites. TO BE IGNORED. */
  188. /* ======================================================================== */
  189. /* Return a token from a given file descriptor. */
  190. String ftok(FILE *fd, const char *del)
  191. {
  192.     String token;
  193.     unsigned int size = 30, s_index = 0;
  194.     char c;
  195.  
  196.     assert(del, "[Error] Empty del string.\n", return ((String){ NULL, 0 }));
  197.  
  198.     mem_arr(char, token.s, size);
  199.  
  200.     /* Ignore initial delimiters. */
  201.     while ((c = fgetc(fd)) != EOF && strchr(del, c)) { ; }
  202.  
  203.     if (c == EOF) {
  204.         free(token.s);
  205.         return (String) { NULL, 0 };
  206.     }
  207.  
  208.     do {
  209.         if (s_index >= size) {
  210.             token.s = (char *)ec_realloc(token.s, size += 30);
  211.         }
  212.         *(token.s + s_index) = c;
  213.         ++s_index;
  214.     } while ((c = fgetc(fd)) != EOF && !strchr(del, c));
  215.  
  216.     token.s = (char *)ec_realloc(token.s, s_index + 1); // "Tight fitting" string.
  217.     token.s[s_index] = 0; // End string.
  218.     token.len = s_index;
  219.  
  220.     return token;
  221. }
  222.  
  223. /* I/O handling. */
  224. FILE *ec_fopen(const char *file_path, const char *mode)
  225. {
  226.     FILE *fd;
  227.  
  228.     assert(file_path, "[Error] Empty file_path string.\n", exit(NULL_INPUT));
  229.     assert(mode, "[Error] Empty mode string.\n", exit(NULL_INPUT));
  230.  
  231.     fd = fopen(file_path, mode);
  232.  
  233.     if (NULL == fd) {
  234.         puts("[Error] fopen() returned NULL.");
  235.         exit(RESOURCE_ACQUISITION_FAILED);
  236.     }
  237.  
  238.     return fd;
  239. }
  240.  
  241. /* Memory management. */
  242. #define assert_alloc(allocator_name)                            \
  243.     assert((new_ptr != NULL),                                   \
  244.            "[Error] " #allocator_name "() returned NULL.\n",    \
  245.            exit(RESOURCE_ACQUISITION_FAILED));
  246. void *ec_calloc(size_t nitems, size_t size)
  247. {
  248.     void *new_ptr;
  249.  
  250.     new_ptr = calloc(nitems, size);
  251.     assert_alloc(calloc);
  252.  
  253.     return new_ptr;
  254. }
  255. void *ec_realloc(void *ptr, size_t size)
  256. {
  257.     void *new_ptr;
  258.  
  259.     new_ptr = realloc(ptr, size);
  260.     assert_alloc(realloc);
  261.  
  262.     return new_ptr;
  263. }
  264. void *ec_malloc(size_t size)
  265. {
  266.     return ec_realloc(NULL, size);
  267. }
  268. /* ======================================================================== */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement