- if(argc==2) {
- in=T_FileStream_stdin();
- } else {
- in=T_FileStream_open(argv[2], "r");
- if(in==NULL) {
- fprintf(stderr, "gencmn: unable to open input file %s\n", argv[2]);
- exit(U_FILE_ACCESS_ERROR);
- }
- }
-
- if (verbose) {
- if(sourceTOC) {
- printf("generating %s_%s.c (table of contents source file)\n", options[6].value, options[7].value);
- } else {
- printf("generating %s.%s (common data file with table of contents)\n", options[6].value, options[7].value);
- }
- }
-
- /* read the list of files and get their lengths */
- while(T_FileStream_readLine(in, line, sizeof(line))!=NULL) {
- /* remove trailing newline characters */
- s=line;
- while(*s!=0) {
- if(*s=='\r' || *s=='\n') {
- *s=0;
- break;
- }
- ++s;
- }
-
- /* check for comment */
-
- if (*line == '#') {
- continue;
- }
-
- /* add the file */
-
- addFile(getLongPathname(line), sourceTOC, verbose);
- }
-
- if(in!=T_FileStream_stdin()) {
- T_FileStream_close(in);
- }
-
- if(fileCount==0) {
- fprintf(stderr, "gencmn: no files listed in %s\n", argc==2 ? "<stdin>" : argv[2]);
- return 0;
- }
-
- /* sort the files by basename */
- qsort(files, fileCount, sizeof(File), compareFiles);
-
- if(!sourceTOC) {
- UNewDataMemory *out;
-
- /* determine the offsets of all basenames and files in this common one */
- basenameOffset=4+8*fileCount;
- fileOffset=(basenameOffset+(basenameTotal+15))&~0xf;
- for(i=0; i<fileCount; ++i) {
- files[i].fileOffset=fileOffset;
- fileOffset+=(files[i].fileSize+15)&~0xf;
- files[i].basenameOffset=basenameOffset;
- basenameOffset+=files[i].basenameLength;
- }
-
- /* create the output file */
- out=udata_create(options[4].value, options[7].value, options[6].value,
- &dataInfo,
- options[3].doesOccur ? U_COPYRIGHT_STRING : options[5].value,
- &errorCode);
- if(U_FAILURE(errorCode)) {
- fprintf(stderr, "gencmn: udata_create(-d %s -n %s -t %s) failed - %s\n",
- options[4].value, options[6].value, options[7].value,
- u_errorName(errorCode));
- exit(errorCode);
- }
-
- /* write the table of contents */
- udata_write32(out, fileCount);
- for(i=0; i<fileCount; ++i) {
- udata_write32(out, files[i].basenameOffset);
- udata_write32(out, files[i].fileOffset);
- }
-
- /* write the basenames */
- for(i=0; i<fileCount; ++i) {
- udata_writeString(out, files[i].basename, files[i].basenameLength);
- }
- length=4+8*fileCount+basenameTotal;
-
- /* copy the files */
- for(i=0; i<fileCount; ++i) {
- /* pad to 16-align the next file */
- length&=0xf;
- if(length!=0) {
- udata_writePadding(out, 16-length);
- }
-
- if (verbose) {
- printf("adding %s (%ld byte%s)\n", files[i].pathname, (long)files[i].fileSize, files[i].fileSize == 1 ? "" : "s");
- }
-
- /* copy the next file */
- file=T_FileStream_open(files[i].pathname, "rb");
- if(file==NULL) {
- fprintf(stderr, "gencmn: unable to open listed file %s\n", files[i].pathname);
- exit(U_FILE_ACCESS_ERROR);
- }
- for(nread = 0;;) {
- length=T_FileStream_read(file, buffer, sizeof(buffer));
- if(length <= 0) {
- break;
- }
- nread += length;
- udata_writeBlock(out, buffer, length);
- }
- T_FileStream_close(file);
- length=files[i].fileSize;
-
- if (nread != files[i].fileSize) {
- fprintf(stderr, "gencmn: unable to read %s properly (got %ld/%ld byte%s)\n", files[i].pathname, (long)nread, (long)files[i].fileSize, files[i].fileSize == 1 ? "" : "s");
- exit(U_FILE_ACCESS_ERROR);
- }
- }
-
- /* finish */
- udata_finish(out, &errorCode);
- if(U_FAILURE(errorCode)) {
- fprintf(stderr, "gencmn: udata_finish() failed - %s\n", u_errorName(errorCode));
- exit(errorCode);
- }
- } else {
- /* write a .c source file with the table of contents */
- char *filename;
- FileStream *out;
-
- /* create the output filename */
- filename=s=buffer;
- uprv_strcpy(filename, options[4].value);
- s=filename+uprv_strlen(filename);
- if(s>filename && *(s-1)!=U_FILE_SEP_CHAR) {
- *s++=U_FILE_SEP_CHAR;
- }
- uprv_strcpy(s, options[6].value);
- if(*(options[7].value)!=0) {
- s+=uprv_strlen(s);
- *s++='_';
- uprv_strcpy(s, options[7].value);
- }
- s+=uprv_strlen(s);
- uprv_strcpy(s, ".c");
-
- /* open the output file */
- out=T_FileStream_open(filename, "w");
- if(out==NULL) {
- fprintf(stderr, "gencmn: unable to open .c output file %s\n", filename);
- exit(U_FILE_ACCESS_ERROR);
- }
-
- /* If an entrypoint is specified, use it. */
- if(options[9].doesOccur) {
- entrypointName = options[9].value;
- } else {
- entrypointName = options[6].value;
- }
-
-
-#if 0
- symPrefix = (char *) uprv_malloc(uprv_strlen(entrypointName) + 2);
-
- /* test for NULL */
- if (symPrefix == NULL) {
- sprintf(buffer, "U_MEMORY_ALLOCATION_ERROR");
- exit(U_MEMORY_ALLOCATION_ERROR);
- }
-
- uprv_strcpy(symPrefix, entrypointName);
- uprv_strcat(symPrefix, "_");
-#endif
-
- /* write the source file */
- sprintf(buffer,
- "/*\n"
- " * ICU common data table of contents for %s.%s ,\n"
- " * Automatically generated by icu/source/tools/gencmn/gencmn .\n"
- " */\n\n"
- "#include \"unicode/utypes.h\"\n"
- "#include \"unicode/udata.h\"\n"
- "\n"
- "/* external symbol declarations for data */\n",
- options[6].value, options[7].value);
- T_FileStream_writeLine(out, buffer);
-
- sprintf(buffer, "extern const char\n %s%s[]", symPrefix?symPrefix:"", files[0].pathname);
- T_FileStream_writeLine(out, buffer);
- for(i=1; i<fileCount; ++i) {
- sprintf(buffer, ",\n %s%s[]", symPrefix?symPrefix:"", files[i].pathname);
- T_FileStream_writeLine(out, buffer);
- }
- T_FileStream_writeLine(out, ";\n\n");
-
- sprintf(
- buffer,
- "U_EXPORT const struct {\n"
- " uint16_t headerSize;\n"
- " uint8_t magic1, magic2;\n"
- " UDataInfo info;\n"
- " char padding[%lu];\n"
- " uint32_t count, reserved;\n"
- " struct {\n"
- " const char *name;\n"
- " const void *data;\n"
- " } toc[%lu];\n"
- "} U_EXPORT2 %s_dat = {\n"
- " 32, 0xda, 0x27, {\n"
- " %lu, 0,\n"
- " %u, %u, %u, 0,\n"
- " {0x54, 0x6f, 0x43, 0x50},\n"
- " {1, 0, 0, 0},\n"
- " {0, 0, 0, 0}\n"
- " },\n"
- " \"\", %lu, 0, {\n",
- (unsigned long)32-4-sizeof(UDataInfo),
- (unsigned long)fileCount,
- entrypointName,
- (unsigned long)sizeof(UDataInfo),
- U_IS_BIG_ENDIAN,
- U_CHARSET_FAMILY,
- U_SIZEOF_UCHAR,
- (unsigned long)fileCount
- );
- T_FileStream_writeLine(out, buffer);
-
- sprintf(buffer, " { \"%s\", %s%s }", files[0].basename, symPrefix?symPrefix:"", files[0].pathname);
- T_FileStream_writeLine(out, buffer);
- for(i=1; i<fileCount; ++i) {
- sprintf(buffer, ",\n { \"%s\", %s%s }", files[i].basename, symPrefix?symPrefix:"", files[i].pathname);
- T_FileStream_writeLine(out, buffer);
- }
-
- T_FileStream_writeLine(out, "\n }\n};\n");
- T_FileStream_close(out);
-
- uprv_free(symPrefix);
- }