(svn r22102) -Codechange: Also use the getopt function in strgen.

This commit is contained in:
alberth 2011-02-18 20:52:42 +00:00
parent 41a67a9b94
commit ff1e0a3ed3
6 changed files with 127 additions and 91 deletions

View File

@ -54,11 +54,15 @@ alloc_func.o: $(SRC_DIR)/core/alloc_func.cpp endian_host.h
$(E) '$(STAGE) Compiling $(<:$(SRC_DIR)/%.cpp=%.cpp)' $(E) '$(STAGE) Compiling $(<:$(SRC_DIR)/%.cpp=%.cpp)'
$(Q)$(CXX_BUILD) $(CFLAGS_BUILD) -DSTRGEN -c -o $@ $< $(Q)$(CXX_BUILD) $(CFLAGS_BUILD) -DSTRGEN -c -o $@ $<
getoptdata.o: $(SRC_DIR)/misc/getoptdata.cpp $(SRC_DIR)/misc/getoptdata.h
$(E) '$(STAGE) Compiling $(<:$(SRC_DIR)/misc/%.cpp=%.cpp)'
$(Q)$(CXX_BUILD) $(CFLAGS_BUILD) -DSTRGEN -c -o $@ $<
lang/english.txt: $(LANG_DIR)/english.txt lang/english.txt: $(LANG_DIR)/english.txt
$(Q)mkdir -p lang $(Q)mkdir -p lang
$(Q)cp $(LANG_DIR)/english.txt lang/english.txt $(Q)cp $(LANG_DIR)/english.txt lang/english.txt
$(STRGEN): alloc_func.o string.o strgen.o $(STRGEN): alloc_func.o string.o strgen.o getoptdata.o
$(E) '$(STAGE) Compiling and Linking $@' $(E) '$(STAGE) Compiling and Linking $@'
$(Q)$(CXX_BUILD) $(LDFLAGS_BUILD) $^ -o $@ $(Q)$(CXX_BUILD) $(LDFLAGS_BUILD) $^ -o $@

View File

@ -76,6 +76,7 @@
<ClCompile Include="..\src\core\alloc_func.cpp" /> <ClCompile Include="..\src\core\alloc_func.cpp" />
<ClCompile Include="..\src\strgen\strgen.cpp" /> <ClCompile Include="..\src\strgen\strgen.cpp" />
<ClCompile Include="..\src\string.cpp" /> <ClCompile Include="..\src\string.cpp" />
<ClCompile Include="..\src\misc\getoptdata.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\src\core\alloc_func.hpp" /> <ClInclude Include="..\src\core\alloc_func.hpp" />
@ -84,6 +85,7 @@
<ClInclude Include="..\src\core\endian_func.hpp" /> <ClInclude Include="..\src\core\endian_func.hpp" />
<ClInclude Include="..\src\stdafx.h" /> <ClInclude Include="..\src\stdafx.h" />
<ClInclude Include="..\src\string.h" /> <ClInclude Include="..\src\string.h" />
<ClInclude Include="..\src\misc\getoptdata.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">

View File

@ -16,6 +16,9 @@
<ClCompile Include="..\src\string.cpp"> <ClCompile Include="..\src\string.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\src\misc\getoptdata.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\src\core\alloc_func.hpp" /> <ClInclude Include="..\src\core\alloc_func.hpp" />
@ -24,5 +27,6 @@
<ClInclude Include="..\src\core\endian_func.hpp" /> <ClInclude Include="..\src\core\endian_func.hpp" />
<ClInclude Include="..\src\stdafx.h" /> <ClInclude Include="..\src\stdafx.h" />
<ClInclude Include="..\src\string.h" /> <ClInclude Include="..\src\string.h" />
<ClInclude Include="..\src\misc\getoptdata.h" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -125,6 +125,10 @@
RelativePath="..\src\string.cpp" RelativePath="..\src\string.cpp"
> >
</File> </File>
<File
RelativePath="..\src\misc\getoptdata.cpp"
>
</File>
</Filter> </Filter>
<File <File
RelativePath="..\src\core\alloc_func.hpp" RelativePath="..\src\core\alloc_func.hpp"
@ -150,6 +154,10 @@
RelativePath="..\src\string.h" RelativePath="..\src\string.h"
> >
</File> </File>
<File
RelativePath="..\src\misc\getoptdata.h"
>
</File>
</Files> </Files>
<Globals> <Globals>
</Globals> </Globals>

