16 static inline char* gu_str_append(
char* str,
size_t* off,
17 const char* app,
size_t app_len)
20 assert(str == NULL || *(str + *off - 1) ==
'\0');
21 tmp = realloc(str, *off + app_len + 1);
24 memcpy(tmp + *off, app, app_len + 1);
33 static inline const char* gu_str_next(
const char* str)
35 return strchr(str,
'\0') + 1;
41 static inline const char* gu_str_advance(
const char* str,
size_t n)
43 const char* ptr = str;
46 ptr = gu_str_next(ptr);
66 static inline char* gu_str_table_set_name(
char* str,
size_t* off,
const char* name)
68 return gu_str_append(str, off, name, strlen(name));
71 static inline const char* gu_str_table_get_name(
const char* str)
76 static inline char* gu_str_table_append_size(
char* str,
size_t* off,
size_t n)
79 size_t len = snprintf(buf,
sizeof(buf),
"%zu", n);
80 return gu_str_append(str, off, buf, len);
83 static inline char* gu_str_table_set_n_cols(
char* str,
size_t* off,
size_t n)
85 return gu_str_table_append_size(str, off, n);
88 static inline size_t gu_str_table_get_n_cols(
const char* str)
90 str = gu_str_advance(str, 1);
91 return strtoul(str, NULL, 0);
94 static inline char* gu_str_table_set_n_rows(
char* str,
size_t* off,
size_t n)
96 return gu_str_table_append_size(str, off, n);
99 static inline size_t gu_str_table_get_n_rows(
const char* str)
101 str = gu_str_advance(str, 2);
102 return strtoul(str, NULL, 0);
106 static inline char* gu_str_table_set_cols(
char* str,
112 for (i = 0; i < n; ++i)
114 str = gu_str_append(str, off, cols[i], strlen(cols[i]));
119 static inline char* gu_str_table_append_row(
char* str,
125 for (i = 0; i < n; ++i)
127 str = gu_str_append(str, off, row[i], strlen(row[i]));
133 static inline const char* gu_str_table_get_cols(
const char* str,
size_t n,
137 str = gu_str_advance(str, 3);
138 for (i = 0; i < n; i++)
141 str = gu_str_next(str);
147 static inline const char* gu_str_table_rows_begin(
const char* str,
size_t n)
149 return gu_str_advance(str, 3 + n);
152 static inline const char* gu_str_table_row_get(
const char* str,
157 for (i = 0; i < n; ++i)
160 str = gu_str_next(str);
165 static inline void gu_str_table_print_row(FILE* file,
size_t n,
166 const char*
const row[])
169 for (i = 0; i < n; ++i)
171 fprintf(file,
"%s ", row[i]);
176 static inline void gu_str_table_print(FILE* file,
const char* str)
179 size_t n_cols, n_rows;
182 fprintf(file,
"%s\n", gu_str_table_get_name(str));
183 n_cols = gu_str_table_get_n_cols(str);
184 n_rows = gu_str_table_get_n_rows(str);
186 vec = malloc(n_cols*
sizeof(
char*));
187 ptr = gu_str_table_get_cols(str, n_cols, vec);
188 gu_str_table_print_row(file, n_cols, vec);
189 for (i = 0; i < n_rows; ++i)
191 ptr = gu_str_table_row_get(ptr, n_cols, vec);
192 gu_str_table_print_row(file, n_cols, vec);