void writeFileContent(const TCHAR *file2write, const char *content2write) { FILE *f = generic_fopen(file2write, TEXT("w+")); fwrite(content2write, sizeof(content2write[0]), strlen(content2write), f); fflush(f); fclose(f); }
std::string getFileContent(const TCHAR *file2read) { const size_t blockSize = 1024; char data[blockSize]; std::string wholeFileContent = ""; FILE *fp = generic_fopen(file2read, TEXT("rb")); size_t lenFile = 0; do { lenFile = fread(data, 1, blockSize - 1, fp); if (lenFile <= 0) break; if (lenFile >= blockSize - 1) data[blockSize - 1] = '\0'; else data[lenFile] = '\0'; wholeFileContent += data; } while (lenFile > 0); fclose(fp); return wholeFileContent; }
bool FileManager::createEmptyFile(const TCHAR * path) { FILE * file = generic_fopen(path, TEXT("wb")); if (!file) return false; fclose(file); return true; }
void writeLog(const TCHAR *logFileName, const char *log2write) { FILE *f = generic_fopen(logFileName, TEXT("a+")); fwrite(log2write, sizeof(log2write[0]), strlen(log2write), f); fputc('\n', f); fflush(f); fclose(f); }
bool TiXmlDocument::LoadFile( const TCHAR* filename ) { // Delete the existing data: Clear(); location.Clear(); // There was a really terrifying little bug here. The code: // value = filename // in the STL case, cause the assignment method of the std::generic_string to // be called. What is strange, is that the std::generic_string had the same // address as it's c_str() method, and so bad things happen. Looks // like a bug in the Microsoft STL implementation. // See STL_STRING_BUG above. // Fixed with the StringToBuffer class. value = filename; FILE* file = generic_fopen( value.c_str (), TEXT("r") ); if ( file ) { // Get the file size, so we can pre-allocate the generic_string. HUGE speed impact. long length = 0; fseek( file, 0, SEEK_END ); length = ftell( file ); fseek( file, 0, SEEK_SET ); // Strange case, but good to handle up front. if ( length == 0 ) { fclose( file ); return false; } // If we have a file, assume it is all one big XML file, and read it in. // The document parser may decide the document ends sooner than the entire file, however. TIXML_STRING data; data.reserve( length ); const int BUF_SIZE = 2048; TCHAR buf[BUF_SIZE]; while( generic_fgets( buf, BUF_SIZE, file ) ) { data += buf; } fclose( file ); Parse( data.c_str(), 0 ); if ( Error() ) return false; else return true; } SetError( TIXML_ERROR_OPENING_FILE, 0, 0 ); return false; }
bool TiXmlDocument::SaveFile( const TCHAR * filename ) const { // The old c stuff lives on... FILE* fp = generic_fopen( filename, TEXT("w") ); if ( fp ) { Print( fp, 0 ); fclose( fp ); return true; } return false; }
int mame_fchecksum(const char *gamename, const char *filename, unsigned int *length, char* hash) { mame_file *file; /* first open the file; we pass the source hash because it contains the expected checksum for the file (used to load by checksum) */ file = generic_fopen(FILETYPE_ROM, gamename, filename, hash, FILEFLAG_OPENREAD | FILEFLAG_HASH | FILEFLAG_VERIFY_ONLY); /* if we didn't succeed return -1 */ if (!file) return -1; /* close the file and save the length & checksum */ hash_data_copy(hash, file->hash); *length = file->length; mame_fclose(file); return 0; }
inline bool FileManager::loadFileData(Document doc, const TCHAR * filename, char* data, Utf8_16_Read * UnicodeConvertor, LangType & language, int & encoding, FormatType* pFormat) { FILE *fp = generic_fopen(filename, TEXT("rb")); if (!fp) return false; //Get file size _fseeki64 (fp , 0 , SEEK_END); unsigned __int64 fileSize =_ftelli64(fp); rewind(fp); // size/6 is the normal room Scintilla keeps for editing, but here we limit it to 1MiB when loading (maybe we want to load big files without editing them too much) unsigned __int64 bufferSizeRequested = fileSize + min(1<<20,fileSize/6); // As a 32bit application, we cannot allocate 2 buffer of more than INT_MAX size (it takes the whole address space) if(bufferSizeRequested > INT_MAX) { ::MessageBox(NULL, TEXT("File is too big to be opened by Notepad++"), TEXT("File open problem"), MB_OK|MB_APPLMODAL); /* _nativeLangSpeaker.messageBox("NbFileToOpenImportantWarning", _pPublicInterface->getHSelf(), TEXT("File is too big to be opened by Notepad++"), TEXT("File open problem"), MB_OK|MB_APPLMODAL); */ fclose(fp); return false; } //Setup scratchtilla for new filedata _pscratchTilla->execute(SCI_SETSTATUS, SC_STATUS_OK); // reset error status _pscratchTilla->execute(SCI_SETDOCPOINTER, 0, doc); bool ro = _pscratchTilla->execute(SCI_GETREADONLY) != 0; if (ro) { _pscratchTilla->execute(SCI_SETREADONLY, false); } _pscratchTilla->execute(SCI_CLEARALL); if (language < L_EXTERNAL) { _pscratchTilla->execute(SCI_SETLEXER, ScintillaEditView::langNames[language].lexerID); } else { int id = language - L_EXTERNAL; TCHAR * name = NppParameters::getInstance()->getELCFromIndex(id)._name; WcharMbcsConvertor *wmc = WcharMbcsConvertor::getInstance(); const char *pName = wmc->wchar2char(name, CP_ACP); _pscratchTilla->execute(SCI_SETLEXERLANGUAGE, 0, (LPARAM)pName); } if (encoding != -1) _pscratchTilla->execute(SCI_SETCODEPAGE, SC_CP_UTF8); bool success = true; FormatType format = FormatType::unknown; __try { // First allocate enough memory for the whole file (this will reduce memory copy during loading) _pscratchTilla->execute(SCI_ALLOCATE, WPARAM(bufferSizeRequested)); if (_pscratchTilla->execute(SCI_GETSTATUS) != SC_STATUS_OK) throw; size_t lenFile = 0; size_t lenConvert = 0; //just in case conversion results in 0, but file not empty bool isFirstTime = true; int incompleteMultibyteChar = 0; do { lenFile = fread(data+incompleteMultibyteChar, 1, blockSize-incompleteMultibyteChar, fp) + incompleteMultibyteChar; if (lenFile == 0) break; if (isFirstTime) { // check if file contain any BOM if (Utf8_16_Read::determineEncoding((unsigned char *)data, lenFile) != uni8Bit) { // if file contains any BOM, then encoding will be erased, // and the document will be interpreted as UTF encoding = -1; } else if (encoding == -1) { if (NppParameters::getInstance()->getNppGUI()._detectEncoding) encoding = detectCodepage(data, lenFile); } if (language == L_TEXT) { // check the language du fichier language = detectLanguageFromTextBegining((unsigned char *)data, lenFile); } isFirstTime = false; } if (encoding != -1) { if (encoding == SC_CP_UTF8) { // Pass through UTF-8 (this does not check validity of characters, thus inserting a multi-byte character in two halfs is working) _pscratchTilla->execute(SCI_APPENDTEXT, lenFile, (LPARAM)data); } else { WcharMbcsConvertor* wmc = WcharMbcsConvertor::getInstance(); int newDataLen = 0; const char *newData = wmc->encode(encoding, SC_CP_UTF8, data, lenFile, &newDataLen, &incompleteMultibyteChar); _pscratchTilla->execute(SCI_APPENDTEXT, newDataLen, (LPARAM)newData); } if (format == FormatType::unknown) format = getEOLFormatForm(data, lenFile, FormatType::unknown); } else { lenConvert = UnicodeConvertor->convert(data, lenFile); _pscratchTilla->execute(SCI_APPENDTEXT, lenConvert, (LPARAM)(UnicodeConvertor->getNewBuf())); } if (_pscratchTilla->execute(SCI_GETSTATUS) != SC_STATUS_OK) throw; if (incompleteMultibyteChar != 0) { // copy bytes to next buffer memcpy(data, data+blockSize-incompleteMultibyteChar, incompleteMultibyteChar); } } while (lenFile > 0); } __except(EXCEPTION_EXECUTE_HANDLER) //TODO: should filter correctly for other exceptions; the old filter(GetExceptionCode(), GetExceptionInformation()) was only catching access violations { ::MessageBox(NULL, TEXT("File is too big to be opened by Notepad++"), TEXT("File open problem"), MB_OK|MB_APPLMODAL); success = false; } fclose(fp); // broadcast the format if (pFormat != nullptr) *pFormat = (format != FormatType::unknown) ? format : FormatType::osdefault; _pscratchTilla->execute(SCI_EMPTYUNDOBUFFER); _pscratchTilla->execute(SCI_SETSAVEPOINT); if (ro) _pscratchTilla->execute(SCI_SETREADONLY, true); _pscratchTilla->execute(SCI_SETDOCPOINTER, 0, _scratchDocDefault); return success; }
bool TiXmlDocument::LoadFile( const TCHAR* filename ) { // Delete the existing data: Clear(); location.Clear(); // There was a really terrifying little bug here. The code: // value = filename // in the STL case, cause the assignment method of the std::generic_string to // be called. What is strange, is that the std::generic_string had the same // address as it's c_str() method, and so bad things happen. Looks // like a bug in the Microsoft STL implementation. // See STL_STRING_BUG above. // Fixed with the StringToBuffer class. value = filename; FILE* file = generic_fopen( value.c_str (), TEXT("r") ); if ( file ) { // Get the file size, so we can pre-allocate the generic_string. HUGE speed impact. long length = 0; fseek( file, 0, SEEK_END ); length = ftell( file ); fseek( file, 0, SEEK_SET ); // Strange case, but good to handle up front. if ( length == 0 ) { fclose( file ); return false; } // If we have a file, assume it is all one big XML file, and read it in. // The document parser may decide the document ends sooner than the entire file, however. TIXML_STRING data; data.reserve( length ); const int BUF_SIZE = 2048; TCHAR buf[BUF_SIZE]; while( generic_fgets( buf, BUF_SIZE, file ) ) { data += buf; } fclose( file ); //input is in UTF-8, so transformation is needed to UTF-16 used by windows for TCHAR in unicode mode std::vector<char> inputdataInUTF8(data.size()+1); //+1 for the null termination size_t datalength = wcstombs(inputdataInUTF8.data(), data.c_str(), data.size()); int transformedDataCharCount = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)inputdataInUTF8.data(), -1, nullptr, 0); std::vector<wchar_t> transformedData(transformedDataCharCount+1); //+1 for the null termination transformedDataCharCount = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)inputdataInUTF8.data(), -1, transformedData.data(), transformedDataCharCount); if(transformedDataCharCount > 0) { //replace the original data with the new tranformed one, on success ot transformation otherwise go with old style data data.clear(); data = transformedData.data(); } Parse( data.c_str(), 0 ); if ( Error() ) return false; else return true; } SetError( TIXML_ERROR_OPENING_FILE, 0, 0 ); return false; }
mame_file *mame_fopen(const char *gamename, const char *filename, int filetype, int openforwrite) { /* first verify that we aren't trying to open read-only types as writeables */ switch (filetype) { /* read-only cases */ case FILETYPE_ROM: #ifndef MESS case FILETYPE_IMAGE: #endif case FILETYPE_SAMPLE: case FILETYPE_HIGHSCORE_DB: case FILETYPE_ARTWORK: case FILETYPE_HISTORY: case FILETYPE_LANGUAGE: case FILETYPE_INI: if (openforwrite) { logerror("mame_fopen: type %02x write not supported\n", filetype); return NULL; } break; /* write-only cases */ case FILETYPE_SCREENSHOT: #ifdef PINMAME case FILETYPE_WAVE: #endif /* PINMAME */ if (!openforwrite) { logerror("mame_fopen: type %02x read not supported\n", filetype); return NULL; } break; } /* now open the file appropriately */ switch (filetype) { /* ROM files */ case FILETYPE_ROM: return generic_fopen(filetype, gamename, filename, 0, FILEFLAG_OPENREAD | FILEFLAG_HASH); /* read-only disk images */ case FILETYPE_IMAGE: return generic_fopen(filetype, gamename, filename, 0, FILEFLAG_OPENREAD | FILEFLAG_NOZIP); /* differencing disk images */ case FILETYPE_IMAGE_DIFF: return generic_fopen(filetype, gamename, filename, 0, FILEFLAG_OPENREAD | FILEFLAG_OPENWRITE); /* samples */ case FILETYPE_SAMPLE: return generic_fopen(filetype, gamename, filename, 0, FILEFLAG_OPENREAD); /* artwork files */ case FILETYPE_ARTWORK: return generic_fopen(filetype, gamename, filename, 0, FILEFLAG_OPENREAD); /* NVRAM files */ case FILETYPE_NVRAM: return generic_fopen(filetype, NULL, gamename, 0, openforwrite ? FILEFLAG_OPENWRITE : FILEFLAG_OPENREAD); /* high score files */ case FILETYPE_HIGHSCORE: if (!mame_highscore_enabled()) return NULL; return generic_fopen(filetype, NULL, gamename, 0, openforwrite ? FILEFLAG_OPENWRITE : FILEFLAG_OPENREAD); /* highscore database */ case FILETYPE_HIGHSCORE_DB: return generic_fopen(filetype, NULL, filename, 0, FILEFLAG_OPENREAD); /* config files */ case FILETYPE_CONFIG: return generic_fopen(filetype, NULL, gamename, 0, openforwrite ? FILEFLAG_OPENWRITE : FILEFLAG_OPENREAD); /* input logs */ case FILETYPE_INPUTLOG: return generic_fopen(filetype, NULL, gamename, 0, openforwrite ? FILEFLAG_OPENWRITE : FILEFLAG_OPENREAD); /* save state files */ case FILETYPE_STATE: return generic_fopen(filetype, NULL, filename, 0, openforwrite ? FILEFLAG_OPENWRITE : FILEFLAG_OPENREAD); /* memory card files */ case FILETYPE_MEMCARD: return generic_fopen(filetype, NULL, filename, 0, openforwrite ? FILEFLAG_OPENWRITE : FILEFLAG_OPENREAD); /* screenshot files */ case FILETYPE_SCREENSHOT: #ifdef PINMAME case FILETYPE_WAVE: #endif /* PINMAME */ return generic_fopen(filetype, NULL, filename, 0, FILEFLAG_OPENWRITE); /* history files */ case FILETYPE_HISTORY: return generic_fopen(filetype, NULL, filename, 0, FILEFLAG_OPENREAD); /* cheat file */ case FILETYPE_CHEAT: return generic_fopen(filetype, NULL, filename, 0, FILEFLAG_OPENREAD | (openforwrite ? FILEFLAG_OPENWRITE : 0)); /* language file */ case FILETYPE_LANGUAGE: return generic_fopen(filetype, NULL, filename, 0, FILEFLAG_OPENREAD); /* ctrlr files */ case FILETYPE_CTRLR: return generic_fopen(filetype, gamename, filename, 0, openforwrite ? FILEFLAG_OPENWRITE : FILEFLAG_OPENREAD); /* game specific ini files */ case FILETYPE_INI: return generic_fopen(filetype, NULL, gamename, 0, FILEFLAG_OPENREAD); /* anything else */ default: logerror("mame_fopen(): unknown filetype %02x\n", filetype); return NULL; } return NULL; }
/* Similar to mame_fopen(,,FILETYPE_ROM), but lets you specify an expected checksum (better encapsulation of the load by CRC used for ZIP files) */ mame_file *mame_fopen_rom(const char *gamename, const char *filename, const char* exphash) { return generic_fopen(FILETYPE_ROM, gamename, filename, exphash, FILEFLAG_OPENREAD | FILEFLAG_HASH); }