// Initializes the board by getting the puzzle from a csv file. // 0 means empty (an example of a row: 0,0,1,0,0,2,0,0,3) void Board::read(string filename) { std::ifstream inFile(filename.c_str(), std::ios::in); // If the file does not exist, throw an error. if (!inFile) throw NoInputFile(); // Temporary strings to read input. string temp; vector<string> lines; // Read all lines first and store them in an array, while (getline(inFile, temp)) lines.push_back(temp); if (lines.size() != size) throw BadInput(); // Wrong row counts. // For each line, extract values using string streams and set values. for (short i = 0; i < size; i++) { std::istringstream ss(lines[i]); vector<unsigned char> values; while (getline(ss, temp, ',')) values.push_back(std::atoi(temp.c_str())); // Check for wrong col counts. if (values.size() != size) throw BadInput(); for (short j = 0; j < size; j++) { if (values[j] < 0 || values[j] > size) throw InvalidPuzzle(); // Bad value is given. set(i, j, values[j]); }; }; inFile.close(); // Check for the validity of the given puzzle. if (!ifValid()) throw InvalidPuzzle(); };
std::istream& ZipFileInput::openFile(const stromx::runtime::InputProvider::OpenMode mode) { if(! m_initialized) throw WrongState("Zip file input has not been initialized."); if(m_currentFile) throw WrongState("File has already been opened."); if(! hasFile()) throw NoInputFile(); std::ios_base::openmode iosmode = std::ios_base::in; if(mode == BINARY) iosmode |= std::ios_base::binary; struct zip_stat stat; if(zip_stat(m_archiveHandle, m_currentFilename.c_str(), 0, &stat) < 0) throw FileAccessFailed(m_currentFilename, m_archive, "Failed to access file in zip archive."); if(stat.size == 0) throw FileAccessFailed(m_currentFilename, m_archive, "File in zip archive has zero size."); unsigned int fileSize = (unsigned int)(stat.size); std::vector<char> content(fileSize); zip_file* file = zip_fopen(m_archiveHandle, m_currentFilename.c_str(), 0); if(! file) throw FileAccessFailed(m_currentFilename, m_archive, "Failed to open file in zip archive."); if((unsigned int)(zip_fread(file, &content[0], fileSize)) != fileSize) throw FileAccessFailed(m_currentFilename, m_archive, "Failed to read file in zip archive."); m_currentFile = new std::istringstream(std::string(&content[0], fileSize), iosmode); return *m_currentFile; }