intl/icu/source/tools/icuinfo/icuinfo.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/tools/icuinfo/icuinfo.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,280 @@
     1.4 +/*
     1.5 +*******************************************************************************
     1.6 +*
     1.7 +*   Copyright (C) 1999-2012, International Business Machines
     1.8 +*   Corporation and others.  All Rights Reserved.
     1.9 +*
    1.10 +*******************************************************************************
    1.11 +*   file name:  icuinfo.cpp
    1.12 +*   encoding:   US-ASCII
    1.13 +*   tab size:   8 (not used)
    1.14 +*   indentation:4
    1.15 +*
    1.16 +*   created on: 2009-2010
    1.17 +*   created by: Steven R. Loomis
    1.18 +*
    1.19 +*   This program shows some basic info about the current ICU.
    1.20 +*/
    1.21 +
    1.22 +#include <stdio.h>
    1.23 +#include <stdlib.h>
    1.24 +#include "unicode/utypes.h"
    1.25 +#include "unicode/putil.h"
    1.26 +#include "unicode/uclean.h"
    1.27 +#include "udbgutil.h"
    1.28 +#include "unewdata.h"
    1.29 +#include "cmemory.h"
    1.30 +#include "cstring.h"
    1.31 +#include "uoptions.h"
    1.32 +#include "toolutil.h"
    1.33 +#include "icuplugimp.h"
    1.34 +#include <unicode/uloc.h>
    1.35 +#include <unicode/ucnv.h>
    1.36 +#include "unicode/ucal.h"
    1.37 +#include <unicode/ulocdata.h>
    1.38 +#include "putilimp.h"
    1.39 +#include "unicode/uchar.h"
    1.40 +
    1.41 +static UOption options[]={
    1.42 +  /*0*/ UOPTION_HELP_H,
    1.43 +  /*1*/ UOPTION_HELP_QUESTION_MARK,
    1.44 +  /*2*/ UOPTION_ICUDATADIR,
    1.45 +  /*3*/ UOPTION_VERBOSE,
    1.46 +  /*4*/ UOPTION_DEF("list-plugins", 'L', UOPT_NO_ARG),
    1.47 +  /*5*/ UOPTION_DEF("milisecond-time", 'm', UOPT_NO_ARG),
    1.48 +  /*6*/ UOPTION_DEF("cleanup", 'K', UOPT_NO_ARG),
    1.49 +  /*7*/ UOPTION_DEF("xml", 'x', UOPT_REQUIRES_ARG),
    1.50 +};
    1.51 +
    1.52 +static UErrorCode initStatus = U_ZERO_ERROR;
    1.53 +static UBool icuInitted = FALSE;
    1.54 +
    1.55 +static void do_init() {
    1.56 +    if(!icuInitted) {
    1.57 +      u_init(&initStatus);
    1.58 +      icuInitted = TRUE;
    1.59 +    }
    1.60 +}
    1.61 +
    1.62 +
    1.63 +void cmd_millis()
    1.64 +{
    1.65 +  printf("Milliseconds since Epoch: %.0f\n", uprv_getUTCtime());
    1.66 +}
    1.67 +
    1.68 +void cmd_version(UBool /* noLoad */, UErrorCode &errorCode)
    1.69 +{
    1.70 +
    1.71 +    do_init();
    1.72 +
    1.73 +    udbg_writeIcuInfo(stdout); /* print the XML format */
    1.74 +    
    1.75 +    union {
    1.76 +        uint8_t byte;
    1.77 +        uint16_t word;
    1.78 +    } u;
    1.79 +    u.word=0x0100;
    1.80 +    if(U_IS_BIG_ENDIAN==u.byte) {
    1.81 +      //printf("U_IS_BIG_ENDIAN: %d\n", U_IS_BIG_ENDIAN);
    1.82 +    } else {
    1.83 +        fprintf(stderr, "  error: U_IS_BIG_ENDIAN=%d != %d=actual 'is big endian'\n",
    1.84 +                U_IS_BIG_ENDIAN, u.byte);
    1.85 +        errorCode=U_INTERNAL_PROGRAM_ERROR;
    1.86 +    }
    1.87 +
    1.88 +    if(U_SIZEOF_WCHAR_T==sizeof(wchar_t)) {
    1.89 +      //printf("U_SIZEOF_WCHAR_T: %d\n", U_SIZEOF_WCHAR_T);
    1.90 +    } else {
    1.91 +        fprintf(stderr, "  error: U_SIZEOF_WCHAR_T=%d != %d=sizeof(wchar_t)\n",
    1.92 +                U_SIZEOF_WCHAR_T, (int)sizeof(wchar_t));
    1.93 +        errorCode=U_INTERNAL_PROGRAM_ERROR;
    1.94 +    }
    1.95 +
    1.96 +    int charsetFamily;
    1.97 +    if('A'==0x41) {
    1.98 +        charsetFamily=U_ASCII_FAMILY;
    1.99 +    } else if('A'==0xc1) {
   1.100 +        charsetFamily=U_EBCDIC_FAMILY;
   1.101 +    } else {
   1.102 +        charsetFamily=-1;  // unknown
   1.103 +    }
   1.104 +    if(U_CHARSET_FAMILY==charsetFamily) {
   1.105 +      //printf("U_CHARSET_FAMILY: %d\n", U_CHARSET_FAMILY);
   1.106 +    } else {
   1.107 +        fprintf(stderr, "  error: U_CHARSET_FAMILY=%d != %d=actual charset family\n",
   1.108 +                U_CHARSET_FAMILY, charsetFamily);
   1.109 +        errorCode=U_INTERNAL_PROGRAM_ERROR;
   1.110 +    }
   1.111 +
   1.112 +    printf("\n\nICU Initialization returned: %s\n", u_errorName(initStatus));
   1.113 +    
   1.114 +    
   1.115 +#if U_ENABLE_DYLOAD
   1.116 +    const char *pluginFile = uplug_getPluginFile();
   1.117 +    printf("Plugin file is: %s\n", (pluginFile&&*pluginFile)?pluginFile:"(not set. try setting ICU_PLUGINS to a directory.)");
   1.118 +#else
   1.119 +    fprintf(stderr, "Dynamic Loading: is disabled. No plugins will be loaded at start-up.\n");
   1.120 +#endif
   1.121 +}
   1.122 +
   1.123 +void cmd_cleanup()
   1.124 +{
   1.125 +    u_cleanup();
   1.126 +    fprintf(stdout, "ICU u_cleanup() called.\n");
   1.127 +}
   1.128 +
   1.129 +
   1.130 +void cmd_listplugins() {
   1.131 +    int32_t i;
   1.132 +    UPlugData *plug;
   1.133 +
   1.134 +    do_init();
   1.135 +    printf("ICU Initialized: u_init() returned %s\n", u_errorName(initStatus));
   1.136 +    
   1.137 +    printf("Plugins: \n");
   1.138 +    printf(    "# %6s   %s \n",
   1.139 +                       "Level",
   1.140 +                       "Name" );
   1.141 +    printf(    "    %10s:%-10s\n",
   1.142 +                       "Library",
   1.143 +                       "Symbol"
   1.144 +            );
   1.145 +
   1.146 +                       
   1.147 +    printf(    "       config| (configuration string)\n");
   1.148 +    printf(    " >>>   Error          | Explanation \n");
   1.149 +    printf(    "-----------------------------------\n");
   1.150 +        
   1.151 +    for(i=0;(plug=uplug_getPlugInternal(i))!=NULL;i++) {
   1.152 +        UErrorCode libStatus = U_ZERO_ERROR;
   1.153 +        const char *name = uplug_getPlugName(plug);
   1.154 +        const char *sym = uplug_getSymbolName(plug);
   1.155 +        const char *lib = uplug_getLibraryName(plug, &libStatus);
   1.156 +        const char *config = uplug_getConfiguration(plug);
   1.157 +        UErrorCode loadStatus = uplug_getPlugLoadStatus(plug);
   1.158 +        const char *message = NULL;
   1.159 +        
   1.160 +        printf("\n#%d  %-6s %s \n",
   1.161 +            i+1,
   1.162 +            udbg_enumName(UDBG_UPlugLevel,(int32_t)uplug_getPlugLevel(plug)),
   1.163 +            name!=NULL?(*name?name:"this plugin did not call uplug_setPlugName()"):"(null)"
   1.164 +        );
   1.165 +        printf("    plugin| %10s:%-10s\n",
   1.166 +            (U_SUCCESS(libStatus)?(lib!=NULL?lib:"(null)"):u_errorName(libStatus)),
   1.167 +            sym!=NULL?sym:"(null)"
   1.168 +        );
   1.169 +        
   1.170 +        if(config!=NULL&&*config) {
   1.171 +            printf("    config| %s\n", config);
   1.172 +        }
   1.173 +        
   1.174 +        switch(loadStatus) {
   1.175 +            case U_PLUGIN_CHANGED_LEVEL_WARNING:
   1.176 +                message = "Note: This plugin changed the system level (by allocating memory or calling something which does). Later plugins may not load.";
   1.177 +                break;
   1.178 +                
   1.179 +            case U_PLUGIN_DIDNT_SET_LEVEL:
   1.180 +                message = "Error: This plugin did not call uplug_setPlugLevel during QUERY.";
   1.181 +                break;
   1.182 +            
   1.183 +            case U_PLUGIN_TOO_HIGH:
   1.184 +                message = "Error: This plugin couldn't load because the system level was too high. Try loading this plugin earlier.";
   1.185 +                break;
   1.186 +                
   1.187 +            case U_ZERO_ERROR: 
   1.188 +                message = NULL; /* no message */
   1.189 +                break;
   1.190 +            default:
   1.191 +                if(U_FAILURE(loadStatus)) {
   1.192 +                    message = "error loading:";
   1.193 +                } else {
   1.194 +                    message = "warning during load:";
   1.195 +                }            
   1.196 +        }
   1.197 +        
   1.198 +        if(message!=NULL) {
   1.199 +            printf("\\\\\\ status| %s\n"
   1.200 +                   "/// %s\n", u_errorName(loadStatus), message);
   1.201 +        }
   1.202 +        
   1.203 +    }
   1.204 +	if(i==0) {
   1.205 +		printf("No plugins loaded.\n");
   1.206 +	}
   1.207 +
   1.208 +}
   1.209 +
   1.210 +
   1.211 +
   1.212 +extern int
   1.213 +main(int argc, char* argv[]) {
   1.214 +    UErrorCode errorCode = U_ZERO_ERROR;
   1.215 +    UBool didSomething = FALSE;
   1.216 +    
   1.217 +    /* preset then read command line options */
   1.218 +    argc=u_parseArgs(argc, argv, sizeof(options)/sizeof(options[0]), options);
   1.219 +
   1.220 +    /* error handling, printing usage message */
   1.221 +    if(argc<0) {
   1.222 +        fprintf(stderr,
   1.223 +            "error in command line argument \"%s\"\n",
   1.224 +            argv[-argc]);
   1.225 +    }
   1.226 +    if( options[0].doesOccur || options[1].doesOccur) {
   1.227 +      fprintf(stderr, "%s: Output information about the current ICU\n", argv[0]);
   1.228 +      fprintf(stderr, "Options:\n"
   1.229 +              " -h     or  --help                 - Print this help message.\n"
   1.230 +              " -m     or  --millisecond-time     - Print the current UTC time in milliseconds.\n"
   1.231 +              " -d <dir>   or  --icudatadir <dir> - Set the ICU Data Directory\n"
   1.232 +              " -v                                - Print version and configuration information about ICU\n"
   1.233 +              " -L         or  --list-plugins     - List and diagnose issues with ICU Plugins\n"
   1.234 +              " -K         or  --cleanup          - Call u_cleanup() before exitting (will attempt to unload plugins)\n"
   1.235 +              "\n"
   1.236 +              "If no arguments are given, the tool will print ICU version and configuration information.\n"
   1.237 +              );
   1.238 +      fprintf(stderr, "International Components for Unicode %s\n%s\n", U_ICU_VERSION, U_COPYRIGHT_STRING );
   1.239 +      return argc<0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR;
   1.240 +    }
   1.241 +    
   1.242 +    if(options[2].doesOccur) {
   1.243 +      u_setDataDirectory(options[2].value);
   1.244 +    }
   1.245 +
   1.246 +    if(options[5].doesOccur) {
   1.247 +      cmd_millis();
   1.248 +      didSomething=TRUE;
   1.249 +    } 
   1.250 +    if(options[4].doesOccur) {
   1.251 +      cmd_listplugins();
   1.252 +      didSomething = TRUE;
   1.253 +    }
   1.254 +
   1.255 +    if(options[3].doesOccur) {
   1.256 +      cmd_version(FALSE, errorCode);
   1.257 +      didSomething = TRUE;
   1.258 +    }
   1.259 +
   1.260 +    if(options[7].doesOccur) {  /* 2nd part of version: cleanup */
   1.261 +      FILE *out = fopen(options[7].value, "w");
   1.262 +      if(out==NULL) {
   1.263 +        fprintf(stderr,"ERR: can't write to XML file %s\n", options[7].value);
   1.264 +        return 1;
   1.265 +      }
   1.266 +      /* todo: API for writing DTD? */
   1.267 +      fprintf(out, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
   1.268 +      udbg_writeIcuInfo(out);
   1.269 +      fclose(out);
   1.270 +      didSomething = TRUE;
   1.271 +    }
   1.272 +
   1.273 +    if(options[6].doesOccur) {  /* 2nd part of version: cleanup */
   1.274 +      cmd_cleanup();
   1.275 +      didSomething = TRUE;
   1.276 +    }
   1.277 +
   1.278 +    if(!didSomething) {
   1.279 +      cmd_version(FALSE, errorCode);  /* at least print the version # */
   1.280 +    }
   1.281 +
   1.282 +    return U_FAILURE(errorCode);
   1.283 +}

mercurial