#include"unicode/platform.h" #if U_PLATFORM == U_PF_CYGWIN && defined(__STRICT_ANSI__) /* GCC on cygwin (not msys2) with -std=c++11 or newer has stopped defining fileno, unless gcc extensions are enabled (-std=gnu11). fileno is POSIX, but is not standard ANSI C. It has always been a GCC extension, which everyone used until recently. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=40278#c7
#if !UCONFIG_NO_FORMATTING /* if locale is 0, use the default */ if (u_locbund_init(&result->str.fBundle, locale) == nullptr) { /* DO NOT FCLOSE HERE! */
uprv_free(result); return nullptr;
} #endif
/* If the codepage is not "" use the ucnv_open default behavior */ if(codepage == nullptr || *codepage != '\0') {
result->fConverter = ucnv_open(codepage, &status);
} /* else result->fConverter is already memset'd to nullptr. */
if(U_SUCCESS(status)) {
result->fOwnFile = takeOwnership;
} else { #if !UCONFIG_NO_FORMATTING
u_locbund_close(&result->str.fBundle); #endif /* DO NOT fclose here!!!!!! */
uprv_free(result);
result = nullptr;
}
return result;
}
U_CAPI UFILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
u_finit(FILE *f, constchar *locale, constchar *codepage)
{ return finit_owner(f, locale, codepage, false);
}
U_CAPI UFILE* U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
u_fopen(constchar *filename, constchar *perm, constchar *locale, constchar *codepage)
{
UFILE *result;
FILE *systemFile = fopen(filename, perm); if (systemFile == nullptr) { return nullptr;
}
result = finit_owner(systemFile, locale, codepage, true);
if (!result) { /* Something bad happened.
Maybe the converter couldn't be opened. */
fclose(systemFile);
}
return result; /* not a file leak */
}
// FILENAME_BUF_MAX represents the largest size that we are willing to use for a // stack-allocated buffer to contain a file name or path. If PATH_MAX (POSIX) or MAX_PATH // (Windows) are defined and are smaller than this we will use their defined value; // otherwise, we will use FILENAME_BUF_MAX for the stack-allocated buffer, and dynamically // allocate a buffer for any file name or path that is that length or longer. #define FILENAME_BUF_MAX 296 #ifdefined PATH_MAX && PATH_MAX < FILENAME_BUF_MAX #define FILENAME_BUF_CAPACITY PATH_MAX #elifdefined MAX_PATH && MAX_PATH < FILENAME_BUF_MAX #define FILENAME_BUF_CAPACITY MAX_PATH #else #define FILENAME_BUF_CAPACITY FILENAME_BUF_MAX #endif
icu::UnicodeString filenameString(true, filename, -1); // readonly aliasing, does not allocate memory // extract with conversion to platform default codepage, return full length (not including 0 termination)
int32_t filenameLength = filenameString.extract(0, filenameString.length(), filenameBuffer, FILENAME_BUF_CAPACITY); if (filenameLength >= FILENAME_BUF_CAPACITY) { // could not fit (with zero termination) in buffer
filenameBuffer = static_cast<char *>(uprv_malloc(++filenameLength)); // add one for zero termination if (!filenameBuffer) { return nullptr;
}
filenameString.extract(0, filenameString.length(), filenameBuffer, filenameLength);
}
result = u_fopen(filenameBuffer, perm, locale, codepage); #if U_PLATFORM_USES_ONLY_WIN32_API /* Try Windows API _wfopen if the above fails. */ if (!result) { // TODO: test this code path, including wperm. wchar_t wperm[40] = {};
size_t retVal;
mbstowcs_s(&retVal, wperm, UPRV_LENGTHOF(wperm), perm, _TRUNCATE);
FILE *systemFile = _wfopen(reinterpret_cast<constwchar_t *>(filename), wperm); // may return nullptr for long filename if (systemFile) {
result = finit_owner(systemFile, locale, codepage, true);
} if (!result && systemFile) { /* Something bad happened. Maybe the converter couldn't be opened.
Bu do not fclose(systemFile) if systemFile is nullptr. */
fclose(systemFile);
}
} #endif if (filenameBuffer != buffer) {
uprv_free(filenameBuffer);
} return result; /* not a file leak */
}
result = (UFILE*) uprv_malloc(sizeof(UFILE)); /* Null pointer test */ if (result == nullptr) { return nullptr; /* Just get out. */
}
uprv_memset(result, 0, sizeof(UFILE));
result->str.fBuffer = stringBuf;
result->str.fPos = stringBuf;
result->str.fLimit = stringBuf+capacity;
#if !UCONFIG_NO_FORMATTING /* if locale is 0, use the default */ if (u_locbund_init(&result->str.fBundle, locale) == nullptr) { /* DO NOT FCLOSE HERE! */
uprv_free(result); return nullptr;
} #endif
U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
u_fsetcodepage( constchar *codepage,
UFILE *file)
{
UErrorCode status = U_ZERO_ERROR;
int32_t retVal = -1;
/* We use the normal default codepage for this system, and not the one for the locale. */ if ((file->str.fPos == file->str.fBuffer) && (file->str.fLimit == file->str.fBuffer)) {
ucnv_close(file->fConverter);
file->fConverter = ucnv_open(codepage, &status); if(U_SUCCESS(status)) {
retVal = 0;
}
} return retVal;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.