ReactOS Fundraising Campaign 2012
 
€ 4,060 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

regtests2xml.c
Go to the documentation of this file.
00001 /*
00002  * Convert debug output from running the regression tests
00003  * on ReactOS to an xml document.
00004  * Casper S. Hornstrup <chorns@users.sourceforge.net>
00005  */
00006 
00007 #include <stdio.h>
00008 #include <fcntl.h>
00009 #include <sys/stat.h>
00010 #include <stdlib.h>
00011 #include <string.h>
00012 
00013 #ifdef WIN32
00014 #include <io.h>
00015 #include <dos.h>
00016 #else
00017 #include <sys/io.h>
00018 #include <errno.h>
00019 #include <sys/types.h>
00020 #include <dirent.h>
00021 #include <unistd.h>
00022 #endif
00023 #include <ctype.h>
00024 #ifndef WIN32
00025 #ifndef MAX_PATH
00026 #define MAX_PATH 260
00027 #endif
00028 #define DIR_SEPARATOR_CHAR '/'
00029 #define DIR_SEPARATOR_STRING "/"
00030 #else
00031 #define DIR_SEPARATOR_CHAR '\\'
00032 #define DIR_SEPARATOR_STRING "\\"
00033 #endif
00034 
00035 typedef struct _TEST_RESULT_INFO
00036 {
00037   struct _TEST_RESULT_INFO *next;
00038   char testname[100];
00039   char result[200];
00040   int succeeded; /* 0 = failed, 1 = succeeded */
00041 } TEST_RESULT_INFO, *PTEST_RESULT_INFO;
00042 
00043 
00044 static FILE *out;
00045 static FILE *file_handle = NULL;
00046 static char *file_buffer = NULL;
00047 static unsigned int file_size = 0;
00048 static int file_pointer = 0;
00049 static PTEST_RESULT_INFO test_result_info_list = NULL;
00050 
00051 
00052 static char*
00053 convert_path(char* origpath)
00054 {
00055    char* newpath;
00056    int i;
00057 
00058    newpath = strdup(origpath);
00059 
00060    i = 0;
00061    while (newpath[i] != 0)
00062      {
00063 #ifndef WIN32
00064     if (newpath[i] == '\\')
00065       {
00066          newpath[i] = '/';
00067       }
00068 #else
00069 #ifdef WIN32
00070     if (newpath[i] == '/')
00071       {
00072          newpath[i] = '\\';
00073       }
00074 #endif
00075 #endif
00076     i++;
00077      }
00078    return(newpath);
00079 }
00080 
00081 static void
00082 write_line(char *line)
00083 {
00084   char buf[200];
00085 
00086   memset(buf, 0, sizeof(buf));
00087   strcpy(buf, line);
00088   /* Terminate the line */
00089   buf[strlen(buf)] = '\r';
00090   buf[strlen(buf)] = '\n';
00091 
00092   (void)fwrite(&buf[0], 1, strlen(buf), out);
00093 }
00094 
00095 
00096 static void
00097 read_file(char *filename)
00098 {
00099   file_handle = fopen(filename, "rb");
00100   if (file_handle == NULL)
00101     {
00102       printf("Can't open %s\n", filename);
00103       exit(1);
00104     }
00105 
00106   // Get the size of the file
00107   fseek(file_handle, 0, SEEK_END);
00108   file_size = ftell(file_handle);
00109 
00110   // Load it all into memory
00111   file_buffer = malloc(file_size);
00112   if (file_buffer == NULL)
00113     {
00114       fclose(file_handle);
00115       printf("Out of memory\n");
00116       exit(1);
00117     }
00118   fseek(file_handle, 0, SEEK_SET);
00119   if (file_size > 0)
00120     {
00121       if (fread (file_buffer, 1, file_size, file_handle) < 1)
00122         {
00123           fclose(file_handle);
00124           printf("Read error in file %s\n", filename);
00125           exit(1);
00126         }
00127     }
00128 
00129   file_pointer = 0;
00130 }
00131 
00132 static void
00133 close_file()
00134 {
00135   free(file_buffer);
00136   file_buffer = NULL;
00137   fclose(file_handle);
00138   file_handle = NULL;
00139   file_pointer = 0;
00140 }
00141 
00142 static int
00143 is_whitespace(char ch)
00144 {
00145   if (ch == ' ')
00146     {
00147       return 1;
00148     }
00149   if (ch == '\t')
00150     {
00151       return 1;
00152     }
00153   return 0;
00154 }
00155 
00156 static int
00157 is_eol_char(char ch)
00158 {
00159   if (ch == '\r')
00160     {
00161       return 1;
00162     }
00163   if (ch == '\n')
00164     {
00165       return 1;
00166     }
00167   return 0;
00168 }
00169 
00170 static void
00171 skip_line()
00172 {
00173   while ((file_pointer < file_size) && (!is_eol_char(file_buffer[file_pointer])))
00174     {
00175       file_pointer++;
00176     }
00177   if ((file_pointer < file_size) && (is_eol_char(file_buffer[file_pointer])))
00178     {
00179       file_pointer++;
00180       if ((file_pointer < file_size) && (file_buffer[file_pointer] == '\n'))
00181         {
00182           file_pointer++;
00183         }
00184     }
00185 }
00186 
00187 static void
00188 skip_whitespace()
00189 {
00190   while ((file_pointer < file_size) && !is_eol_char(file_buffer[file_pointer])
00191     && is_whitespace(file_buffer[file_pointer]))
00192     {
00193       file_pointer++;
00194     }
00195 }
00196 
00197 static int
00198 skip_to_next_test()
00199 {
00200   static char test_marker[] = "ROSREGTEST:";
00201   int found_test = 0;
00202 
00203   while ((file_pointer < file_size) && (!found_test))
00204     {
00205       skip_whitespace();
00206       found_test = 1;
00207       int i = 0;
00208       while (1)
00209         {
00210           if (i >= strlen(test_marker))
00211             {
00212               break;
00213             }
00214           if (is_eol_char(file_buffer[file_pointer]))
00215             {
00216               found_test = 0;
00217               break;
00218             }
00219           if (file_buffer[file_pointer] != test_marker[i])
00220             {
00221               found_test = 0;
00222               break;
00223             }
00224           file_pointer++;
00225           i++;
00226         }
00227       if (!found_test)
00228         {
00229           skip_line();
00230         }
00231     }
00232   return found_test;
00233 }
00234 
00235 static int
00236 read_until(char ch, char* buf)
00237 {
00238   int start = file_pointer;
00239   while ((file_pointer < file_size))
00240     {
00241       if (file_buffer[file_pointer] == ch)
00242         {
00243           strncpy(buf, &file_buffer[start], file_pointer - start);
00244           buf[file_pointer - start] = 0;
00245           return 1;
00246         }
00247       file_pointer++;
00248     }
00249   return 0;
00250 }
00251 
00252 static int
00253 read_until_end(char* buf)
00254 {
00255   int start = file_pointer;
00256   while ((file_pointer < file_size))
00257     {
00258       if (is_eol_char(file_buffer[file_pointer]))
00259         {
00260           strncpy(buf, &file_buffer[start], file_pointer - start);
00261           buf[file_pointer - start] = 0;
00262           skip_line();
00263           return 1;
00264         }
00265       file_pointer++;
00266     }
00267   return 0;
00268 }
00269 
00270 static void
00271 parse_file(char *filename)
00272 {
00273   PTEST_RESULT_INFO test_result_info;
00274 
00275   read_file(filename);
00276 
00277   do
00278     {
00279       if (!skip_to_next_test())
00280         {
00281           break;
00282         }
00283 
00284       /*
00285        * FORMAT:
00286        * [ROSREGTEST:][space][|][<testname>][|][space][Status:][space][<result of running test>]
00287        */
00288 
00289       test_result_info = malloc(sizeof(TEST_RESULT_INFO));
00290       if (test_result_info == NULL)
00291         {
00292           printf("Out of memory\n");
00293           exit(1);
00294         }
00295 
00296       /* Skip whitespaces */
00297       skip_whitespace();
00298 
00299       /* [|] */
00300       file_pointer++;
00301 
00302       /* <testname> */
00303       read_until(')', test_result_info->testname);
00304 
00305       /* [|] */
00306       file_pointer++;
00307 
00308       /* [space] */
00309       file_pointer++;
00310 
00311       /* Status: */
00312       file_pointer += 7;
00313 
00314       /* [space] */
00315       file_pointer++;
00316 
00317       /* <result of running test> */
00318       read_until_end(test_result_info->result);
00319 
00320       if (strncmp(test_result_info->result, "Success", 7) == 0)
00321         {
00322           test_result_info->succeeded = 1;
00323         }
00324       else
00325         {
00326           test_result_info->succeeded = 0;
00327         }
00328 
00329       test_result_info->next = test_result_info_list;
00330       test_result_info_list = test_result_info;
00331     } while (1);
00332 
00333   close_file();
00334 }
00335 
00336 static void
00337 generate_xml()
00338 {
00339   PTEST_RESULT_INFO test_result_info;
00340   char buf[200];
00341   int success_rate;
00342   int succeeded_total;
00343   int failed_total;
00344 
00345   succeeded_total = 0;
00346   failed_total = 0;
00347 
00348   test_result_info = test_result_info_list;
00349   while (test_result_info != NULL)
00350     {
00351       if (test_result_info->succeeded)
00352         {
00353           succeeded_total++;
00354         }
00355       else
00356         {
00357           failed_total++;
00358         }
00359       test_result_info = test_result_info->next;
00360     }
00361 
00362   if (succeeded_total + failed_total > 0)
00363     {
00364       success_rate = ((succeeded_total) * 100) / (succeeded_total + failed_total);
00365     }
00366   else
00367     {
00368       success_rate = 100;
00369     }
00370 
00371   write_line("<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>");
00372   write_line("");
00373 
00374   sprintf(buf, "<testresults success_rate=\"%d\" succeeded_total=\"%d\" failed_total=\"%d\">",
00375     success_rate, succeeded_total, failed_total);
00376   write_line(buf);
00377 
00378   if (test_result_info_list != NULL)
00379     {
00380       test_result_info = test_result_info_list;
00381       while (test_result_info != NULL)
00382         {
00383           sprintf(buf, "<testresult testname=\"%s\" succeeded=\"%s\" result=\"%s\">",
00384             test_result_info->testname,
00385             test_result_info->succeeded == 1 ? "true" : "false",
00386             test_result_info->result);
00387           write_line(buf);
00388           write_line("</testresult>");
00389           test_result_info = test_result_info->next;
00390         }
00391     }
00392 
00393   write_line("</testresults>");
00394 }
00395 
00396 static char HELP[] =
00397   "REGTESTS2XML input-filename output-filename\n"
00398   "\n"
00399   "  input-filename   File containing output from running regression tests\n"
00400   "  output-filename  File to create\n";
00401 
00402 int main(int argc, char **argv)
00403 {
00404   char *input_file;
00405   char *output_file;
00406 
00407   if (argc < 3)
00408     {
00409       puts(HELP);
00410       return 1;
00411     }
00412 
00413   input_file = convert_path(argv[1]);
00414   if (input_file[0] == 0)
00415     {
00416       free(input_file);
00417       printf("Missing input-filename\n");
00418       return 1;
00419     }
00420 
00421   output_file = convert_path(argv[2]);
00422   if (output_file[0] == 0)
00423     {
00424       free(output_file);
00425       free(input_file);
00426       printf("Missing output-filename\n");
00427       return 1;
00428     }
00429 
00430   out = fopen(output_file, "wb");
00431   if (out == NULL)
00432     {
00433         free(input_file);
00434         free(output_file);
00435         printf("Cannot open output file");
00436         return 1;
00437      }
00438 
00439   parse_file(input_file);
00440 
00441   generate_xml();
00442 
00443   free(input_file);
00444   free(output_file);
00445   fclose(out);
00446 
00447   return 0;
00448 }

Generated on Tue May 22 2012 04:41:49 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.