/** Create new DataFile, or return existing DataFile. */ DataFile* DataFileList::AddDataFile(FileName const& nameIn, ArgList& argIn, DataFile::DataFormatType typeIn) { // If no filename, no output desired if (nameIn.empty()) return 0; FileName fname( nameIn ); // Append ensemble number if set. //rprintf("DEBUG: Setting up data file '%s' with ensembleNum %i\n", nameIn.base(), ensembleNum_); if (ensembleNum_ != -1) fname.Append( "." + integerToString(ensembleNum_) ); // Check if filename in use by CpptrajFile. CpptrajFile* cf = GetCpptrajFile(fname); if (cf != 0) { mprinterr("Error: Data file name '%s' already in use by text output file '%s'.\n", fname.full(), cf->Filename().full()); return 0; } // Check if this filename already in use DataFile* Current = GetDataFile(fname); // If no DataFile associated with name, create new DataFile if (Current==0) { Current = new DataFile(); if (Current->SetupDatafile(fname, argIn, typeIn, debug_)) { mprinterr("Error: Setting up data file %s\n", fname.full()); delete Current; return 0; } fileList_.push_back(Current); } else { // Set debug level Current->SetDebug(debug_); // If a type was specified, make sure it matches. if (typeIn != DataFile::UNKNOWN_DATA && typeIn != Current->Type()) { mprinterr("Error: '%s' is type %s but has been requested as type %s.\n", Current->DataFilename().full(), Current->FormatString(), DataFile::FormatString( typeIn )); return 0; } // Check for keywords that do not match file type DataFile::DataFormatType kType = DataFile::GetFormatFromArg( argIn ); if (kType != DataFile::UNKNOWN_DATA && kType != Current->Type()) mprintf("Warning: %s is type %s but type %s keyword specified; ignoring keyword.\n", Current->DataFilename().full(), Current->FormatString(), DataFile::FormatString( kType )); // Process Arguments if (!argIn.empty()) Current->ProcessArgs( argIn ); } return Current; }
Exec::RetType Exec_ReadData::Execute(CpptrajState& State, ArgList& argIn) { DataFile dataIn; dataIn.SetDebug( State.DFL().Debug() ); std::string filenameIn = argIn.GetStringNext(); File::NameArray fnames = File::ExpandToFilenames( filenameIn ); if (fnames.empty()) { mprinterr("Error: '%s' matches no files.\n", filenameIn.c_str()); return CpptrajState::ERR; } int err = 0; int idx = -1; bool useIndex = argIn.hasKey("separate"); for (File::NameArray::const_iterator fn = fnames.begin(); fn != fnames.end(); ++fn) { if (useIndex) idx++; if (dataIn.ReadDataIn( *fn, argIn, State.DSL(), idx, fnames.size() )!=0) { mprinterr("Error: Could not read data file '%s'.\n", fn->full()); err++; } } if (err > 0) return CpptrajState::ERR; return CpptrajState::OK; }