//------------------------------------------------------------------------------ // copyData() -- copy member data //------------------------------------------------------------------------------ void FileWriter::copyData(const FileWriter& org, const bool cc) { BaseClass::copyData(org); if (cc) initData(); setFilename(org.filename); setPathName(org.pathname); // Need to re-open the file if (sout != nullptr) { if (isOpen()) sout->close(); delete sout; } sout = nullptr; fileOpened = false; fileFailed = false; eodFlag = false; setFullFilename(nullptr); }
//------------------------------------------------------------------------------ // Open the data file //------------------------------------------------------------------------------ bool PrintHandler::openFile() { // When we're already open, just return if (isOpen()) return true; // If we don't have a file name then we're using the standard output if (filename == 0 || filename->len() == 0) return true; // clear the old 'full' file name setFullFilename(0); // local flags (default is success) bool tOpened = true; bool tFailed = false; //--- // Allocate space for the full file name //--- size_t nameLength = 0; if (pathname != 0) { nameLength += pathname->len(); // add the length of the path name nameLength += 1; // add a character for the slash } nameLength += filename->len(); // add the length of the file name nameLength += 4; // add characters for possible version number, "_V99" nameLength += 1; // Add one for the null(0) at the end of the string char* fullname = new char[nameLength]; fullname[0] = '\0'; //--- // Create the (initial) full file name //--- if (pathname != 0 && pathname->len() > 0) { lcStrcat(fullname, nameLength ,*pathname); lcStrcat(fullname, nameLength, "/"); } lcStrcat(fullname,nameLength,*filename); //--- // Make sure that it doesn't already exist (we don't want to over write good data). //--- bool validName = !doesFileExist(fullname); if ( !validName ) { // If the file already exists, try appending a version number "v99" .. char* origname = new char[nameLength]; lcStrcpy(origname, nameLength, fullname); validName = false; for (unsigned int i = 1; i <= 99 && !validName; i++) { std::sprintf(fullname, "%s_v%02d", origname, i); validName = !doesFileExist(fullname); } if ( !validName ) { if (isMessageEnabled(MSG_ERROR)) { std::cerr << "PrintHandler::openFile(): All version of the data file already exists: " << origname << std::endl; } tOpened = false; tFailed = true; } delete[] origname; } //--- // When we have a valid file name ... //--- if ( validName ) { // The file name with the path and version number setFullFilename(fullname); //--- // Make sure we have an output stream //--- if (sout == 0) sout = new std::ofstream(); //--- // Open the file //--- sout->open(fullname, std::ios_base::out); if (isMessageEnabled(MSG_INFO)) { std::cout << "PrintHandler::openFile() Opening data file = " << fullname << std::endl; } if (sout->fail()) { if (isMessageEnabled(MSG_ERROR)) { std::cerr << "PrintHandler::openFile(): Failed to open data file: " << fullname << std::endl; } tOpened = false; tFailed = true; } } delete[] fullname; fileOpened = tOpened; fileFailed = tFailed; return fileOpened; }