diff --git a/example/ex-dict03.c b/example/ex-dict03.c index 396beabc..541de1b8 100644 --- a/example/ex-dict03.c +++ b/example/ex-dict03.c @@ -82,7 +82,7 @@ int main(void) } break; - case 7: + case 6: dict_map_clear(mp); string_clear (str); exit(0); @@ -90,6 +90,7 @@ int main(void) default: printf("Wrong Choice\n"); + break; } } } diff --git a/example/ex-grep01.c b/example/ex-grep01.c index e44c14cf..0c9ae428 100644 --- a/example/ex-grep01.c +++ b/example/ex-grep01.c @@ -26,11 +26,14 @@ int main(int argc, const char *argv[]) M_LET(positions, map_pos_t) { // Open the file FILE *f = fopen(argv[1], "rt"); - if (!f) return 1; + if (!f) { + fprintf(stderr, "ERROR: Cannot open %s.\n", argv[1]); + return 1; + } // While it reads some word from the file while (string_fget_word(word, " \t\n\r\"(),=", f)) { - // Get the reference to this word in the dictionnary (or create it if needed) + // Get the reference to the array of this word in the dictionnary (or create it if needed) array_size_t *array = map_pos_get_at(positions, word); // Push the new offset to this word in the associated array array_size_push_back(*array, (size_t)ftell(f) - string_size(word)); @@ -42,8 +45,9 @@ int main(int argc, const char *argv[]) // Print the words and where they are: for M_EACH(pair, positions, map_pos_t) { string_fputs(stdout, pair->key); - for M_EACH(pos, pair->value, array_size_t) + for M_EACH(pos, pair->value, array_size_t) { printf(" %zu", *pos); + } printf("\n"); } } // All created objects are destroyed beyond this point diff --git a/example/ex-mph.c b/example/ex-mph.c index 89fa18fd..510c30f1 100644 --- a/example/ex-mph.c +++ b/example/ex-mph.c @@ -24,22 +24,25 @@ /* Define the needed data structure : */ -// Define a dictionnary of constant string --> integer +// Define a dictionnary of constant string --> integer and register it. DICT_DEF2(dict_mph, const char *, M_CSTR_OPLIST, uint32_t, M_DEFAULT_OPLIST) #define M_OPL_dict_mph_t() DICT_OPLIST(dict_mph, M_CSTR_OPLIST, M_DEFAULT_OPLIST) -// Defin an array of integer +// Define an array of integer to store the value and register it. ARRAY_DEF(array_value, uint32_t) // Start from 1. 0 is 'None' #define M_OPL_array_value_t() ARRAY_OPLIST(array_value) -// Defin an array of integer +// Define an array of integer to store the seed and register it. ARRAY_DEF(array_seed, int32_t) #define M_OPL_array_seed_t() ARRAY_OPLIST(array_seed) +// Define algorithms on array_seed ALGO_DEF(array_seed, array_seed_t) // Define an array of constant string that are ordered by their size. ARRAY_DEF(array_cstr, const char *, M_CSTR_OPLIST) +// Register it globally but with a custom CMP operator static inline int array_cstr_cmp(const array_cstr_t a, const array_cstr_t b) { + // We compare only the size size_t sa = array_cstr_size(a); size_t sb = array_cstr_size(b); return (sa > sb) ? -1 : (sa < sb); @@ -191,7 +194,11 @@ static void dict_read_from_file(dict_mph_t dict, array_string_t arr, const char filename[]) { FILE *f = fopen(filename, "rt"); - if (!f) abort(); + if (!f) { + fprintf(stderr, "ERROR: Cannot open %s\n", filename); + exit(2); + } + uint32_t line = 1; M_LET(str, string_t) { while (!feof(f) && !ferror(f)) { @@ -234,12 +241,14 @@ test(array_seed_t seed, array_value_t value, dict_mph_t dict) int main(int argc, const char *argv[]) { + // Create arr as array_string_t, dict as dist_mph_t and seed as array_seed_t M_LET(arr, array_string_t) M_LET(dict, dict_mph_t) M_LET(seed, array_seed_t) + // and value as array_value_t M_LET(value, array_value_t) { - const char *filename = (argc > 1) ? argv[1] :"/usr/share/dict/words"; - dict_read_from_file(dict, arr, filename); - CreateMinimalPerfectHash(seed, value, dict); - test(seed, value, dict); + const char *filename = (argc > 1) ? argv[1] :"/usr/share/dict/words"; + dict_read_from_file(dict, arr, filename); + CreateMinimalPerfectHash(seed, value, dict); + test(seed, value, dict); } return 0; diff --git a/example/ex-multi01.c b/example/ex-multi01.c index f1deeaa7..fb1893f2 100644 --- a/example/ex-multi01.c +++ b/example/ex-multi01.c @@ -4,6 +4,7 @@ #include "m-array.h" #include "m-string.h" +// Define an array of integer ARRAY_DEF(array_int, int) typedef struct { @@ -25,7 +26,7 @@ static void tape_devance(tape_t tape) { if (tape->pos > 0) tape->pos--; } typedef struct { string_t code; - array_int_t bracket_map; // Should use map when possible. + array_int_t bracket_map; } program_t[1]; static void program_init(program_t p, string_t text) { @@ -98,7 +99,11 @@ static void program_run(program_t p){ static void read_file(string_t str, const string_t filename) { FILE *f = fopen(string_get_cstr(filename), "rt"); - if (!f) abort(); + if (!f) { + fprintf(stderr, "ERROR: Cannot open %s\n", string_get_cstr(filename)); + exit(2); + } + // Read the full FILE in the string string_fgets (str, f, STRING_READ_FILE); fclose(f); } diff --git a/example/ex-multi04.c b/example/ex-multi04.c index 2f499140..c81ef2c2 100644 --- a/example/ex-multi04.c +++ b/example/ex-multi04.c @@ -51,6 +51,9 @@ TUPLE_DEF2(mlib_points, (first_ref, mlib_point_ptr, M_PTR_OPLIST), static void let2(void) { + // Initialize p1 as mlib_point_t with (4,4) + // and p2 as mlib_point_t with (5,5) + // and pair as mlib_point_t with default value M_LET((p1, 4, 4), (p2, 5, 5), mlib_point_t) { M_LET(pair, mlib_points_t) { pair->first_ref = p1; @@ -68,6 +71,7 @@ static void let2(void) static void let(void) { + // Let's initialize all the declared types M_LET(s, symbol_t) M_LET(l, symbol_list_t) M_LET(r,record_t) diff --git a/example/ex-json01.c b/example/ex11-json01.c similarity index 88% rename from example/ex-json01.c rename to example/ex11-json01.c index 4cfa6468..4cd595a9 100644 --- a/example/ex-json01.c +++ b/example/ex11-json01.c @@ -2,9 +2,6 @@ #include "m-tuple.h" #include "m-array.h" -// Serial json is not supported for standard types if not C11 -#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L - // Let's define a 'person' composed of several fields. TUPLE_DEF2(person, (surname, string_t), @@ -50,7 +47,7 @@ int main(void) // Let's have base as a base_t M_LET(base, base_t) { // Read the JSON file and fill-in base - read(base, "ex-json01.json"); + read(base, "ex11-json01.json"); // Print the contents of base: printf ("List of presents:\n"); for M_EACH(el, base, base_t) { @@ -61,10 +58,3 @@ int main(void) } exit(0); } - -#else -int main(void) -{ - exit(0); -} -#endif diff --git a/example/ex-json01.json b/example/ex11-json01.json similarity index 100% rename from example/ex-json01.json rename to example/ex11-json01.json diff --git a/example/ex-section.c b/example/ex11-section.c similarity index 67% rename from example/ex-section.c rename to example/ex11-section.c index 8defead3..f5d072f5 100644 --- a/example/ex-section.c +++ b/example/ex11-section.c @@ -1,5 +1,3 @@ -#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L - #include #include "m-array.h" @@ -7,12 +5,15 @@ #include "m-dict.h" #include "m-string.h" +// Let's define a tuple composed of (offset, value) and register it TUPLE_DEF2(symbol, (offset, long), (value, long)) #define M_OPL_symbol_t() TUPLE_OPLIST(symbol, M_DEFAULT_OPLIST, M_DEFAULT_OPLIST) +// Let's define an array of such tuple and register it ARRAY_DEF(array_symbol, symbol_t) #define M_OPL_array_symbol_t() ARRAY_OPLIST(array_symbol, M_OPL_symbol_t()) +// Let's define a dictionnary associating a string_t to such an array and register it DICT_DEF2(sections, string_t, array_symbol_t) #define M_OPL_sections_t() DICT_OPLIST(sections, STRING_OPLIST, M_OPL_array_symbol_t()) @@ -20,13 +21,21 @@ int main(int argc, const char *argv[]) { if (argc < 2) abort(); FILE *f = fopen(argv[1], "rt"); - if (!f) abort(); + if (!f) { + fprintf(stderr, "ERROR: Cannot open %s\n", argv[1]); + return 2; + } + + // Create & initialize sc as sections_t M_LET(sc, sections_t) { + // Read the section from the FILE sections_in_str(sc, f); + // Get the section named .text array_symbol_t *a = sections_get(sc, STRING_CTE(".text")); if (a == NULL) { printf("There is no .text section."); } else { + // Print its content printf("Section .text is :"); array_symbol_out_str(stdout, *a); printf("\n"); @@ -34,7 +43,3 @@ int main(int argc, const char *argv[]) } return 0; } - -#else -int main(void) { return 0; } -#endif