Skip to content

Commit

Permalink
build: remove dependency on icu io library
Browse files Browse the repository at this point in the history
The library is only used in a single build-time tool where it can be
easily substituted by regular libc I/O functions.

PR-URL: #13656
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Refael Ackermann <[email protected]>
  • Loading branch information
bnoordhuis authored and addaleax committed Jul 11, 2017
1 parent f8a8ea2 commit 2ab8601
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 84 deletions.
1 change: 0 additions & 1 deletion configure
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,6 @@ def configure_intl(o):
'stubdata': 'stubdata',
'common': 'common',
'i18n': 'i18n',
'io': 'io',
'tools': 'tools/toolutil',
'genccode': 'tools/genccode',
'genrb': 'tools/genrb',
Expand Down
3 changes: 0 additions & 3 deletions tools/icu/icu-generic.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,6 @@
'<@(icu_src_tools)',
'<@(icu_src_common)',
'<@(icu_src_i18n)',
'<@(icu_src_io)',
'<@(icu_src_stubdata)',
],
'sources!': [
Expand All @@ -492,7 +491,6 @@
'include_dirs': [
'<(icu_path)/source/common',
'<(icu_path)/source/i18n',
'<(icu_path)/source/io',
'<(icu_path)/source/tools/toolutil',
],
'defines': [
Expand All @@ -512,7 +510,6 @@
'include_dirs': [
'<(icu_path)/source/common',
'<(icu_path)/source/i18n',
'<(icu_path)/source/io',
'<(icu_path)/source/tools/toolutil',
],
'conditions': [
Expand Down
162 changes: 82 additions & 80 deletions tools/icu/iculslocs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ Japanese, it doesn't *claim* to have Japanese.

#include "string.h"
#include "charstr.h" // ICU internal header
#include <unicode/ustdio.h>
#include <unicode/ures.h>
#include <unicode/udata.h>
#include <unicode/putil.h>
Expand All @@ -69,36 +68,36 @@ CharString packageName;
const char* locale = RES_INDEX; // locale referring to our index

void usage() {
u_printf("Usage: %s [options]\n", PROG);
u_printf(
printf("Usage: %s [options]\n", PROG);
printf(
"This program lists and optionally regenerates the locale "
"manifests\n"
" in ICU 'res_index.res' files.\n");
u_printf(
printf(
" -i ICUDATA Set ICUDATA dir to ICUDATA.\n"
" NOTE: this must be the first option given.\n");
u_printf(" -h This Help\n");
u_printf(" -v Verbose Mode on\n");
u_printf(" -l List locales to stdout\n");
u_printf(
printf(" -h This Help\n");
printf(" -v Verbose Mode on\n");
printf(" -l List locales to stdout\n");
printf(
" if Verbose mode, then missing (unopenable)"
"locales\n"
" will be listed preceded by a '#'.\n");
u_printf(
printf(
" -b res_index.txt Write 'corrected' bundle "
"to res_index.txt\n"
" missing bundles will be "
"OMITTED\n");
u_printf(
printf(
" -T TREE Choose tree TREE\n"
" (TREE should be one of: \n"
" ROOT, brkitr, coll, curr, lang, rbnf, region, zone)\n");
// see ureslocs.h and elsewhere
u_printf(
printf(
" -N NAME Choose name NAME\n"
" (default: '%s')\n",
U_ICUDATA_NAME);
u_printf(
printf(
"\nNOTE: for best results, this tool ought to be "
"linked against\n"
"stubdata. i.e. '%s -l' SHOULD return an error with "
Expand All @@ -108,7 +107,7 @@ void usage() {

#define ASSERT_SUCCESS(status, what) \
if (U_FAILURE(*status)) { \
u_printf("%s:%d: %s: ERROR: %s %s\n", \
printf("%s:%d: %s: ERROR: %s %s\n", \
__FILE__, \
__LINE__, \
PROG, \
Expand All @@ -130,7 +129,7 @@ void calculatePackageName(UErrorCode* status) {
}
}
if (VERBOSE) {
u_printf("packageName: %s\n", packageName.data());
printf("packageName: %s\n", packageName.data());
}
}

Expand All @@ -146,41 +145,41 @@ void calculatePackageName(UErrorCode* status) {
int localeExists(const char* loc, UBool* exists) {
UErrorCode status = U_ZERO_ERROR;
if (VERBOSE > 1) {
u_printf("Trying to open %s:%s\n", packageName.data(), loc);
printf("Trying to open %s:%s\n", packageName.data(), loc);
}
LocalUResourceBundlePointer aResource(
ures_openDirect(packageName.data(), loc, &status));
*exists = FALSE;
if (U_SUCCESS(status)) {
*exists = true;
if (VERBOSE > 1) {
u_printf("%s:%s existed!\n", packageName.data(), loc);
printf("%s:%s existed!\n", packageName.data(), loc);
}
return 0;
} else if (status == U_MISSING_RESOURCE_ERROR) {
*exists = false;
if (VERBOSE > 1) {
u_printf("%s:%s did NOT exist (%s)!\n",
packageName.data(),
loc,
u_errorName(status));
printf("%s:%s did NOT exist (%s)!\n",
packageName.data(),
loc,
u_errorName(status));
}
return 0; // "good" failure
} else {
// some other failure..
u_printf("%s:%d: %s: ERROR %s opening %s:%s for test.\n",
__FILE__,
__LINE__,
u_errorName(status),
packageName.data(),
loc);
printf("%s:%d: %s: ERROR %s opening %s for test.\n",
__FILE__,
__LINE__,
u_errorName(status),
packageName.data(),
loc);
return 1; // abort
}
}

void printIndent(const LocalUFILEPointer* bf, int indent) {
void printIndent(FILE* bf, int indent) {
for (int i = 0; i < indent + 1; i++) {
u_fprintf(bf->getAlias(), " ");
fprintf(bf, " ");
}
}

Expand All @@ -191,7 +190,7 @@ void printIndent(const LocalUFILEPointer* bf, int indent) {
*/
int dumpAllButInstalledLocales(int lev,
LocalUResourceBundlePointer* bund,
LocalUFILEPointer* bf,
FILE* bf,
UErrorCode* status) {
ures_resetIterator(bund->getAlias());
LocalUResourceBundlePointer t;
Expand All @@ -200,30 +199,30 @@ int dumpAllButInstalledLocales(int lev,
ASSERT_SUCCESS(status, "while processing table");
const char* key = ures_getKey(t.getAlias());
if (VERBOSE > 1) {
u_printf("dump@%d: got key %s\n", lev, key);
printf("dump@%d: got key %s\n", lev, key);
}
if (lev == 0 && !strcmp(key, INSTALLEDLOCALES)) {
if (VERBOSE > 1) {
u_printf("dump: skipping '%s' as it must be evaluated.\n", key);
printf("dump: skipping '%s' as it must be evaluated.\n", key);
}
} else {
printIndent(bf, lev);
u_fprintf(bf->getAlias(), "%s", key);
fprintf(bf, "%s", key);
switch (ures_getType(t.getAlias())) {
case URES_STRING: {
int32_t len = 0;
const UChar* s = ures_getString(t.getAlias(), &len, status);
ASSERT_SUCCESS(status, "getting string");
u_fprintf(bf->getAlias(), ":string {\"");
u_file_write(s, len, bf->getAlias());
u_fprintf(bf->getAlias(), "\"}");
fprintf(bf, ":string {\"");
fwrite(s, len, 1, bf);
fprintf(bf, "\"}");
} break;
default: {
u_printf("ERROR: unhandled type in dumpAllButInstalledLocales().\n");
printf("ERROR: unhandled type in dumpAllButInstalledLocales().\n");
return 1;
} break;
}
u_fprintf(bf->getAlias(), "\n");
fprintf(bf, "\n");
}
}
return 0;
Expand All @@ -232,28 +231,27 @@ int dumpAllButInstalledLocales(int lev,
int list(const char* toBundle) {
UErrorCode status = U_ZERO_ERROR;

LocalUFILEPointer bf;
FILE* bf = NULL;

if (toBundle != NULL) {
if (VERBOSE) {
u_printf("writing to bundle %s\n", toBundle);
printf("writing to bundle %s\n", toBundle);
}
// we write UTF-8 with BOM only. No exceptions.
bf.adoptInstead(u_fopen(toBundle, "w", "en_US_POSIX", "UTF-8"));
if (bf.isNull()) {
u_printf("ERROR: Could not open '%s' for writing.\n", toBundle);
bf = fopen(toBundle, "wb");
if (bf == NULL) {
printf("ERROR: Could not open '%s' for writing.\n", toBundle);
return 1;
}
u_fputc(0xFEFF, bf.getAlias()); // write BOM
u_fprintf(bf.getAlias(), "// -*- Coding: utf-8; -*-\n//\n");
fprintf(bf, "\xEF\xBB\xBF"); // write UTF-8 BOM
fprintf(bf, "// -*- Coding: utf-8; -*-\n//\n");
}

// first, calculate the bundle name.
calculatePackageName(&status);
ASSERT_SUCCESS(&status, "calculating package name");

if (VERBOSE) {
u_printf("\"locale\": %s\n", locale);
printf("\"locale\": %s\n", locale);
}

LocalUResourceBundlePointer bund(
Expand All @@ -265,33 +263,34 @@ int list(const char* toBundle) {

int32_t count = ures_getSize(installedLocales.getAlias());
if (VERBOSE) {
u_printf("Locales: %d\n", count);
printf("Locales: %d\n", count);
}

if (bf.isValid()) {
if (bf != NULL) {
// write the HEADER
u_fprintf(bf.getAlias(),
"// Warning this file is automatically generated\n"
"// Updated by %s based on %s:%s.txt\n",
PROG,
packageName.data(),
locale);
u_fprintf(bf.getAlias(),
"%s:table(nofallback) {\n"
" // First, everything besides InstalledLocales:\n",
locale);
if (dumpAllButInstalledLocales(0, &bund, &bf, &status)) {
u_printf("Error dumping prolog for %s\n", toBundle);
fprintf(bf,
"// Warning this file is automatically generated\n"
"// Updated by %s based on %s:%s.txt\n",
PROG,
packageName.data(),
locale);
fprintf(bf,
"%s:table(nofallback) {\n"
" // First, everything besides InstalledLocales:\n",
locale);
if (dumpAllButInstalledLocales(0, &bund, bf, &status)) {
printf("Error dumping prolog for %s\n", toBundle);
fclose(bf);
return 1;
}
// in case an error was missed
ASSERT_SUCCESS(&status, "while writing prolog");

u_fprintf(bf.getAlias(),
" %s:table { // %d locales in input %s.res\n",
INSTALLEDLOCALES,
count,
locale);
fprintf(bf,
" %s:table { // %d locales in input %s.res\n",
INSTALLEDLOCALES,
count,
locale);
}

// OK, now list them.
Expand All @@ -305,35 +304,38 @@ int list(const char* toBundle) {

const char* key = ures_getKey(subkey.getAlias());
if (VERBOSE > 1) {
u_printf("@%d: %s\n", i, key);
printf("@%d: %s\n", i, key);
}
// now, see if the locale is installed..

UBool exists;
if (localeExists(key, &exists)) {
if (bf != NULL) fclose(bf);
return 1; // get out.
}
if (exists) {
validCount++;
u_printf("%s\n", key);
if (bf.isValid()) {
u_fprintf(bf.getAlias(), " %s {\"\"}\n", key);
printf("%s\n", key);
if (bf != NULL) {
fprintf(bf, " %s {\"\"}\n", key);
}
} else {
if (bf.isValid()) {
u_fprintf(bf.getAlias(), "// %s {\"\"}\n", key);
if (bf != NULL) {
fprintf(bf, "// %s {\"\"}\n", key);
}
if (VERBOSE) {
u_printf("#%s\n", key); // verbosity one - '' vs '#'
printf("#%s\n", key); // verbosity one - '' vs '#'
}
}
}

if (bf.isValid()) {
u_fprintf(bf.getAlias(), " } // %d/%d valid\n", validCount, count);
if (bf != NULL) {
fprintf(bf, " } // %d/%d valid\n", validCount, count);
// write the HEADER
u_fprintf(bf.getAlias(), "}\n");
fprintf(bf, "}\n");
fclose(bf);
}

return 0;
}

Expand All @@ -346,24 +348,24 @@ int main(int argc, const char* argv[]) {
VERBOSE++;
} else if (!strcmp(arg, "-i") && (argsLeft >= 1)) {
if (i != 1) {
u_printf("ERROR: -i must be the first argument given.\n");
printf("ERROR: -i must be the first argument given.\n");
usage();
return 1;
}
const char* dir = argv[++i];
u_setDataDirectory(dir);
if (VERBOSE) {
u_printf("ICUDATA is now %s\n", dir);
printf("ICUDATA is now %s\n", dir);
}
} else if (!strcmp(arg, "-T") && (argsLeft >= 1)) {
TREE = argv[++i];
if (VERBOSE) {
u_printf("TREE is now %s\n", TREE);
printf("TREE is now %s\n", TREE);
}
} else if (!strcmp(arg, "-N") && (argsLeft >= 1)) {
NAME = argv[++i];
if (VERBOSE) {
u_printf("NAME is now %s\n", NAME);
printf("NAME is now %s\n", NAME);
}
} else if (!strcmp(arg, "-?") || !strcmp(arg, "-h")) {
usage();
Expand All @@ -377,7 +379,7 @@ int main(int argc, const char* argv[]) {
return 1;
}
} else {
u_printf("Unknown or malformed option: %s\n", arg);
printf("Unknown or malformed option: %s\n", arg);
usage();
return 1;
}
Expand Down
1 change: 1 addition & 0 deletions tools/icu/shrink-icu-src.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def icu_ignore(dir, files):
elif subdir == 'source':
ign = ign + ['layout','samples','test','extra','config','layoutex','allinone','data']
ign = ign + ['runConfigureICU','install-sh','mkinstalldirs','configure']
ign = ign + ['io']
elif subdir == 'source/tools':
ign = ign + ['tzcode','ctestfw','gensprep','gennorm2','gendict','icuswap',
'genbrk','gencfu','gencolusb','genren','memcheck','makeconv','gencnval','icuinfo','gentest']
Expand Down

0 comments on commit 2ab8601

Please sign in to comment.