Revision: 9569 https://osdn.net/projects/ttssh2/scm/svn/commits/9569 Author: zmatsuo Date: 2021-12-05 22:08:06 +0900 (Sun, 05 Dec 2021) Log Message: ----------- 読み込んだファイルの文字コードも返すAPIを追加 - LoadFileU8C() Modified Paths: -------------- trunk/teraterm/common/fileread.cpp trunk/teraterm/common/fileread.h -------------- next part -------------- Modified: trunk/teraterm/common/fileread.cpp =================================================================== --- trunk/teraterm/common/fileread.cpp 2021-12-05 13:07:54 UTC (rev 9568) +++ trunk/teraterm/common/fileread.cpp 2021-12-05 13:08:06 UTC (rev 9569) @@ -80,7 +80,9 @@ /** * \x83t\x83@\x83C\x83\x8B\x82\xF0\x83\x81\x83\x82\x83\x8A\x82ɓǂݍ\x9E\x82\xDE + * @param[in] fp "rb" \x82ŃI\x81[\x83v\x83\x93\x82\xB7\x82邱\x82\xC6 * @param[out] *_len \x83T\x83C\x83Y(\x8DŌ\xE3\x82ɕt\x89\xC1\x82\xB3\x82\xEA\x82\xE9"\0\0"\x82\xF0\x8A܂\xDE) + * \x8Fȗ\xAA\x95s\x89\xC2 * @retval \x83t\x83@\x83C\x83\x8B\x82̒\x86\x90g\x82ւ̃|\x83C\x83\x93\x83^(\x8Eg\x97p\x8C\xE3free()\x82\xB7\x82邱\x82\xC6) * NULL=\x83G\x83\x89\x81[ */ @@ -115,17 +117,22 @@ * * @param[out] *_len \x83T\x83C\x83Y(\x8DŌ\xE3\x82ɕt\x89\xC1\x82\xB3\x82\xEA\x82\xE9"\0"\x82\xF0\x8A܂\xDE) * NULL\x82̂Ƃ\xAB\x82͒\xB7\x82\xB3\x82\xF0\x95Ԃ\xB3\x82Ȃ\xA2 + * @param[out] *_code \x83t\x83@\x83C\x83\x8B\x82̕\xB6\x8E\x9A\x83R\x81[\x83h * @retval \x83t\x83@\x83C\x83\x8B\x82̒\x86\x90g\x82ւ̃|\x83C\x83\x93\x83^(\x8Eg\x97p\x8C\xE3free()\x82\xB7\x82邱\x82\xC6) * NULL=\x83G\x83\x89\x81[ */ -char *LoadFileU8(FILE *fp, size_t *_len) +char *LoadFileU8C(FILE *fp, size_t *_len, LoadFileCode *_code) { size_t len; + LoadFileCode code; void *vbuf = LoadRawFile(fp, &len); if (vbuf == NULL) { if (_len != NULL) { *_len = 0; } + if (_code != NULL) { + *_code = FILE_CODE_NONE; + } return NULL; } @@ -133,6 +140,7 @@ if (len >= 3 && (buf[0] == 0xef && buf[1] == 0xbb && buf[2] == 0xbf)) { // UTF-8 BOM // trim BOM + code = FILE_CODE_UTF8; len -= 3; memmove(&buf[0], &buf[3], len); } else if(len >= 2 && (buf[0] == 0xff && buf[1] == 0xfe)) { @@ -139,6 +147,7 @@ // UTF-16LE BOM // UTF-16LE -> UTF-8 const char *u8 = ToU8W((wchar_t *)&buf[2]); + code = FILE_CODE_UTF16LE; free(buf); buf = (uint8_t *)u8; @@ -158,6 +167,7 @@ } // UTF-16LE -> UTF-8 const char *u8 = ToU8W((wchar_t *)&buf[2]); + code = FILE_CODE_UTF16BE; free(buf); buf = (uint8_t *)u8; @@ -164,10 +174,12 @@ } else { // ACP? -> UTF-8 const char *u8 = ToU8A((char *)buf); + code = FILE_CODE_UTF8; if (u8 != NULL) { // ACP -> UTF-8 free(buf); buf = (uint8_t *)u8; + code = FILE_CODE_ACP; } } @@ -174,6 +186,9 @@ if (_len != NULL) { *_len = strlen((char *)buf)+1; // \x89\xFC\x82߂Ē\xB7\x82\xB3\x82\xF0\x8Cv\x82\xE9 } + if (_code != NULL) { + *_code = code; + } return (char *)buf; } @@ -180,11 +195,26 @@ /** * \x83t\x83@\x83C\x83\x8B\x82\xF0\x83\x81\x83\x82\x83\x8A\x82ɓǂݍ\x9E\x82\xDE * \x92\x86\x90g\x82\xCDUTF-8\x82ɕϊ\xB7\x82\xB3\x82\xEA\x82\xE9 + * \x83t\x83@\x83C\x83\x8B\x82̍Ō\xE3\x82\xCD '\0'\x82Ń^\x81[\x83~\x83l\x81[\x83g\x82\xB3\x82\xEA\x82Ă\xA2\x82\xE9 * * @param[out] *_len \x83T\x83C\x83Y(\x8DŌ\xE3\x82ɕt\x89\xC1\x82\xB3\x82\xEA\x82\xE9"\0"\x82\xF0\x8A܂\xDE) + * NULL\x82̂Ƃ\xAB\x82͒\xB7\x82\xB3\x82\xF0\x95Ԃ\xB3\x82Ȃ\xA2 * @retval \x83t\x83@\x83C\x83\x8B\x82̒\x86\x90g\x82ւ̃|\x83C\x83\x93\x83^(\x8Eg\x97p\x8C\xE3free()\x82\xB7\x82邱\x82\xC6) * NULL=\x83G\x83\x89\x81[ */ +char *LoadFileU8(FILE *fp, size_t *_len) +{ + return LoadFileU8C(fp, _len, NULL); +} + +/** + * \x83t\x83@\x83C\x83\x8B\x82\xF0\x83\x81\x83\x82\x83\x8A\x82ɓǂݍ\x9E\x82\xDE + * \x92\x86\x90g\x82\xCDUTF-8\x82ɕϊ\xB7\x82\xB3\x82\xEA\x82\xE9 + * + * @param[out] *_len \x83T\x83C\x83Y(\x8DŌ\xE3\x82ɕt\x89\xC1\x82\xB3\x82\xEA\x82\xE9"\0"\x82\xF0\x8A܂\xDE) + * @retval \x83t\x83@\x83C\x83\x8B\x82̒\x86\x90g\x82ւ̃|\x83C\x83\x93\x83^(\x8Eg\x97p\x8C\xE3free()\x82\xB7\x82邱\x82\xC6) + * NULL=\x83G\x83\x89\x81[ + */ char *LoadFileU8A(const char *FileName, size_t *_len) { *_len = 0; Modified: trunk/teraterm/common/fileread.h =================================================================== --- trunk/teraterm/common/fileread.h 2021-12-05 13:07:54 UTC (rev 9568) +++ trunk/teraterm/common/fileread.h 2021-12-05 13:08:06 UTC (rev 9569) @@ -26,10 +26,24 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#pragma once + +#include <stdio.h> +#include <stdlib.h> + #ifdef __cplusplus extern "C" { #endif//__cplusplus +typedef enum { + FILE_CODE_NONE, + FILE_CODE_UTF8, + FILE_CODE_UTF16LE, + FILE_CODE_UTF16BE, + FILE_CODE_ACP, +} LoadFileCode; + +char *LoadFileU8C(FILE *fp, size_t *_len, LoadFileCode *_code); char *LoadFileU8A(const char *FileName, size_t *_len); char *LoadFileU8W(const wchar_t *FileName, size_t *_len); wchar_t *LoadFileWA(const char *FileName, size_t *_len);