mirror of
https://github.com/unicode-org/icu.git
synced 2025-04-20 20:19:32 +00:00
ICU-587 extended example for workshop
X-SVN-Rev: 2427
This commit is contained in:
parent
a911445853
commit
2004a9f8b7
6 changed files with 320 additions and 70 deletions
97
icu4c/source/samples/uresb/resources.dsp
Normal file
97
icu4c/source/samples/uresb/resources.dsp
Normal file
|
@ -0,0 +1,97 @@
|
|||
# Microsoft Developer Studio Project File - Name="resources" - Package Owner=<4>
|
||||
# Microsoft Developer Studio Generated Build File, Format Version 6.00
|
||||
# ** DO NOT EDIT **
|
||||
|
||||
# TARGTYPE "Win32 (x86) External Target" 0x0106
|
||||
|
||||
CFG=resources - Win32 Debug
|
||||
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
|
||||
!MESSAGE use the Export Makefile command and run
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "resources.mak".
|
||||
!MESSAGE
|
||||
!MESSAGE You can specify a configuration when running NMAKE
|
||||
!MESSAGE by defining the macro CFG on the command line. For example:
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "resources.mak" CFG="resources - Win32 Debug"
|
||||
!MESSAGE
|
||||
!MESSAGE Possible choices for configuration are:
|
||||
!MESSAGE
|
||||
!MESSAGE "resources - Win32 Release" (based on "Win32 (x86) External Target")
|
||||
!MESSAGE "resources - Win32 Debug" (based on "Win32 (x86) External Target")
|
||||
!MESSAGE
|
||||
|
||||
# Begin Project
|
||||
# PROP AllowPerConfigDependencies 0
|
||||
# PROP Scc_ProjName ""
|
||||
# PROP Scc_LocalPath ""
|
||||
|
||||
!IF "$(CFG)" == "resources - Win32 Release"
|
||||
|
||||
# PROP BASE Use_MFC
|
||||
# PROP BASE Use_Debug_Libraries 0
|
||||
# PROP BASE Output_Dir "Release"
|
||||
# PROP BASE Intermediate_Dir "Release"
|
||||
# PROP BASE Cmd_Line "NMAKE /f resources.mak"
|
||||
# PROP BASE Rebuild_Opt "/a"
|
||||
# PROP BASE Target_File "resources.exe"
|
||||
# PROP BASE Bsc_Name "resources.bsc"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC
|
||||
# PROP Use_Debug_Libraries 0
|
||||
# PROP Output_Dir "Release"
|
||||
# PROP Intermediate_Dir "Release"
|
||||
# PROP Cmd_Line "nmake /f "resources.mak""
|
||||
# PROP Rebuild_Opt "/a"
|
||||
# PROP Target_File "resources.exe"
|
||||
# PROP Bsc_Name ""
|
||||
# PROP Target_Dir ""
|
||||
|
||||
!ELSEIF "$(CFG)" == "resources - Win32 Debug"
|
||||
|
||||
# PROP BASE Use_MFC
|
||||
# PROP BASE Use_Debug_Libraries 1
|
||||
# PROP BASE Output_Dir "resources___Win32_Debug"
|
||||
# PROP BASE Intermediate_Dir "resources___Win32_Debug"
|
||||
# PROP BASE Cmd_Line "NMAKE /f resources.mak"
|
||||
# PROP BASE Rebuild_Opt "/a"
|
||||
# PROP BASE Target_File "resources.exe"
|
||||
# PROP BASE Bsc_Name "resources.bsc"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC
|
||||
# PROP Use_Debug_Libraries 1
|
||||
# PROP Output_Dir "resources___Win32_Debug"
|
||||
# PROP Intermediate_Dir "resources___Win32_Debug"
|
||||
# PROP Cmd_Line "nmake /f "resources.mak""
|
||||
# PROP Rebuild_Opt "/a"
|
||||
# PROP Target_File "resources.exe"
|
||||
# PROP Bsc_Name ""
|
||||
# PROP Target_Dir ""
|
||||
|
||||
!ENDIF
|
||||
|
||||
# Begin Target
|
||||
|
||||
# Name "resources - Win32 Release"
|
||||
# Name "resources - Win32 Debug"
|
||||
|
||||
!IF "$(CFG)" == "resources - Win32 Release"
|
||||
|
||||
!ELSEIF "$(CFG)" == "resources - Win32 Debug"
|
||||
|
||||
!ENDIF
|
||||
|
||||
# Begin Group "Source Files"
|
||||
|
||||
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
|
||||
# End Group
|
||||
# Begin Group "Header Files"
|
||||
|
||||
# PROP Default_Filter "h;hpp;hxx;hm;inl"
|
||||
# End Group
|
||||
# Begin Group "Resource Files"
|
||||
|
||||
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
|
||||
# End Group
|
||||
# End Target
|
||||
# End Project
|
20
icu4c/source/samples/uresb/resources.mak
Normal file
20
icu4c/source/samples/uresb/resources.mak
Normal file
|
@ -0,0 +1,20 @@
|
|||
TARGETS = en.res root.res sr.res
|
||||
GENRB = $(MAKEDIR)\..\..\tools\genrb\debug\genrb.exe
|
||||
GENRBOPT = -s. -d.
|
||||
|
||||
all : $(TARGETS)
|
||||
@echo All targets are up to date
|
||||
|
||||
clean :
|
||||
-erase $(TARGETS)
|
||||
|
||||
|
||||
en.res : en.txt
|
||||
$(GENRB) $(GENRBOPT) $?
|
||||
|
||||
root.res : root.txt
|
||||
$(GENRB) $(GENRBOPT) $?
|
||||
|
||||
sr.res : sr.txt
|
||||
$(GENRB) $(GENRBOPT) --encoding cp1251 $?
|
||||
|
|
@ -1,3 +1,5 @@
|
|||
// Use --encoding cp1251 for genrb
|
||||
|
||||
sr {
|
||||
errorcodes {
|
||||
"Íåìà ãðåøêå",
|
||||
|
|
|
@ -15,38 +15,61 @@
|
|||
*/
|
||||
|
||||
/******************************************************************************
|
||||
* A program uses simple resource bundle readable by udata - example for
|
||||
* This program prints out resource bundles - example for
|
||||
* ICU workshop
|
||||
* TODO: make a complete i18n layout for this program.
|
||||
******************************************************************************/
|
||||
|
||||
#include "unicode/ures.h"
|
||||
#include "unicode/ustdio.h"
|
||||
#include "unicode/uloc.h"
|
||||
#include "uoptions.h"
|
||||
#include <stdlib.h>
|
||||
#ifdef WIN32
|
||||
#include <direct.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#define URESB_DEFAULTTRUNC 40
|
||||
|
||||
static char *currdir = NULL;
|
||||
/*--locale sr_YU and --encoding cp855
|
||||
* are interesting on Win32
|
||||
*/
|
||||
|
||||
static const char *locale = NULL;
|
||||
static const char *encoding = NULL;
|
||||
static const char *resPath = NULL;
|
||||
static const int32_t indentsize = 4;
|
||||
static UFILE *outerr = NULL;
|
||||
static int32_t truncsize = URESB_DEFAULTTRUNC;
|
||||
static UBool trunc = FALSE;
|
||||
|
||||
const UChar baderror[] = { 0x0042, 0x0041, 0x0044, 0x0000 };
|
||||
|
||||
const UChar *getErrorName(int32_t errorNumber);
|
||||
const UChar *getErrorName(UErrorCode errorNumber);
|
||||
void reportError(UErrorCode *status);
|
||||
void printOutBundle(UFILE *out, UResourceBundle *resource, int32_t indent, UErrorCode *status);
|
||||
void printIndent(UFILE *out, int32_t indent);
|
||||
void printHex(UFILE *out, const int8_t *what);
|
||||
|
||||
static UOption options[]={
|
||||
UOPTION_HELP_H,
|
||||
UOPTION_HELP_QUESTION_MARK,
|
||||
{ "locale", NULL, NULL, NULL, 'l', UOPT_REQUIRES_ARG, 0 }
|
||||
{ "locale", NULL, NULL, NULL, 'l', UOPT_REQUIRES_ARG, 0 },
|
||||
UOPTION_ENCODING,
|
||||
{ "path", NULL, NULL, NULL, 'p', UOPT_OPTIONAL_ARG, 0 },
|
||||
{ "truncate", NULL, NULL, NULL, 't', UOPT_OPTIONAL_ARG, 0 }
|
||||
};
|
||||
|
||||
|
||||
extern int
|
||||
main(int argc, const char *argv[]) {
|
||||
|
||||
UResourceBundle *example = NULL;
|
||||
UFILE *out = u_finit(stdout, NULL, NULL);
|
||||
UResourceBundle *bundle = NULL;
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
UFILE *out = NULL;
|
||||
int32_t i = 0;
|
||||
#ifdef WIN32
|
||||
currdir = _getcwd(NULL, 0);
|
||||
|
@ -75,17 +98,171 @@ main(int argc, const char *argv[]) {
|
|||
locale = uloc_getDefault();
|
||||
}
|
||||
|
||||
printf("We are running under %s locale\n", locale);
|
||||
if(options[3].doesOccur) {
|
||||
encoding = options[3].value;
|
||||
} else {
|
||||
encoding = NULL;
|
||||
}
|
||||
|
||||
if(options[4].doesOccur) {
|
||||
if(options[4].value != NULL) {
|
||||
resPath = options[4].value; /* we'll use users resources */
|
||||
} else {
|
||||
resPath = NULL; /* we'll use ICU system resources for dumping */
|
||||
}
|
||||
} else {
|
||||
resPath = currdir; /* we'll just dump uresb samples resources */
|
||||
}
|
||||
|
||||
if(options[5].doesOccur) {
|
||||
trunc = TRUE;
|
||||
if(options[5].value != NULL) {
|
||||
truncsize = atoi(options[5].value); /* user defined printable size */
|
||||
} else {
|
||||
truncsize = URESB_DEFAULTTRUNC; /* we'll use default omitting size */
|
||||
}
|
||||
} else {
|
||||
trunc = FALSE;
|
||||
}
|
||||
|
||||
outerr = u_finit(stderr, locale, encoding);
|
||||
out = u_finit(stdout, locale, encoding);
|
||||
|
||||
u_fprintf(outerr, "We are running under %s locale\n", locale);
|
||||
/*
|
||||
for(i = 0; i<20; i++) {
|
||||
u_fprintf(out, "Error number %d is %U\n", i, getErrorName(i));
|
||||
reportError(&i);
|
||||
}
|
||||
*/
|
||||
bundle = ures_open(resPath, locale, &status);
|
||||
if(U_SUCCESS(status)) {
|
||||
u_fprintf(out, "%s\n", locale);
|
||||
printOutBundle(out, bundle, 0, &status);
|
||||
} else {
|
||||
reportError(&status);
|
||||
}
|
||||
|
||||
ures_close(bundle);
|
||||
|
||||
u_fclose(out);
|
||||
u_fclose(outerr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
const UChar *getErrorName(int32_t errorNumber) {
|
||||
void printIndent(UFILE *out, int32_t indent) {
|
||||
char inchar[256];
|
||||
int32_t i = 0;
|
||||
for(i = 0; i<indent; i++) {
|
||||
inchar[i] = ' ';
|
||||
}
|
||||
inchar[indent] = '\0';
|
||||
u_fprintf(out, "%s", inchar);
|
||||
}
|
||||
|
||||
void printHex(UFILE *out, const int8_t *what) {
|
||||
int8_t value = (int8_t)*what;
|
||||
char result[256];
|
||||
itoa(value, result, 16);
|
||||
if(value < 0x10) {
|
||||
u_fprintf(out, "0%s", result);
|
||||
} else {
|
||||
u_fprintf(out, "%s", result);
|
||||
}
|
||||
}
|
||||
|
||||
void printOutBundle(UFILE *out, UResourceBundle *resource, int32_t indent, UErrorCode *status) {
|
||||
int32_t noOfElements = ures_getSize(resource);
|
||||
int32_t i = 0;
|
||||
const char *key = ures_getKey(resource);
|
||||
|
||||
switch(ures_getType(resource)) {
|
||||
case RES_STRING :
|
||||
{
|
||||
int32_t len=0;
|
||||
const UChar* string = ures_getString(resource, &len, status);
|
||||
/* TODO: String truncation */
|
||||
/*
|
||||
if(trunc && len > truncsize) {
|
||||
printIndent(out, indent);
|
||||
u_fprintf(out, "// WARNING: this string, size %d is truncated to %d\n", len, truncsize/2);
|
||||
len = truncsize/2;
|
||||
}
|
||||
*/
|
||||
printIndent(out, indent);
|
||||
if(key != NULL) {
|
||||
u_fprintf(out, "%s { \"%U\" } \n", key, string);
|
||||
} else {
|
||||
u_fprintf(out, "\"%U\",\n", string);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case RES_INT :
|
||||
printIndent(out, indent);
|
||||
if(key != NULL) {
|
||||
u_fprintf(out, "%s:int { %li } \n", key, ures_getInt(resource, status));
|
||||
} else {
|
||||
u_fprintf(out, "%li,\n", ures_getInt(resource, status));
|
||||
}
|
||||
break;
|
||||
case RES_BINARY :
|
||||
{
|
||||
int32_t len = 0;
|
||||
const int8_t *data = (const int8_t *)ures_getBinary(resource, &len, status);
|
||||
if(trunc && len > truncsize) {
|
||||
printIndent(out, indent);
|
||||
u_fprintf(out, "// WARNING: this resource, size %li is truncated to %li\n", len, truncsize/2);
|
||||
len = truncsize/2;
|
||||
}
|
||||
if(U_SUCCESS(*status) && len > 0) {
|
||||
if(key != NULL) {
|
||||
printIndent(out, indent);
|
||||
u_fprintf(out, "%s:binary { ", key);
|
||||
for(i = 0; i<len; i++) {
|
||||
printHex(out, data++);
|
||||
}
|
||||
u_fprintf(out, " }\n");
|
||||
} else {
|
||||
u_fprintf(outerr, "This is a VERY STRANGE resource INDEED!\n");
|
||||
}
|
||||
|
||||
} else {
|
||||
reportError(status);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case RES_TABLE :
|
||||
case RES_ARRAY :
|
||||
{
|
||||
UResourceBundle *t = NULL;
|
||||
ures_resetIterator(resource);
|
||||
printIndent(out, indent);
|
||||
if(key != NULL) {
|
||||
u_fprintf(out, "%s ", key);
|
||||
}
|
||||
u_fprintf(out, "{\n");
|
||||
|
||||
while(ures_hasNext(resource)) {
|
||||
t = ures_getNextResource(resource, t, status);
|
||||
printOutBundle(out, t, indent+indentsize, status);
|
||||
}
|
||||
|
||||
printIndent(out, indent);
|
||||
u_fprintf(out, "}\n");
|
||||
ures_close(t);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void reportError(UErrorCode *status) {
|
||||
u_fprintf(outerr, "Error %d : %U happened!\n", *status, getErrorName(*status));
|
||||
}
|
||||
|
||||
|
||||
const UChar *getErrorName(UErrorCode errorNumber) {
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
int32_t len = 0;
|
||||
|
||||
|
|
|
@ -98,66 +98,5 @@ SOURCE=.\uresb.c
|
|||
|
||||
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
|
||||
# End Group
|
||||
# Begin Group "resource bundles"
|
||||
|
||||
# PROP Default_Filter ".txt"
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\en.txt
|
||||
|
||||
!IF "$(CFG)" == "uresb - Win32 Release"
|
||||
|
||||
!ELSEIF "$(CFG)" == "uresb - Win32 Debug"
|
||||
|
||||
# Begin Custom Build - Performing Custom Build Step on $(InputPath)
|
||||
InputPath=.\en.txt
|
||||
|
||||
"res1" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
|
||||
..\..\tools\genrb\debug\genrb -s . -d . $(InputPath)
|
||||
|
||||
# End Custom Build
|
||||
|
||||
!ENDIF
|
||||
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\root.txt
|
||||
|
||||
!IF "$(CFG)" == "uresb - Win32 Release"
|
||||
|
||||
!ELSEIF "$(CFG)" == "uresb - Win32 Debug"
|
||||
|
||||
# Begin Custom Build
|
||||
InputPath=.\root.txt
|
||||
|
||||
"res2" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
|
||||
..\..\tools\genrb\debug\genrb -s . -d . $(InputPath)
|
||||
|
||||
# End Custom Build
|
||||
|
||||
!ENDIF
|
||||
|
||||
# End Source File
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\sr.txt
|
||||
|
||||
!IF "$(CFG)" == "uresb - Win32 Release"
|
||||
|
||||
!ELSEIF "$(CFG)" == "uresb - Win32 Debug"
|
||||
|
||||
# Begin Custom Build
|
||||
InputPath=.\sr.txt
|
||||
|
||||
"res3" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)"
|
||||
..\..\tools\genrb\debug\genrb -s . -d . -e cp1251 $(InputPath)
|
||||
|
||||
# End Custom Build
|
||||
|
||||
!ENDIF
|
||||
|
||||
# End Source File
|
||||
# End Group
|
||||
# End Target
|
||||
# End Project
|
||||
|
|
|
@ -3,7 +3,7 @@ Microsoft Developer Studio Workspace File, Format Version 6.00
|
|||
|
||||
###############################################################################
|
||||
|
||||
Project: "uresb"=".\uresb.dsp" - Package Owner=<4>
|
||||
Project: "resources"=.\resources.dsp - Package Owner=<4>
|
||||
|
||||
Package=<5>
|
||||
{{{
|
||||
|
@ -15,6 +15,21 @@ Package=<4>
|
|||
|
||||
###############################################################################
|
||||
|
||||
Project: "uresb"=.\uresb.dsp - Package Owner=<4>
|
||||
|
||||
Package=<5>
|
||||
{{{
|
||||
}}}
|
||||
|
||||
Package=<4>
|
||||
{{{
|
||||
Begin Project Dependency
|
||||
Project_Dep_Name resources
|
||||
End Project Dependency
|
||||
}}}
|
||||
|
||||
###############################################################################
|
||||
|
||||
Global:
|
||||
|
||||
Package=<5>
|
||||
|
|
Loading…
Add table
Reference in a new issue