// License & terms of use: http://www.unicode.org/copyright.html
#include <iostream>
+#include <vector>
#include "toolutil.h"
#include "uoptions.h"
#include "cmemory.h"
UBool haveCopyright = TRUE;
UCPTrieType trieType = UCPTRIE_TYPE_SMALL;
+const char* destdir = "";
void handleError(ErrorCode& status, const char* context) {
if (status.isFailure()) {
}
void dumpBinaryProperty(UProperty uproperty, FILE* f) {
- IcuToolErrorCode status("icuwriteuprops: dumpBinaryProperty");
+ IcuToolErrorCode status("icuexportdata: dumpBinaryProperty");
const char* fullPropName = u_getPropertyName(uproperty, U_LONG_PROPERTY_NAME);
const char* shortPropName = u_getPropertyName(uproperty, U_SHORT_PROPERTY_NAME);
const USet* uset = u_getBinaryPropertySet(uproperty, status);
fputs("[[binary_property]]\n", f);
fprintf(f, "long_name = \"%s\"\n", fullPropName);
- fprintf(f, "short_name = \"%s\"\n", shortPropName);
+ if (shortPropName) fprintf(f, "short_name = \"%s\"\n", shortPropName);
usrc_writeUnicodeSet(f, uset, UPRV_TARGET_SYNTAX_TOML);
}
void dumpEnumeratedProperty(UProperty uproperty, FILE* f) {
- IcuToolErrorCode status("icuwriteuprops: dumpEnumeratedProperty");
+ IcuToolErrorCode status("icuexportdata: dumpEnumeratedProperty");
const char* fullPropName = u_getPropertyName(uproperty, U_LONG_PROPERTY_NAME);
const char* shortPropName = u_getPropertyName(uproperty, U_SHORT_PROPERTY_NAME);
const UCPMap* umap = u_getIntPropertyMap(uproperty, status);
fputs("[[enum_property]]\n", f);
fprintf(f, "long_name = \"%s\"\n", fullPropName);
- fprintf(f, "short_name = \"%s\"\n", shortPropName);
+ if (shortPropName) fprintf(f, "short_name = \"%s\"\n", shortPropName);
usrc_writeUCPMap(f, umap, uproperty, UPRV_TARGET_SYNTAX_TOML);
fputs("\n", f);
usrc_writeUCPTrie(f, shortPropName, utrie.getAlias(), UPRV_TARGET_SYNTAX_TOML);
}
+FILE* prepareOutputFile(const char* basename) {
+ IcuToolErrorCode status("icuexportdata");
+ CharString outFileName;
+ if (destdir != nullptr && *destdir != 0) {
+ outFileName.append(destdir, status).ensureEndsWithFileSeparator(status);
+ }
+ outFileName.append(basename, status);
+ outFileName.append(".toml", status);
+ handleError(status, basename);
+
+ FILE* f = fopen(outFileName.data(), "w");
+ if (f == nullptr) {
+ std::cerr << "Unable to open file: " << outFileName.data() << std::endl;
+ exit(U_FILE_ACCESS_ERROR);
+ }
+ if (!QUIET) {
+ std::cout << "Writing to: " << outFileName.data() << std::endl;
+ }
+
+ if (haveCopyright) {
+ usrc_writeCopyrightHeader(f, "#", 2021);
+ }
+ usrc_writeFileNameGeneratedBy(f, "#", basename, "icuexportdata.cpp");
+
+ return f;
+}
+
enum {
OPT_HELP_H,
OPT_HELP_QUESTION_MARK,
- OPT_COPYRIGHT,
+ OPT_MODE,
OPT_TRIE_TYPE,
OPT_VERSION,
OPT_DESTDIR,
+ OPT_ALL,
+ OPT_INDEX,
+ OPT_COPYRIGHT,
OPT_VERBOSE,
OPT_QUIET,
OPT_COUNT
};
-#define UOPTION_TRIE_TYPE UOPTION_DEF("trie-type", 't', UOPT_REQUIRES_ARG)
+#define UOPTION_MODE UOPTION_DEF("mode", 'm', UOPT_REQUIRES_ARG)
+#define UOPTION_TRIE_TYPE UOPTION_DEF("trie-type", '\1', UOPT_REQUIRES_ARG)
+#define UOPTION_ALL UOPTION_DEF("all", '\1', UOPT_NO_ARG)
+#define UOPTION_INDEX UOPTION_DEF("index", '\1', UOPT_NO_ARG)
static UOption options[]={
UOPTION_HELP_H,
UOPTION_HELP_QUESTION_MARK,
- UOPTION_COPYRIGHT,
+ UOPTION_MODE,
UOPTION_TRIE_TYPE,
UOPTION_VERSION,
UOPTION_DESTDIR,
+ UOPTION_ALL,
+ UOPTION_INDEX,
+ UOPTION_COPYRIGHT,
UOPTION_VERBOSE,
UOPTION_QUIET,
};
int main(int argc, char* argv[]) {
-
U_MAIN_INIT_ARGS(argc, argv);
/* preset then read command line options */
argc=u_parseArgs(argc, argv, UPRV_LENGTHOF(options), options);
if(options[OPT_VERSION].doesOccur) {
- printf("icuwriteuprops version %s, ICU tool to write Unicode property .toml files\n",
+ printf("icuexportdata version %s, ICU tool to dump data files for external consumers\n",
U_ICU_DATA_VERSION);
printf("%s\n", U_COPYRIGHT_STRING);
exit(0);
argc=-1;
}
- if(argc<0 || options[OPT_HELP_H].doesOccur || options[OPT_HELP_QUESTION_MARK].doesOccur) {
+ /* get the options values */
+ haveCopyright = options[OPT_COPYRIGHT].doesOccur;
+ destdir = options[OPT_DESTDIR].value;
+ VERBOSE = options[OPT_VERBOSE].doesOccur;
+ QUIET = options[OPT_QUIET].doesOccur;
+
+ // Load list of Unicode properties
+ std::vector<const char*> propNames;
+ for (int i=1; i<argc; i++) {
+ propNames.push_back(argv[i]);
+ }
+ if (options[OPT_ALL].doesOccur) {
+ for (int i=UCHAR_BINARY_START; i<UCHAR_INT_LIMIT; i++) {
+ if (i == UCHAR_BINARY_LIMIT) {
+ i = UCHAR_INT_START;
+ }
+ UProperty uprop = static_cast<UProperty>(i);
+ const char* propName = u_getPropertyName(uprop, U_SHORT_PROPERTY_NAME);
+ if (propName == NULL) {
+ propName = u_getPropertyName(uprop, U_LONG_PROPERTY_NAME);
+ if (propName != NULL && VERBOSE) {
+ std::cerr << "Note: falling back to long name for: " << propName << std::endl;
+ }
+ }
+ if (propName != NULL) {
+ propNames.push_back(propName);
+ }
+ }
+ }
+
+ if (propNames.empty()
+ || options[OPT_HELP_H].doesOccur
+ || options[OPT_HELP_QUESTION_MARK].doesOccur
+ || !options[OPT_MODE].doesOccur) {
FILE *stdfile=argc<0 ? stderr : stdout;
fprintf(stdfile,
- "usage: %s [-options] properties...\n"
+ "usage: %s -m uprops [-options] [--all | properties...]\n"
"\tdump Unicode property data to .toml files\n"
"options:\n"
"\t-h or -? or --help this usage text\n"
"\t-V or --version show a version message\n"
- "\t-c or --copyright include a copyright notice\n"
- "\t-t or --trie-type set the trie type (small or fast, default small)\n"
+ "\t-m or --mode mode: currently only 'uprops', but more may be added\n"
+ "\t --trie-type set the trie type (small or fast, default small)\n"
"\t-d or --destdir destination directory, followed by the path\n"
+ "\t --all write out all properties known to icuexportdata\n"
+ "\t --index write an _index.toml summarizing all data exported\n"
+ "\t-c or --copyright include a copyright notice\n"
"\t-v or --verbose Turn on verbose output\n"
"\t-q or --quiet do not display warnings and progress\n",
argv[0]);
return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR;
}
- /* get the options values */
- haveCopyright = options[OPT_COPYRIGHT].doesOccur;
- const char *destdir = options[OPT_DESTDIR].value;
- VERBOSE = options[OPT_VERBOSE].doesOccur;
- QUIET = options[OPT_QUIET].doesOccur;
+ const char* mode = options[OPT_MODE].value;
+ if (uprv_strcmp(mode, "uprops") != 0) {
+ fprintf(stderr, "Invalid option for --mode (must be uprops)\n");
+ return U_ILLEGAL_ARGUMENT_ERROR;
+ }
if (options[OPT_TRIE_TYPE].doesOccur) {
if (uprv_strcmp(options[OPT_TRIE_TYPE].value, "fast") == 0) {
}
}
- for (int i=1; i<argc; i++) {
- auto propName = argv[i];
+ for (const char* propName : propNames) {
UProperty propEnum = u_getPropertyEnum(propName);
if (propEnum == UCHAR_INVALID_CODE) {
std::cerr << "Error: Invalid property alias: " << propName << std::endl;
return U_ILLEGAL_ARGUMENT_ERROR;
}
- IcuToolErrorCode status("icuwriteuprops");
- CharString outFileName;
- if (destdir != nullptr && *destdir != 0) {
- outFileName.append(destdir, status).ensureEndsWithFileSeparator(status);
- }
- outFileName.append(propName, status);
- outFileName.append(".toml", status);
- handleError(status, propName);
-
- FILE* f = fopen(outFileName.data(), "w");
- if (f == nullptr) {
- std::cerr << "Unable to open file: " << outFileName.data() << std::endl;
- return U_FILE_ACCESS_ERROR;
- }
- if (!QUIET) {
- std::cout << "Writing to: " << outFileName.data() << std::endl;
- }
+ FILE* f = prepareOutputFile(propName);
- if (haveCopyright) {
- usrc_writeCopyrightHeader(f, "#", 2021);
- }
- usrc_writeFileNameGeneratedBy(f, "#", propName, "icuwriteuprops.cpp");
+ UVersionInfo versionInfo;
+ u_getUnicodeVersion(versionInfo);
+ char uvbuf[U_MAX_VERSION_STRING_LENGTH];
+ u_versionToString(versionInfo, uvbuf);
+ fprintf(f, "icu_version = \"%s\"\nunicode_version = \"%s\"\n\n",
+ U_ICU_VERSION,
+ uvbuf);
if (propEnum < UCHAR_BINARY_LIMIT) {
dumpBinaryProperty(propEnum, f);
fclose(f);
}
+
+ if (options[OPT_INDEX].doesOccur) {
+ FILE* f = prepareOutputFile("_index");
+ fprintf(f, "index = [\n");
+ for (const char* propName : propNames) {
+ // At this point, propName is a valid property name, so it should be alphanum ASCII
+ fprintf(f, " { filename=\"%s.toml\" },\n", propName);
+ }
+ fprintf(f, "]\n");
+ fclose(f);
+ }
+
+ return 0;
}