static int32_t u_printf_pad_and_justify(void *context, const u_printf_spec_info *info, const UChar *result, int32_t resultLen) { UFILE *output = (UFILE *)context; int32_t written, i; /* pad and justify, if needed */ if(info->fWidth != -1 && resultLen < info->fWidth) { /* left justify */ if(info->fLeft) { written = u_file_write(result, resultLen, output); for(i = 0; i < info->fWidth - resultLen; ++i) { written += u_file_write(&info->fPadChar, 1, output); } } /* right justify */ else { written = 0; for(i = 0; i < info->fWidth - resultLen; ++i) { written += u_file_write(&info->fPadChar, 1, output); } written += u_file_write(result, resultLen, output); } } /* just write the formatted output */ else { written = u_file_write(result, resultLen, output); } return written; }
U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ u_fputs(const UChar *s, UFILE *f) { int32_t count = u_file_write(s, u_strlen(s), f); count += u_file_write(DELIMITERS, DELIMITERS_LEN, f); return count; }
static int32_t U_EXPORT2 u_printf_write(void * context, const UChar * str, int32_t count) { return u_file_write(str, count, (UFILE *)context); }
U_CAPI UChar32 U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ u_fputc(UChar32 uc, UFILE *f) { UChar buf[2]; int32_t idx = 0; UBool isError = FALSE; U16_APPEND(buf, idx, sizeof(buf)/sizeof(*buf), uc, isError); if (isError) { return U_EOF; } return u_file_write(buf, idx, f) == idx ? uc : U_EOF; }
static int icu_ufile_write(lua_State *L) { int nargs = lua_gettop(L) - 1; int arg = 2; int status = 1; UFILE* ufile = icu4lua_checkopenufile(L,1,UFILE_UV_META); for (; nargs--; arg++) { if (lua_type(L, arg) == LUA_TNUMBER) { status = status && u_fprintf(ufile, LUA_NUMBER_FMT, lua_tonumber(L, arg))>0; } else { const UChar* ustr = icu4lua_checkustring(L, arg, UFILE_UV_USTRING_META); int32_t ustr_l = (int32_t)icu4lua_ustrlen(L, arg); status = status && (u_file_write(ustr, ustr_l, ufile) == ustr_l); } } return pushresult(L, status, NULL); }
U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */ u_fputc(UChar uc, UFILE *f) { return u_file_write(&uc, 1, f) == 1 ? uc : EOF; }
static void printString(UFILE *out, const UChar *str, int32_t len) { u_file_write(str, len, out); }
void file_write(uint64_t descriptor, JSStringRef text) { UFILE* ufile = descriptor_to_ufile(descriptor); u_file_write(JSStringGetCharactersPtr(text), (uint32_t)JSStringGetLength(text), ufile); }
/* * main() This one function is all of the application code. */ int main(int argc, char **argv) { UBool displayUsage = FALSE; /* Set true if command line err or help */ /* option was requested. */ UBool verbose = FALSE; /* Set true if -v command line option. */ char *optionError = NULL; /* If command line contains an unrecognized */ /* option, this will point to it. */ char *locale=NULL; /* Locale name. Null for system default, */ /* otherwise set from command line. */ const char * programName = argv[0]; /* Program invocation name. */ UFILE *u_stdout; /* Unicode stdout file. */ UErrorCode err = U_ZERO_ERROR; /* Error return, used for most ICU */ /* functions. */ UResourceBundle *myResources; /* ICU Resource "handles" */ UResourceBundle *fortunes_r; int32_t numFortunes; /* Number of fortune strings available. */ int i; const UChar *resString; /* Points to strings fetched from Resources. */ int32_t len; /* Process command line options. * -l locale specify a locale * -v verbose mode. Display extra messages. * -? or --help display a usage line */ for (i=1; i<argc; i++) { if (strcmp(argv[i], "-l") ==0) { if (++i < argc) { locale = argv[i]; } continue; } if (strcmp(argv[i], "-v") == 0) { verbose = TRUE; continue;} if (strcmp(argv[i], "-?") == 0 || strcmp(argv[i], "--help") == 0) { displayUsage = TRUE; continue;} optionError = argv[i]; displayUsage = TRUE; break; } /* ICU's icuio package provides a convenient way to write Unicode * data to stdout. The string data that we get from resources * will be UChar * strings, which icuio can handle nicely. */ u_stdout = u_finit(stdout, NULL /*locale*/, NULL /*codepage */); if (verbose) { u_fprintf(u_stdout, "%s: checking output via icuio.\n", programName); } #ifndef UFORTUNE_NOSETAPPDATA /* Tell ICU where our resource data is located in memory. * The data lives in the Fortune_Resources dll, and we just * pass the address of an exported symbol from that library * to ICU. */ udata_setAppData("fortune_resources", &fortune_resources_dat, &err); if (U_FAILURE(err)) { fprintf(stderr, "%s: udata_setAppData failed with error \"%s\"\n", programName, u_errorName(err)); exit(-1); } #endif /* Open our resources. */ myResources = ures_open("fortune_resources", locale, &err); if (U_FAILURE(err)) { fprintf(stderr, "%s: ures_open failed with error \"%s\"\n", programName, u_errorName(err)); exit(-1); } if (verbose) { u_fprintf(u_stdout, "status from ures_open(\"fortune_resources\", %s) is %s\n", locale? locale: " ", u_errorName(err)); } /* * Display any command line option usage errors and/or the * usage help message. These messages come from our resource bundle. */ if (optionError != NULL) { const UChar *msg = ures_getStringByKey(myResources, "optionMessage", &len, &err); if (U_FAILURE(err)) { fprintf(stderr, "%s: ures_getStringByKey(\"optionMessage\") failed, %s\n", programName, u_errorName(err)); exit(-1); } u_file_write(msg, len, u_stdout); /* msg is UChar *, from resource */ u_fprintf(u_stdout, " %s\n", optionError); /* optionError is char *, from argv */ } if (displayUsage) { const UChar *usage; int returnValue=0; usage = ures_getStringByKey(myResources, "usage", &len, &err); if (U_FAILURE(err)) { fprintf(stderr, "%s: ures_getStringByKey(\"usage\") failed, %s\n", programName, u_errorName(err)); exit(-1); } u_file_write(usage, len, u_stdout); if (optionError != NULL) {returnValue = -1;} return returnValue; } /* * Open the "fortunes" resources from within the already open resources */ fortunes_r = ures_getByKey(myResources, "fortunes", NULL, &err); if (U_FAILURE(err)) { fprintf(stderr, "%s: ures_getByKey(\"fortunes\") failed, %s\n", programName, u_errorName(err)); exit(-1); } /* * Pick up and display a random fortune * */ numFortunes = ures_countArrayItems(myResources, "fortunes", &err); if (U_FAILURE(err)) { fprintf(stderr, "%s: ures_countArrayItems(\"fortunes\") failed, %s\n", programName, u_errorName(err)); exit(-1); } if (numFortunes <= 0) { fprintf(stderr, "%s: no fortunes found.\n", programName); exit(-1); } i = (int)time(NULL) % numFortunes; /* Use time to pick a somewhat-random fortune. */ resString = ures_getStringByIndex(fortunes_r, i, &len, &err); if (U_FAILURE(err)) { fprintf(stderr, "%s: ures_getStringByIndex(%d) failed, %s\n", programName, i, u_errorName(err)); exit(-1); } u_file_write(resString, len, u_stdout); /* Write out the message */ u_fputc(0x0a, u_stdout); /* and a trailing newline */ return 0; }