/* * pdc_fopen_logg opens a file. The function expects a UTF-8 encoded file name. * (see function pdc_convert_filename), if define PDC_UNICODE_FILENAME is set. * */ FILE * pdc_fopen_logg(pdc_core *pdc, const char *filename, const char *mode) { FILE *fp = NULL; int i = 0; #if defined(PDC_UNICODE_FILENAME) pdc_byte *outfilename = NULL; pdc_text_format nameformat = PDC_UTF8; pdc_text_format targetnameformat = pdc_utf16; int len = (int) pdc_strlen(filename); int outlen = 0; if (pdc_is_utf16be_unicode(filename)) nameformat = pdc_utf16be; /* convert filename from UTF-8 / UTF-16BE to UTF-16 or Latin-1 */ pdc_convert_string(pdc, nameformat, 0, NULL, (pdc_byte *) filename, len, &targetnameformat, NULL, &outfilename, &outlen, PDC_CONV_TRYBYTES | PDC_CONV_NOBOM, pdc_true); if (targetnameformat == pdc_bytes) { fp = fopen((const char *) outfilename, mode); } else { wchar_t wmode[8]; len = (int) strlen(mode); for (i = 0; i < len; i++) wmode[i] = (wchar_t) mode[i]; wmode[len] = 0; fp = _wfopen((wchar_t *) outfilename, wmode); } pdc_free(pdc, outfilename); #else (void) pdc; /* due to honorlang, codeset of LANG: UTF-8 */ if (pdc_is_utf8_bytecode(filename)) i = 3; fp = fopen(&filename[i], mode); #endif pdc_logg_openclose(pdc, fp, pdc_true); return fp; }
/* normalized file name according PDF specification */ void pdc_put_pdffilename(pdc_output *out, const char *text, int len) { static const char *fn = "pdc_put_pdffilename"; pdc_byte *btext = (pdc_byte *) text; pdc_bool isuni = pdc_is_utf16be_unicode(btext); char *ttext; pdc_byte c, cp, cpp; int i, ia = 0, j = 0; ttext = (char *) pdc_calloc(out->pdc, (size_t) (len + 4), fn); if (isuni) { ttext[0] = PDF_BOM0; ttext[1] = PDF_BOM1; ia = 2; j = 2; } /* absolute path name: * r:\pdfdocs\spec.pdf -> /r/pdfdocs/spec.pdf * pclib/eng:\pdfdocs\spec.pdf -> /pclib/eng/pdfdocs/spec.pdf */ cp = 0x7F; for (i = ia; i < len; i++) { c = btext[i]; if (c == PDF_COLON && (!isuni || cp == 0)) { if (isuni) { ttext[j] = 0; j++; } ttext[j] = PDF_SLASH; j++; break; } cp = c; } cp = 0x7F; cpp = 0x7F; for (i = ia; i < len; i++) { c = btext[i]; if ((c == PDF_BACKSLASH || c == PDF_SLASH || c == PDF_COLON) && (!isuni || cp == 0)) { /* convert to slash, but avoid multiple slashes */ if (cpp != PDF_SLASH) { c = PDF_SLASH; } else { if (isuni) j--; continue; } } ttext[j] = c; j++; cp = c; if (c) cpp = c; } len = j; pdc_put_pdfstring(out, ttext, len); pdc_free(out->pdc, ttext); }