Mercurial > hg > batmud > maputils
changeset 763:44aa26303fa7
Rewrite commandline handling to use th-libs, and other misc. improvements.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 08 May 2009 15:10:47 +0000 |
parents | f0423a3f999a |
children | 508ca16e1f6b |
files | mkbcmap.c |
diffstat | 1 files changed, 167 insertions(+), 50 deletions(-) [+] |
line wrap: on
line diff
--- a/mkbcmap.c Tue May 05 19:48:34 2009 +0000 +++ b/mkbcmap.c Fri May 08 15:10:47 2009 +0000 @@ -1,10 +1,10 @@ /* * mkbcmap - Create interactive XHTML+CSS+JS map from input * Programmed by Matti 'ccr' Hämäläinen (Ggr Pupunen) - * (C) Copyright 2006-2008 Tecnic Software productions (TNSP) + * (C) Copyright 2006-2009 Tecnic Software productions (TNSP) */ -#include <string.h> #include "maputils.h" +#include "th_args.h" #include "th_string.h" @@ -14,11 +14,106 @@ char *extra; } location_t; -#define PASKA +enum { + OUT_ASCII, + OUT_HTML +}; + #define MAX_LOC (512) -int nlocations = 0; -location_t locations[MAX_LOC]; -BOOL optUseTable = FALSE; +int nlocations = 0; +location_t locations[MAX_LOC]; + +char *optDestFilename = NULL, + *optMapFilename = NULL, + *optDescFilename = NULL, + *optUrchinFile = NULL, + *optMapTitle = NULL; + +BOOL optUseTable = FALSE; +int optOutputFormat = OUT_HTML; + + +/* Arguments + */ +optarg_t optList[] = { + { 0, '?', "help", "Show this help", OPT_NONE }, + { 1, 'v', "verbose", "Be more verbose", OPT_NONE }, + { 2, 'o', "output", "Output filename", OPT_ARGREQ }, + { 3, 'A', "ascii", "Use plain ASCII output", OPT_NONE }, + { 4, 'T', "table", "Use HTML table", OPT_NONE }, + { 5, 't', "title", "Map title", OPT_ARGREQ }, + { 6, 'u', "urchin-file", "Specify urchin file", OPT_ARGREQ }, +}; + +const int optListN = (sizeof(optList) / sizeof(optarg_t)); + + +void argShowHelp(void) +{ + th_args_help(stderr, optList, optListN, th_prog_name, + "[options] <input mapfile> <descfile>"); +} + + +BOOL argHandleOpt(const int optN, char *optArg, char *currArg) +{ + switch (optN) { + case 0: + argShowHelp(); + exit(0); + break; + + case 1: + th_verbosityLevel++; + break; + + case 2: + optDestFilename = optArg; + THMSG(2, "Output file set to '%s'.\n", optDestFilename); + break; + + case 3: + optOutputFormat = OUT_ASCII; + break; + + case 4: + optUseTable = TRUE; + THMSG(2, "Using HTML table formatting.\n"); + break; + + case 5: + optMapTitle = optArg; + THMSG(2, "Map title string set to '%s'.\n", optMapTitle); + break; + + case 6: + optUrchinFile = optArg; + THMSG(2, "Urchin filename set to '%s'.\n", optUrchinFile); + break; + + default: + THERR("Unknown option '%s'.\n", currArg); + return FALSE; + } + + return TRUE; +} + + +BOOL argHandleFile(char *currArg) +{ + if (!optMapFilename) + optMapFilename = currArg; + else if (!optDescFilename) + optDescFilename = currArg; + else { + THERR("Too many filenames specified!\n"); + return FALSE; + } + + return TRUE; +} + void addLoc(char c, char *desc, char *extra) @@ -47,6 +142,7 @@ return -1; } + void putEnd(FILE *f, int c, int p) { if (p != -1) { @@ -150,12 +246,12 @@ } -void outputHTML(FILE *outFile, FILE *inFile, char *mapTitle) +void outputHTML(FILE *outFile, FILE *inFile) { int n, k; /* Output XHTML header */ - mcXHTMLhead(outFile, mapTitle); + mcXHTMLhead(outFile, optMapTitle); /* Output CSS style information */ fprintf(outFile, @@ -177,7 +273,6 @@ " div.loctab table tr { vertical-align: top; font-size: 8pt; }\n" ); -#ifdef PASKA if (!optUseTable) { fprintf(outFile, " div.map {\n" @@ -209,7 +304,6 @@ " div.loctab table { width: 95%%; }\n" ); } -#endif mcXHTMLcolors(outFile, "span", NULL, NULL); @@ -219,20 +313,18 @@ "<body onLoad=\"httOnLoad();\">\n" ); - if (optUseTable) { + if (optUseTable && optMapTitle) { fprintf(outFile, "<h3>"); - fprinte(outFile, mapTitle); + fprinte(outFile, optMapTitle); fprintf(outFile, "</h3>\n"); } -#ifdef PASKA if (optUseTable) fprintf(outFile, "<table>\n" " <tr>\n" " <td>\n" ); -#endif fprintf(outFile, "<div class=\"map\">" @@ -248,7 +340,6 @@ "</div>\n" ); -#ifdef PASKA if (optUseTable) { fprintf(outFile, " </td>\n" @@ -277,7 +368,6 @@ "</table>\n" ); } -#endif for (n = 0; n < nlocations; n++) if (locations[n].desc) { @@ -301,13 +391,19 @@ } -void outputASCII(FILE *outFile, FILE *inFile, char *mapTitle) +void outputASCII(FILE *outFile, FILE *inFile) { int c, n, m; + size_t i; char fmt[32]; /* Output title */ - fprintf(outFile, "%s\n\n", mapTitle); + if (optMapTitle) { + fprintf(outFile, "%s\n", optMapTitle); + for (i = 0; i < strlen(optMapTitle); i++) + fputc('-', outFile); + fprintf(outFile, "\n\n"); + } /* Process input, convert to map */ while ((c = fgetc(inFile)) != EOF) { @@ -322,7 +418,7 @@ if (c > m) m = c; } - sprintf(fmt, "%%c %%-%ds | ", m); + snprintf(fmt, sizeof(fmt), "%%c %%-%ds | ", m); /* Print location lists */ m = (nlocations / 2); @@ -343,15 +439,15 @@ } -int parseLocFile(char *pcFilename) +int parseLocFile(char *filename) { FILE *inFile; char s[4096]; size_t i, j, k; - if ((inFile = fopen(pcFilename, "rb")) == NULL) { - fprintf(stderr, "Could not open file '%s' for reading.\n", - pcFilename); + if ((inFile = fopen(filename, "rb")) == NULL) { + THERR("Could not open file '%s' for reading.\n", + filename); return -1; } @@ -390,52 +486,73 @@ int main(int argc, char *argv[]) { FILE *inFile, *outFile; - char *mapTitle, *mapFilename, *descFilename; + + th_init("mkbcmap", "ASCII map converter", "0.3", NULL, NULL); + th_verbosityLevel = 0; - if (argc < 4) { - fprintf(stderr, - "Usage: %s \"<title>\" mapfile descfile > outfile\n", - argv[0]); + /* Parse arguments */ + if (!th_args_process(argc, argv, optList, optListN, + argHandleOpt, argHandleFile, TRUE)) + exit(1); + + if (optMapFilename == NULL || optDescFilename == NULL) { + THERR("You need to specify at least map and desc filenames. (try --help)\n"); exit(0); } - outFile = stdout; - mapTitle = argv[1]; - mapFilename = argv[2]; - descFilename = argv[3]; - /* Parse desc file */ - if (parseLocFile(descFilename) < 0) { - fprintf(stderr, - "Could not properly parse desc file '%s'\n", - descFilename); + if (parseLocFile(optDescFilename) < 0) { + THERR("Could not properly parse desc file '%s'\n", + optDescFilename); exit(3); } - fprintf(stderr, "Parsed %d locations from '%s'.\n", nlocations, descFilename); + THMSG(1, "Parsed %d locations from '%s'.\n", + nlocations, optDescFilename); /* Open mapfile */ - if ((inFile = fopen(mapFilename, "rb")) == NULL) { - fprintf(stderr, - "Could not open mapfile '%s' for reading.\n", - mapFilename); + if ((inFile = fopen(optMapFilename, "rb")) == NULL) { + THERR("Could not open mapfile '%s' for reading.\n", + optMapFilename); exit(3); } - fprintf(stderr, "Using input map '%s', processing ..\n", mapFilename); + if (optDestFilename == NULL) + outFile = stdout; + else if ((outFile = fopen(optDestFilename, "wb")) == NULL) { + fclose(inFile); + THERR("Error opening output file '%s'!\n", + optDestFilename); + exit(1); + } + + THMSG(1, "Using input map '%s', processing ..\n", optMapFilename); /* Output data */ - if (argc > 4 && !strcmp(argv[4], "-ascii")) { - outputASCII(outFile, inFile, mapTitle); - } else { - if (argc > 4 && !strcmp(argv[4], "-usetable")) - optUseTable = TRUE; + switch (optOutputFormat) { + case OUT_ASCII: + outputASCII(outFile, inFile); + break; - outputHTML(outFile, inFile, mapTitle); + case OUT_HTML: + outputHTML(outFile, inFile); + break; + } + + fclose(inFile); + + if (optUrchinFile) { + if ((inFile = fopen(optUrchinFile, "rb")) != NULL) { + int c; + while ((c = fgetc(inFile)) != EOF) + fputc(c, outFile); + fclose(inFile); + } else { + THERR("Urchin file '%s' not found!\n", optUrchinFile); + } } /* Close input and output files */ - fclose(inFile); fclose(outFile); exit(0);