#include <stdio.h> #include <stdlib.h> #include <string.h> #include "replacer.h" /* -------------------------------------------------- */ extern char** txt_tokens; extern size_t nb_tokens; /* -------------------------------------------------- */ #define NB_LINE 512 #define NB_TAGS 8 #define TAGS_SIZE 100 #define VALUES_SIZE 100 static char buff1[]="A ${TAG1} B ${TAG2} C\nA ${TAG3} B ${TAG4} C\nA ${TAG5} B ${TAG6} C\nA ${TAG7} B ${TAG8} C\n\n${TAGNNNN}\n"; int main() { int res; char *buff, *result; char tags[NB_TAGS][TAGS_SIZE], *tag[NB_TAGS]; char values[NB_TAGS][VALUES_SIZE], *value[NB_TAGS]; /* ------------------------------------------------ */ /* Create couples (tags, values) */ /* ------------------------------------------------ */ for (res=0; res<NB_TAGS; res++) { sprintf (tags[res], "TAG%d", res+1); sprintf (values[res], "Value_%d", res+1); tag[res] = tags[res]; value[res] = values[res]; } /* ------------------------------------------------ */ /* Create a huge test buffer */ /* ------------------------------------------------ */ buff = (char*)malloc( strlen(buff1) * NB_LINE + 1 ); if (buff == NULL) { fprintf (stderr, "\n\nCan not allocate memory!\n\n"); return 1; } *buff = 0; for (res=0; res<NB_LINE; res++) { strcat (buff, buff1); } /* ------------------------------------------------ */ /* Search / Replace in the in-memory buffer */ /* ------------------------------------------------ */ result = replace_tags (buff, tag, value, NB_TAGS); if (result == NULL) { fprintf (stderr, "\n\nCan not allocate memory!\n\n"); return 1; } /* ------------------------------------------------ */ /* Now print the result */ /* ------------------------------------------------ */ fprintf (stdout, "\n\nReplaced text is:\n\n[%s]\n\n", result); fprintf (stdout, "\n\n"); /* ------------------------------------------------ */ /* Free all allocated memory */ /* ------------------------------------------------ */ free (result); free (buff); return res; }