22 #define LINE_BUFFER_SIZE 256
25 static void WriteSub(FILE *Output,
char *Sub,
int Column);
44 int FillTemplate(
const char *OutputFilename,
const char *TemplateFilename,
45 char *Keys[],
char *Subs[])
47 FILE *Template = NULL, *Output = NULL;
48 char *Token, *NextPos;
50 struct tm *CurrentTimeInfo;
52 int LineNumber = 0, Success = 0, k, Column;
54 if(!(Template = fopen(TemplateFilename,
"rt")))
56 fprintf(stderr,
"Unable to open template \"%s\".\n",
60 else if(!(Output = fopen(OutputFilename,
"wt")))
62 fprintf(stderr,
"Unable to open \"%s\" for writing.\n",
69 CurrentTimeInfo = localtime (&CurrentTime);
70 strftime(TimeString, 64,
"%Y-%m-%d %H:%M:%S", CurrentTimeInfo);
78 while((NextPos = strstr(Token,
"${")))
84 Column = NextPos - Line;
87 if(!(NextPos = strchr(Token,
'}')))
89 fprintf(stderr,
"Line %d: Missing '}'.\n", LineNumber);
95 if(!strcmp(Token,
"TIME"))
96 fputs(TimeString, Output);
97 else if(!strcmp(Token,
"TEMPLATE_FILENAME"))
98 fputs(TemplateFilename, Output);
99 else if(!strcmp(Token,
"OUTPUT_FILENAME"))
100 fputs(OutputFilename, Output);
104 if(!Keys[k] || !Subs[k])
106 fprintf(stderr,
"Line %d: Unknown key \"%s\".\n",
110 else if(!strcmp(Token, Keys[k]))
113 WriteSub(Output, Subs[k], Column);
121 fputs(Token, Output);
126 fprintf(stderr,
"Error reading \"%s\".\n", TemplateFilename);
129 else if(ferror(Output))
131 fprintf(stderr,
"Error writing \"%s\".\n", OutputFilename);
154 static void WriteSub(FILE *Output,
char *Sub,
int Column)
160 for(Token = Sub; (NextPos = strchr(Token,
'\n')); Token = NextPos + 1)
163 fwrite(Token,
sizeof(
char), NextPos - Token + 1, Output);
166 for(k = 0; k < Column; k++)
171 fputs(Token, Output);
186 char **
AddPair(
char **Keys[],
char **Subs[],
char *Key,
char *SubFormat, ...)
193 for(; (*Keys)[i]; i++)
194 if(!strcmp((*Keys)[i], Key))
199 if(!(*Keys = (
char **)realloc(*Keys,
sizeof(
char *)*(i + 2)))
200 || !(*Subs = (
char **)realloc(*Subs,
sizeof(
char *)*(i + 2)))
202 || !((*Keys)[i] = (
char *)malloc(strlen(Key) + 1)))
204 fprintf(stderr,
"Out of memory.");
208 strcpy((*Keys)[i], Key);
210 (*Keys)[i + 1] = NULL;
211 (*Subs)[i + 1] = NULL;
214 va_start(Args, SubFormat);
230 va_start(Args, Format);
242 vsprintf(Buffer, Format, Args);
245 if(!(NewStr = (
char *)realloc(*Str,
246 ((*Str) ? strlen(*Str) : 0) + strlen(Buffer) + 1)))
248 fprintf(stderr,
"Out of memory.");
253 strcat(NewStr, Buffer);
255 strcpy(NewStr, Buffer);
271 for(i = 0; Strs[i]; i++)