2 * Generate a file with test registrations from a list
3 * of files in a directory.
4 * Casper S. Hornstrup <chorns@users.sourceforge.net>
19 #include <sys/types.h>
27 #define DIR_SEPARATOR_CHAR '/'
28 #define DIR_SEPARATOR_STRING "/"
30 #define DIR_SEPARATOR_CHAR '\\'
31 #define DIR_SEPARATOR_STRING "\\"
38 char* convert_path(char* origpath
)
43 newpath
= strdup(origpath
);
46 while (newpath
[i
] != 0)
49 if (newpath
[i
] == '\\')
55 if (newpath
[i
] == '/')
66 static void write_line(char *line
)
71 memset(buf
, 0, sizeof(buf
));
73 /* Terminate the line */
74 buf
[strlen(buf
)] = '\r';
75 buf
[strlen(buf
)] = '\n';
77 n_out
= fwrite(&buf
[0], 1, strlen(buf
), out
);
80 void register_test(char *filename
, int prototype
)
88 strcpy(testname
, filename
);
91 while (i
> 0 && testname
[i
] != '.')
97 memset(ext
, 0, sizeof(ext
));
98 strncpy(&ext
[0], &testname
[i
], strlen(&testname
[i
]));
100 if ((strncmp(ext
, ".c", 2) != 0) && (strncmp(ext
, ".C", 2) != 0))
112 // Make a capital first letter and make all other letters lower case
113 testname
[0] = toupper(testname
[0]);
114 if (!((testname
[0] >= 'A' && testname
[0] <= 'Z') ||
115 (testname
[0] >= '0' && testname
[0] <= '9')))
120 while (j
< strlen(testname
))
122 testname
[j
] = tolower(testname
[j
]);
123 if (!((testname
[j
] >= 'a' && testname
[j
] <= 'z') ||
124 (testname
[j
] >= '0' && testname
[j
] <= '9')))
133 sprintf(regtest
, "extern int %sTest(int Command, char *Buffer);", testname
);
138 sprintf(call
, "%sTest", testname
);
139 sprintf(regtest
, " AddTest((TestRoutine)%s);", call
);
149 make_file_list (int prototype
)
151 struct _finddata_t f
;
153 char searchbuf
[MAX_PATH
];
155 strcpy(searchbuf
, path
);
156 strcpy(searchbuf
, "*.*");
157 findhandle
=_findfirst(searchbuf
, &f
);
158 if (findhandle
!= -1)
162 if (f
.attrib
& _A_SUBDIR
)
164 /* Skip subdirectories */
168 register_test(f
.name
, prototype
);
170 while (_findnext(findhandle
, &f
) == 0);
171 _findclose(findhandle
);
179 make_file_list (int prototype
)
182 struct dirent
*entry
;
187 dirp
= opendir(path
);
190 while ((entry
= readdir(dirp
)) != NULL
)
192 if (strcmp(entry
->d_name
, ".") == 0 || strcmp(entry
->d_name
, "..") == 0)
193 continue; // skip self and parent
195 if (entry
->d_type
== DT_REG
) // normal file
197 // Check for an absolute path
198 if (path
[0] == DIR_SEPARATOR_CHAR
)
201 strcat(buf
, DIR_SEPARATOR_STRING
);
202 strcat(buf
, entry
->d_name
);
206 getcwd(buf
, sizeof(buf
));
207 strcat(buf
, DIR_SEPARATOR_STRING
);
209 strcat(buf
, entry
->d_name
);
212 if (stat(buf
, &stbuf
) == -1)
214 printf("Can't access '%s' (%s)\n", buf
, strerror(errno
));
218 if (S_ISDIR(stbuf
.st_mode
))
220 /* Skip subdirectories */
224 register_test(entry
->d_name
, prototype
);
231 printf("Can't open %s\n", path
);
237 dirp
= opendir(path
);
240 while ((entry
= readdir(dirp
)) != NULL
)
242 if (strcmp(entry
->d_name
, ".") == 0 || strcmp(entry
->d_name
, "..") == 0)
243 continue; // skip self and parent
245 // Check for an absolute path
246 if (path
[0] == DIR_SEPARATOR_CHAR
)
249 strcat(buf
, DIR_SEPARATOR_STRING
);
250 strcat(buf
, entry
->d_name
);
254 getcwd(buf
, sizeof(buf
));
255 strcat(buf
, DIR_SEPARATOR_STRING
);
257 strcat(buf
, entry
->d_name
);
260 if (stat(buf
, &stbuf
) == -1)
262 printf("Can't access '%s' (%s)\n", buf
, strerror(errno
));
266 if (S_ISDIR(stbuf
.st_mode
))
268 /* Skip subdirectories */
272 register_test(entry
->d_name
, prototype
);
278 printf("Can't open %s\n", path
);
288 "REGTESTS path file\n"
290 " path Path to files\n"
291 " file File to create\n";
293 int main(int argc
, char **argv
)
305 strcpy(buf
, convert_path(argv
[1]));
306 if (buf
[strlen(buf
)] != DIR_SEPARATOR_CHAR
)
309 buf
[strlen(buf
)] = DIR_SEPARATOR_CHAR
;
315 printf("Missing path\n");
319 file
= convert_path(argv
[2]);
322 printf("Missing file\n");
326 out
= fopen(file
, "wb");
329 perror("Cannot open output file");
333 write_line("/* This file is autogenerated. */");
335 write_line("typedef int (*TestRoutine)(int Command, char *Buffer);");
341 write_line("extern void AddTest(TestRoutine Routine);");
343 write_line("void RegisterTests()");