MCPcopy Create free account
hub / github.com/beefytech/Beef / LoadTextData

Method LoadTextData

BeefySysLib/Common.cpp:884–951  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

882}
883
884char* Beefy::LoadTextData(const StringImpl& path, int* size)
885{
886 FILE* fP = fopen(path.c_str(), "rb");
887 if (fP == NULL)
888 return NULL;
889
890 fseek(fP, 0, SEEK_END);
891 int fileSize = (int32)ftell(fP);
892 int strLen = fileSize;
893 fseek(fP, 0, SEEK_SET);
894
895 uint8 charHeader[3] = {0};
896 int readSize = (int)fread(charHeader, 1, 3, fP);
897
898 if ((charHeader[0] == 0xFF) && (charHeader[1] == 0xFE))
899 {
900 //UTF16 LE
901
902 int dataLen = fileSize - 2;
903 char* data = new char[dataLen + 2];
904 data[0] = (char)charHeader[2];
905 data[dataLen] = 0;
906 data[dataLen + 1] = 0;
907 int readSize = (int)fread(data + 1, 1, dataLen - 1, fP);
908 (void)readSize;
909 fclose(fP);
910
911 // UTF16
912 UTF16String str;
913 str.Set((wchar_t*)data);
914 delete [] data;
915
916 String utf8Str = UTF8Encode(str);
917
918 char* utf8Data = new char[utf8Str.length() + 1];
919 strLen = (int)utf8Str.length();
920 if (size != NULL)
921 *size = strLen;
922 memcpy(utf8Data, utf8Str.c_str(), strLen);
923 return utf8Data;
924 }
925 else if ((charHeader[0] == 0xEF) && (charHeader[1] == 0xBB) && (charHeader[2] == 0xBF))
926 {
927 strLen = fileSize - 3;
928 char* data = new char[strLen + 1];
929 data[strLen] = 0;
930 if (size != NULL)
931 *size = strLen;
932 int readSize = (int)fread(data, 1, strLen, fP);
933 (void)readSize;
934 fclose(fP);
935 return data;
936 }
937
938 if (size != NULL)
939 *size = strLen;
940 char* data = new char[strLen + 1];
941 data[strLen] = 0;

Callers

nothing calls this directly

Calls 4

c_strMethod · 0.45
SetMethod · 0.45
lengthMethod · 0.45
ReleaseMethod · 0.45

Tested by

no test coverage detected