Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | ******************************************************************************* |
michael@0 | 3 | * |
michael@0 | 4 | * Copyright (C) 1999-2012, International Business Machines |
michael@0 | 5 | * Corporation and others. All Rights Reserved. |
michael@0 | 6 | * |
michael@0 | 7 | ******************************************************************************* |
michael@0 | 8 | * file name: icuinfo.cpp |
michael@0 | 9 | * encoding: US-ASCII |
michael@0 | 10 | * tab size: 8 (not used) |
michael@0 | 11 | * indentation:4 |
michael@0 | 12 | * |
michael@0 | 13 | * created on: 2009-2010 |
michael@0 | 14 | * created by: Steven R. Loomis |
michael@0 | 15 | * |
michael@0 | 16 | * This program shows some basic info about the current ICU. |
michael@0 | 17 | */ |
michael@0 | 18 | |
michael@0 | 19 | #include <stdio.h> |
michael@0 | 20 | #include <stdlib.h> |
michael@0 | 21 | #include "unicode/utypes.h" |
michael@0 | 22 | #include "unicode/putil.h" |
michael@0 | 23 | #include "unicode/uclean.h" |
michael@0 | 24 | #include "udbgutil.h" |
michael@0 | 25 | #include "unewdata.h" |
michael@0 | 26 | #include "cmemory.h" |
michael@0 | 27 | #include "cstring.h" |
michael@0 | 28 | #include "uoptions.h" |
michael@0 | 29 | #include "toolutil.h" |
michael@0 | 30 | #include "icuplugimp.h" |
michael@0 | 31 | #include <unicode/uloc.h> |
michael@0 | 32 | #include <unicode/ucnv.h> |
michael@0 | 33 | #include "unicode/ucal.h" |
michael@0 | 34 | #include <unicode/ulocdata.h> |
michael@0 | 35 | #include "putilimp.h" |
michael@0 | 36 | #include "unicode/uchar.h" |
michael@0 | 37 | |
michael@0 | 38 | static UOption options[]={ |
michael@0 | 39 | /*0*/ UOPTION_HELP_H, |
michael@0 | 40 | /*1*/ UOPTION_HELP_QUESTION_MARK, |
michael@0 | 41 | /*2*/ UOPTION_ICUDATADIR, |
michael@0 | 42 | /*3*/ UOPTION_VERBOSE, |
michael@0 | 43 | /*4*/ UOPTION_DEF("list-plugins", 'L', UOPT_NO_ARG), |
michael@0 | 44 | /*5*/ UOPTION_DEF("milisecond-time", 'm', UOPT_NO_ARG), |
michael@0 | 45 | /*6*/ UOPTION_DEF("cleanup", 'K', UOPT_NO_ARG), |
michael@0 | 46 | /*7*/ UOPTION_DEF("xml", 'x', UOPT_REQUIRES_ARG), |
michael@0 | 47 | }; |
michael@0 | 48 | |
michael@0 | 49 | static UErrorCode initStatus = U_ZERO_ERROR; |
michael@0 | 50 | static UBool icuInitted = FALSE; |
michael@0 | 51 | |
michael@0 | 52 | static void do_init() { |
michael@0 | 53 | if(!icuInitted) { |
michael@0 | 54 | u_init(&initStatus); |
michael@0 | 55 | icuInitted = TRUE; |
michael@0 | 56 | } |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | |
michael@0 | 60 | void cmd_millis() |
michael@0 | 61 | { |
michael@0 | 62 | printf("Milliseconds since Epoch: %.0f\n", uprv_getUTCtime()); |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | void cmd_version(UBool /* noLoad */, UErrorCode &errorCode) |
michael@0 | 66 | { |
michael@0 | 67 | |
michael@0 | 68 | do_init(); |
michael@0 | 69 | |
michael@0 | 70 | udbg_writeIcuInfo(stdout); /* print the XML format */ |
michael@0 | 71 | |
michael@0 | 72 | union { |
michael@0 | 73 | uint8_t byte; |
michael@0 | 74 | uint16_t word; |
michael@0 | 75 | } u; |
michael@0 | 76 | u.word=0x0100; |
michael@0 | 77 | if(U_IS_BIG_ENDIAN==u.byte) { |
michael@0 | 78 | //printf("U_IS_BIG_ENDIAN: %d\n", U_IS_BIG_ENDIAN); |
michael@0 | 79 | } else { |
michael@0 | 80 | fprintf(stderr, " error: U_IS_BIG_ENDIAN=%d != %d=actual 'is big endian'\n", |
michael@0 | 81 | U_IS_BIG_ENDIAN, u.byte); |
michael@0 | 82 | errorCode=U_INTERNAL_PROGRAM_ERROR; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | if(U_SIZEOF_WCHAR_T==sizeof(wchar_t)) { |
michael@0 | 86 | //printf("U_SIZEOF_WCHAR_T: %d\n", U_SIZEOF_WCHAR_T); |
michael@0 | 87 | } else { |
michael@0 | 88 | fprintf(stderr, " error: U_SIZEOF_WCHAR_T=%d != %d=sizeof(wchar_t)\n", |
michael@0 | 89 | U_SIZEOF_WCHAR_T, (int)sizeof(wchar_t)); |
michael@0 | 90 | errorCode=U_INTERNAL_PROGRAM_ERROR; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | int charsetFamily; |
michael@0 | 94 | if('A'==0x41) { |
michael@0 | 95 | charsetFamily=U_ASCII_FAMILY; |
michael@0 | 96 | } else if('A'==0xc1) { |
michael@0 | 97 | charsetFamily=U_EBCDIC_FAMILY; |
michael@0 | 98 | } else { |
michael@0 | 99 | charsetFamily=-1; // unknown |
michael@0 | 100 | } |
michael@0 | 101 | if(U_CHARSET_FAMILY==charsetFamily) { |
michael@0 | 102 | //printf("U_CHARSET_FAMILY: %d\n", U_CHARSET_FAMILY); |
michael@0 | 103 | } else { |
michael@0 | 104 | fprintf(stderr, " error: U_CHARSET_FAMILY=%d != %d=actual charset family\n", |
michael@0 | 105 | U_CHARSET_FAMILY, charsetFamily); |
michael@0 | 106 | errorCode=U_INTERNAL_PROGRAM_ERROR; |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | printf("\n\nICU Initialization returned: %s\n", u_errorName(initStatus)); |
michael@0 | 110 | |
michael@0 | 111 | |
michael@0 | 112 | #if U_ENABLE_DYLOAD |
michael@0 | 113 | const char *pluginFile = uplug_getPluginFile(); |
michael@0 | 114 | printf("Plugin file is: %s\n", (pluginFile&&*pluginFile)?pluginFile:"(not set. try setting ICU_PLUGINS to a directory.)"); |
michael@0 | 115 | #else |
michael@0 | 116 | fprintf(stderr, "Dynamic Loading: is disabled. No plugins will be loaded at start-up.\n"); |
michael@0 | 117 | #endif |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | void cmd_cleanup() |
michael@0 | 121 | { |
michael@0 | 122 | u_cleanup(); |
michael@0 | 123 | fprintf(stdout, "ICU u_cleanup() called.\n"); |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | |
michael@0 | 127 | void cmd_listplugins() { |
michael@0 | 128 | int32_t i; |
michael@0 | 129 | UPlugData *plug; |
michael@0 | 130 | |
michael@0 | 131 | do_init(); |
michael@0 | 132 | printf("ICU Initialized: u_init() returned %s\n", u_errorName(initStatus)); |
michael@0 | 133 | |
michael@0 | 134 | printf("Plugins: \n"); |
michael@0 | 135 | printf( "# %6s %s \n", |
michael@0 | 136 | "Level", |
michael@0 | 137 | "Name" ); |
michael@0 | 138 | printf( " %10s:%-10s\n", |
michael@0 | 139 | "Library", |
michael@0 | 140 | "Symbol" |
michael@0 | 141 | ); |
michael@0 | 142 | |
michael@0 | 143 | |
michael@0 | 144 | printf( " config| (configuration string)\n"); |
michael@0 | 145 | printf( " >>> Error | Explanation \n"); |
michael@0 | 146 | printf( "-----------------------------------\n"); |
michael@0 | 147 | |
michael@0 | 148 | for(i=0;(plug=uplug_getPlugInternal(i))!=NULL;i++) { |
michael@0 | 149 | UErrorCode libStatus = U_ZERO_ERROR; |
michael@0 | 150 | const char *name = uplug_getPlugName(plug); |
michael@0 | 151 | const char *sym = uplug_getSymbolName(plug); |
michael@0 | 152 | const char *lib = uplug_getLibraryName(plug, &libStatus); |
michael@0 | 153 | const char *config = uplug_getConfiguration(plug); |
michael@0 | 154 | UErrorCode loadStatus = uplug_getPlugLoadStatus(plug); |
michael@0 | 155 | const char *message = NULL; |
michael@0 | 156 | |
michael@0 | 157 | printf("\n#%d %-6s %s \n", |
michael@0 | 158 | i+1, |
michael@0 | 159 | udbg_enumName(UDBG_UPlugLevel,(int32_t)uplug_getPlugLevel(plug)), |
michael@0 | 160 | name!=NULL?(*name?name:"this plugin did not call uplug_setPlugName()"):"(null)" |
michael@0 | 161 | ); |
michael@0 | 162 | printf(" plugin| %10s:%-10s\n", |
michael@0 | 163 | (U_SUCCESS(libStatus)?(lib!=NULL?lib:"(null)"):u_errorName(libStatus)), |
michael@0 | 164 | sym!=NULL?sym:"(null)" |
michael@0 | 165 | ); |
michael@0 | 166 | |
michael@0 | 167 | if(config!=NULL&&*config) { |
michael@0 | 168 | printf(" config| %s\n", config); |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | switch(loadStatus) { |
michael@0 | 172 | case U_PLUGIN_CHANGED_LEVEL_WARNING: |
michael@0 | 173 | message = "Note: This plugin changed the system level (by allocating memory or calling something which does). Later plugins may not load."; |
michael@0 | 174 | break; |
michael@0 | 175 | |
michael@0 | 176 | case U_PLUGIN_DIDNT_SET_LEVEL: |
michael@0 | 177 | message = "Error: This plugin did not call uplug_setPlugLevel during QUERY."; |
michael@0 | 178 | break; |
michael@0 | 179 | |
michael@0 | 180 | case U_PLUGIN_TOO_HIGH: |
michael@0 | 181 | message = "Error: This plugin couldn't load because the system level was too high. Try loading this plugin earlier."; |
michael@0 | 182 | break; |
michael@0 | 183 | |
michael@0 | 184 | case U_ZERO_ERROR: |
michael@0 | 185 | message = NULL; /* no message */ |
michael@0 | 186 | break; |
michael@0 | 187 | default: |
michael@0 | 188 | if(U_FAILURE(loadStatus)) { |
michael@0 | 189 | message = "error loading:"; |
michael@0 | 190 | } else { |
michael@0 | 191 | message = "warning during load:"; |
michael@0 | 192 | } |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | if(message!=NULL) { |
michael@0 | 196 | printf("\\\\\\ status| %s\n" |
michael@0 | 197 | "/// %s\n", u_errorName(loadStatus), message); |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | } |
michael@0 | 201 | if(i==0) { |
michael@0 | 202 | printf("No plugins loaded.\n"); |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | |
michael@0 | 208 | |
michael@0 | 209 | extern int |
michael@0 | 210 | main(int argc, char* argv[]) { |
michael@0 | 211 | UErrorCode errorCode = U_ZERO_ERROR; |
michael@0 | 212 | UBool didSomething = FALSE; |
michael@0 | 213 | |
michael@0 | 214 | /* preset then read command line options */ |
michael@0 | 215 | argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options); |
michael@0 | 216 | |
michael@0 | 217 | /* error handling, printing usage message */ |
michael@0 | 218 | if(argc<0) { |
michael@0 | 219 | fprintf(stderr, |
michael@0 | 220 | "error in command line argument \"%s\"\n", |
michael@0 | 221 | argv[-argc]); |
michael@0 | 222 | } |
michael@0 | 223 | if( options[0].doesOccur || options[1].doesOccur) { |
michael@0 | 224 | fprintf(stderr, "%s: Output information about the current ICU\n", argv[0]); |
michael@0 | 225 | fprintf(stderr, "Options:\n" |
michael@0 | 226 | " -h or --help - Print this help message.\n" |
michael@0 | 227 | " -m or --millisecond-time - Print the current UTC time in milliseconds.\n" |
michael@0 | 228 | " -d <dir> or --icudatadir <dir> - Set the ICU Data Directory\n" |
michael@0 | 229 | " -v - Print version and configuration information about ICU\n" |
michael@0 | 230 | " -L or --list-plugins - List and diagnose issues with ICU Plugins\n" |
michael@0 | 231 | " -K or --cleanup - Call u_cleanup() before exitting (will attempt to unload plugins)\n" |
michael@0 | 232 | "\n" |
michael@0 | 233 | "If no arguments are given, the tool will print ICU version and configuration information.\n" |
michael@0 | 234 | ); |
michael@0 | 235 | fprintf(stderr, "International Components for Unicode %s\n%s\n", U_ICU_VERSION, U_COPYRIGHT_STRING ); |
michael@0 | 236 | return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR; |
michael@0 | 237 | } |
michael@0 | 238 | |
michael@0 | 239 | if(options[2].doesOccur) { |
michael@0 | 240 | u_setDataDirectory(options[2].value); |
michael@0 | 241 | } |
michael@0 | 242 | |
michael@0 | 243 | if(options[5].doesOccur) { |
michael@0 | 244 | cmd_millis(); |
michael@0 | 245 | didSomething=TRUE; |
michael@0 | 246 | } |
michael@0 | 247 | if(options[4].doesOccur) { |
michael@0 | 248 | cmd_listplugins(); |
michael@0 | 249 | didSomething = TRUE; |
michael@0 | 250 | } |
michael@0 | 251 | |
michael@0 | 252 | if(options[3].doesOccur) { |
michael@0 | 253 | cmd_version(FALSE, errorCode); |
michael@0 | 254 | didSomething = TRUE; |
michael@0 | 255 | } |
michael@0 | 256 | |
michael@0 | 257 | if(options[7].doesOccur) { /* 2nd part of version: cleanup */ |
michael@0 | 258 | FILE *out = fopen(options[7].value, "w"); |
michael@0 | 259 | if(out==NULL) { |
michael@0 | 260 | fprintf(stderr,"ERR: can't write to XML file %s\n", options[7].value); |
michael@0 | 261 | return 1; |
michael@0 | 262 | } |
michael@0 | 263 | /* todo: API for writing DTD? */ |
michael@0 | 264 | fprintf(out, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"); |
michael@0 | 265 | udbg_writeIcuInfo(out); |
michael@0 | 266 | fclose(out); |
michael@0 | 267 | didSomething = TRUE; |
michael@0 | 268 | } |
michael@0 | 269 | |
michael@0 | 270 | if(options[6].doesOccur) { /* 2nd part of version: cleanup */ |
michael@0 | 271 | cmd_cleanup(); |
michael@0 | 272 | didSomething = TRUE; |
michael@0 | 273 | } |
michael@0 | 274 | |
michael@0 | 275 | if(!didSomething) { |
michael@0 | 276 | cmd_version(FALSE, errorCode); /* at least print the version # */ |
michael@0 | 277 | } |
michael@0 | 278 | |
michael@0 | 279 | return U_FAILURE(errorCode); |
michael@0 | 280 | } |