/****************************************************************************** MESS - dat2html.c Generates an index and individual system html files from sysinfo.dat ******************************************************************************/ #include #include #include #include #include #include "osdcore.h" #include "osdmess.h" #include "utils.h" void replace_lt_gt(char *line) { char buff[1024] = "", *src = line, *dst = buff; if (!strchr(src, '<') && !strchr(src, '>')) return; while( *src ) { if( *src == '<' ) dst += sprintf(dst, "<"); else if( *src == '>' ) dst += sprintf(dst, ">"); else *dst++ = *src; src++; } *dst = '\0'; strcpy(line, buff); } void a_href_url(char *line) { char buff[1024], c; char *url_beg = strstr(line, "http://"), *url_end; int length; if (!url_beg) return; url_end = strchr(url_beg, ' '); if (!url_end) url_end = url_beg + strlen(url_beg); length = (int) (url_end - url_beg); /* insert the a href */ strcpy(buff, line); /* terminate URL */ c = *url_end; *url_end = '\0'; /* terminate buffer right before the URL */ buff[(int)(url_beg - line)] = '\0'; /* add "); strcat(buff, url_beg); strcat(buff, ""); *url_end = c; strcat(buff, url_end); strcpy(line, buff); } #ifdef _WIN32 int CLIB_DECL utf8_main(int ac, char **av) #else int CLIB_DECL main(int ac, char **av) #endif { char dat_filename[128] = "sysinfo.dat"; char html_filename[128] = "sysinfo.htm"; char html_directory[128] = "sysinfo"; char system_filename[128] = ""; char system_name[128] = ""; int systemcount = 0, linecount = 0, emptycount = 0, ulcount = 0; FILE *dat, *html, *html_system = NULL; time_t tm; tm = time(NULL); if( ac > 1 ) { strcpy(dat_filename, av[1]); if( ac > 2 ) { strcpy(html_filename, av[2]); if( ac > 3 ) { strcpy(html_directory, av[3]); } } } dat = fopen(dat_filename, "r"); if( !dat ) { fprintf(stderr, "cannot open input file '%s'.\n", dat_filename); return 1; } html = fopen(html_filename, "w"); if( !html ) { fprintf(stderr, "cannot create output file '%s'.\n", html_filename); return 1; } osd_mkdir(html_directory); /* Head of the index html file */ fprintf(html, "\n"); fprintf(html, "\n"); fprintf(html, "Contents of %s\n", dat_filename); fprintf(html, "\n"); fprintf(html, "\n"); fprintf(html, "

Contents of %s

\n", dat_filename); fprintf(html, "
\n"); fprintf(html, "

\n"); while( !feof(dat) ) { char line[1024], *eol; fgets(line, sizeof(line), dat); eol = strchr(line, '\n'); if( eol ) *eol = '\0'; eol = strchr(line, '\r'); if( eol ) *eol = '\0'; if( line[0] != '#' ) { if( line[0] == '$' ) { if( mame_strnicmp(line + 1, "info", 4) == 0 ) { char *eq = strchr(line, '='); strcpy(system_name, eq + 1); sprintf(system_filename, "%s/%s.htm", html_directory, system_name); fprintf(html, "•  %s\n", system_filename, system_name); systemcount++; html_system = fopen(system_filename, "w"); if( !html_system ) { fprintf(stderr, "cannot create system_name file '%s'.\n", system_filename); return 1; } /* Head of the system html code */ fprintf(html_system, "\n"); fprintf(html_system, "\n"); fprintf(html_system, "Info for %s\n", system_name); fprintf(html_system, "\n"); fprintf(html_system, "\n"); fprintf(html_system, "\n"); fprintf(html_system, "\n"); fprintf(html_system, "\n", html_filename); fprintf(html_system, "\n", system_name); fprintf(html_system, "\n"); fprintf(html_system, "

Back to index

Info for %s

\n"); fprintf(html_system, "


\n"); linecount = 0; emptycount = 0; } else if( mame_strnicmp(line + 1, "bio", 3) == 0 ) { /* that's just fine... */ } else if( mame_strnicmp(line + 1, "end", 3) == 0 ) { if (html_system) { fprintf(html_system, "
\n"); fprintf(html_system, "
Generated on %s
\n", ctime(&tm)); fprintf(html_system, "\n"); fprintf(html_system, "\n"); fclose(html_system); html_system = NULL; } } } else { if( html_system ) { if ( strlen(line) == 0 ) { if ( emptycount++ > 1 ) fprintf(html_system, "
\n"); } else { replace_lt_gt(line); a_href_url(line); emptycount = 0; if ( linecount == 0 ) { if( ulcount ) fprintf(html_system, "\n"); ulcount = 0; /* first line is header 4 */ fprintf(html_system, "

%s

\n", line); /* Add description to index file */ fprintf(html, " - %s
\n", line); } else { int ul = 0; while( line[ul] && isspace(line[ul]) ) ul++; /* lines beginning with a dash are lists!? */ if( ul > 0 && line[ul] == '-' ) { if( ulcount == 0 ) fprintf(html_system, "
    \n"); fprintf(html_system, "
  • %s\n", line + ul + 1); ulcount++; } else { if( ulcount ) fprintf(html_system, "
\n"); ulcount = 0; /* lines ending in a colon are bold */ if( line[strlen(line)-1] == ':' ) fprintf(html_system, "

%s
\n", line); else fprintf(html_system, "%s
\n", line); } } } } linecount++; } } } fprintf(html, "

\n"); fprintf(html, "
\n"); fprintf(html, "
Generated on %s
\n", ctime(&tm)); fprintf(html, "\n"); fprintf(html, "\n"); fclose(html); fclose(dat); return 0; }