View File

@ -125,6 +125,10 @@
RelativePath="..\src\string.cpp" RelativePath="..\src\string.cpp"
> >
</File> </File>
<File
RelativePath="..\src\misc\getoptdata.cpp"
>
</File>
</Filter> </Filter>
<File <File
RelativePath="..\src\core\alloc_func.hpp" RelativePath="..\src\core\alloc_func.hpp"
@ -150,6 +154,10 @@
RelativePath="..\src\string.h" RelativePath="..\src\string.h"
> >
</File> </File>
<File
RelativePath="..\src\misc\getoptdata.h"
>
</File>
</Files> </Files>
<Globals> <Globals>
</Globals> </Globals>

View File

@ -14,6 +14,7 @@
#include "../string_func.h" #include "../string_func.h"
#include "../strings_type.h" #include "../strings_type.h"
#include "../language.h" #include "../language.h"
#include "../misc/getoptdata.h"
#include "../table/control_codes.h" #include "../table/control_codes.h"
#include <stdarg.h> #include <stdarg.h>
@ -1193,108 +1194,117 @@ static inline char *replace_pathsep(char *s)
static inline char *replace_pathsep(char *s) { return s; } static inline char *replace_pathsep(char *s) { return s; }
#endif #endif
/** Options of strgen. */
static const OptionData _opts[] = {
GETOPT_NOVAL( 'v', "--version"),
GETOPT_GENERAL('C', '\0', "-export-commands", ODF_NO_VALUE),
GETOPT_GENERAL('L', '\0', "-export-plurals", ODF_NO_VALUE),
GETOPT_GENERAL('P', '\0', "-export-pragmas", ODF_NO_VALUE),
GETOPT_NOVAL( 't', "--todo"),
GETOPT_NOVAL( 'w', "--warning"),
GETOPT_NOVAL( 'h', "--help"),
GETOPT_GENERAL('h', '?', NULL, ODF_NO_VALUE),
GETOPT_VALUE( 's', "--source_dir"),
GETOPT_VALUE( 'd', "--dest_dir"),
GETOPT_END(),
};
int CDECL main(int argc, char *argv[]) int CDECL main(int argc, char *argv[])
{ {
char pathbuf[MAX_PATH]; char pathbuf[MAX_PATH];
const char *src_dir = "."; const char *src_dir = ".";
const char *dest_dir = NULL; const char *dest_dir = NULL;
while (argc > 1 && *argv[1] == '-') { GetOptData mgo(argc - 1, argv + 1, _opts);
if (strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version") == 0) { for (;;) {
puts("$Revision$"); int i = mgo.GetOpt();
return 0; if (i == -1) break;
}
if (strcmp(argv[1], "-export-commands") == 0) { switch (i) {
printf("args\tflags\tcommand\treplacement\n"); case 'v':
for (const CmdStruct *cs = _cmd_structs; cs < endof(_cmd_structs); cs++) { puts("$Revision$");
char flags; return 0;
switch (cs->value) {
case 0x200E: case 0x200F: // Implicit BIDI controls
case 0x202A: case 0x202B: case 0x202C: case 0x202D: case 0x202E: // Explicit BIDI controls
case 0xA0: // Non breaking space
case '\n': // Newlines may be added too
case '{': // This special
/* This command may be in the translation when it is not in base */
flags = 'i';
break;
default: case 'C':
if (cs->proc == EmitGender) { printf("args\tflags\tcommand\treplacement\n");
flags = 'g'; // Command needs number of parameters defined by number of genders for (const CmdStruct *cs = _cmd_structs; cs < endof(_cmd_structs); cs++) {
} else if (cs->proc == EmitPlural) { char flags;
flags = 'p'; // Command needs number of parameters defined by plural value switch (cs->value) {
} else { case 0x200E: case 0x200F: // Implicit BIDI controls
flags = '0'; // Command needs no parameters case 0x202A: case 0x202B: case 0x202C: case 0x202D: case 0x202E: // Explicit BIDI controls
} case 0xA0: // Non breaking space
case '\n': // Newlines may be added too
case '{': // This special
/* This command may be in the translation when it is not in base */
flags = 'i';
break;
default:
if (cs->proc == EmitGender) {
flags = 'g'; // Command needs number of parameters defined by number of genders
} else if (cs->proc == EmitPlural) {
flags = 'p'; // Command needs number of parameters defined by plural value
} else {
flags = '0'; // Command needs no parameters
}
}
printf("%i\t%c\t\"%s\"\t\"%s\"\n", cs->consumes, flags, cs->cmd, strstr(cs->cmd, "STRING") ? "STRING" : cs->cmd);
} }
printf("%i\t%c\t\"%s\"\t\"%s\"\n", cs->consumes, flags, cs->cmd, strstr(cs->cmd, "STRING") ? "STRING" : cs->cmd); return 0;
}
return 0;
}
if (strcmp(argv[1], "-export-plurals") == 0) { case 'L':
printf("count\tdescription\n"); printf("count\tdescription\n");
for (const PluralForm *pf = _plural_forms; pf < endof(_plural_forms); pf++) { for (const PluralForm *pf = _plural_forms; pf < endof(_plural_forms); pf++) {
printf("%i\t\"%s\"\n", pf->plural_count, pf->description); printf("%i\t\"%s\"\n", pf->plural_count, pf->description);
} }
return 0; return 0;
}
if (strcmp(argv[1], "-export-pragmas") == 0) { case 'P':
printf("name\tflags\tdefault\tdescription\n"); printf("name\tflags\tdefault\tdescription\n");
for (size_t i = 0; i < lengthof(_pragmas); i++) { for (size_t i = 0; i < lengthof(_pragmas); i++) {
printf("\"%s\"\t%s\t\"%s\"\t\"%s\"\n", printf("\"%s\"\t%s\t\"%s\"\t\"%s\"\n",
_pragmas[i][0], _pragmas[i][1], _pragmas[i][2], _pragmas[i][3]); _pragmas[i][0], _pragmas[i][1], _pragmas[i][2], _pragmas[i][3]);
} }
return 0; return 0;
}
if (strcmp(argv[1], "-t") == 0 || strcmp(argv[1], "--todo") == 0) { case 't':
_show_todo |= 1; _show_todo |= 1;
argc--, argv++; break;
continue;
}
if (strcmp(argv[1], "-w") == 0 || strcmp(argv[1], "--warning") == 0) { case 'w':
_show_todo |= 2; _show_todo |= 2;
argc--, argv++; break;
continue;
}
if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0) { case 'h':
puts( puts(
"strgen - $Revision$\n" "strgen - $Revision$\n"
" -v | --version print version information and exit\n" " -v | --version print version information and exit\n"
" -t | --todo replace any untranslated strings with '<TODO>'\n" " -t | --todo replace any untranslated strings with '<TODO>'\n"
" -w | --warning print a warning for any untranslated strings\n" " -w | --warning print a warning for any untranslated strings\n"
" -h | -? | --help print this help message and exit\n" " -h | -? | --help print this help message and exit\n"
" -s | --source_dir search for english.txt in the specified directory\n" " -s | --source_dir search for english.txt in the specified directory\n"
" -d | --dest_dir put output file in the specified directory, create if needed\n" " -d | --dest_dir put output file in the specified directory, create if needed\n"
" -export-commands export all commands and exit\n" " -export-commands export all commands and exit\n"
" -export-plurals export all plural forms and exit\n" " -export-plurals export all plural forms and exit\n"
" -export-pragmas export all pragmas and exit\n" " -export-pragmas export all pragmas and exit\n"
" Run without parameters and strgen will search for english.txt and parse it,\n" " Run without parameters and strgen will search for english.txt and parse it,\n"
" creating strings.h. Passing an argument, strgen will translate that language\n" " creating strings.h. Passing an argument, strgen will translate that language\n"
" file using english.txt as a reference and output <language>.lng." " file using english.txt as a reference and output <language>.lng."
); );
return 0; return 0;
}
if (argc > 2 && (strcmp(argv[1], "-s") == 0 || strcmp(argv[1], "--source_dir") == 0)) { case 's':
src_dir = replace_pathsep(argv[2]); src_dir = replace_pathsep(mgo.opt);
argc -= 2, argv += 2; break;
continue;
}
if (argc > 2 && (strcmp(argv[1], "-d") == 0 || strcmp(argv[1], "--dest_dir") == 0)) { case 'd':
dest_dir = replace_pathsep(argv[2]); dest_dir = replace_pathsep(mgo.opt);
argc -= 2, argv += 2; break;
continue;
}
fprintf(stderr, "Invalid arguments\n"); case -2:
return 0; fprintf(stderr, "Invalid arguments\n");
return 0;
}
} }
if (dest_dir == NULL) dest_dir = src_dir; // if dest_dir is not specified, it equals src_dir if (dest_dir == NULL) dest_dir = src_dir; // if dest_dir is not specified, it equals src_dir
@ -1303,7 +1313,7 @@ int CDECL main(int argc, char *argv[])
* strgen generates strings.h to the destination directory. If it is supplied * strgen generates strings.h to the destination directory. If it is supplied
* with a (free) parameter the program will translate that language to destination * with a (free) parameter the program will translate that language to destination
* directory. As input english.txt is parsed from the source directory */ * directory. As input english.txt is parsed from the source directory */
if (argc == 1) { if (mgo.numleft == 0) {
mkpath(pathbuf, lengthof(pathbuf), src_dir, "english.txt"); mkpath(pathbuf, lengthof(pathbuf), src_dir, "english.txt");
/* parse master file */ /* parse master file */
@ -1316,7 +1326,7 @@ int CDECL main(int argc, char *argv[])
ottd_mkdir(dest_dir); ottd_mkdir(dest_dir);
mkpath(pathbuf, lengthof(pathbuf), dest_dir, "strings.h"); mkpath(pathbuf, lengthof(pathbuf), dest_dir, "strings.h");
WriteStringsH(pathbuf); WriteStringsH(pathbuf);
} else if (argc == 2) { } else if (mgo.numleft == 1) {
char *r; char *r;
mkpath(pathbuf, lengthof(pathbuf), src_dir, "english.txt"); mkpath(pathbuf, lengthof(pathbuf), src_dir, "english.txt");
@ -1325,12 +1335,12 @@ int CDECL main(int argc, char *argv[])
_masterlang = false; _masterlang = false;
ParseFile(pathbuf, true); ParseFile(pathbuf, true);
MakeHashOfStrings(); MakeHashOfStrings();
ParseFile(replace_pathsep(argv[1]), false); // target file ParseFile(replace_pathsep(mgo.argv[0]), false); // target file
if (_errors) return 1; if (_errors) return 1;
/* get the targetfile, strip any directories and append to destination path */ /* get the targetfile, strip any directories and append to destination path */
r = strrchr(argv[1], PATHSEPCHAR); r = strrchr(mgo.argv[0], PATHSEPCHAR);
mkpath(pathbuf, lengthof(pathbuf), dest_dir, (r != NULL) ? &r[1] : argv[1]); mkpath(pathbuf, lengthof(pathbuf), dest_dir, (r != NULL) ? &r[1] : mgo.argv[0]);
/* rename the .txt (input-extension) to .lng */ /* rename the .txt (input-extension) to .lng */
r = strrchr(pathbuf, '.'); r = strrchr(pathbuf, '.');