long long TCPTransport::recvfile(std::fstream& ofs, long long offset, long long size) { if (!m_bConnected) return -1; if (ofs.bad() || ofs.fail()) return -1; ofs.seekp(offset); int block = 1000000; char* buf = new char[block]; long long recd = 0; while (recd < size) { int unit = int((size - recd) > block ? block : size - recd); recv(buf, unit); ofs.write(buf, unit); recd += unit; } delete [] buf; return recd; }
long long TCPTransport::sendfile(std::fstream& ifs, long long offset, long long size) { if (!m_bConnected) return -1; if (ifs.bad() || ifs.fail()) return -1; ifs.seekg(offset); int block = 1000000; char* buf = new char[block]; long long sent = 0; while (sent < size) { int unit = int((size - sent) > block ? block : size - sent); ifs.read(buf, unit); send(buf, unit); sent += unit; } delete [] buf; return sent; }
static int getImageCount(std::fstream& file_list) { int sample = 0; std::string line; while (std::getline(file_list, line)) ++sample; if (file_list.eof()) file_list.clear(); //otherwise we can't do any further I/O else if (file_list.bad()) { throw std::runtime_error("Error occured while reading files_list.txt"); } return sample; }
/** * Sets the stream position indicator for the stream. The new position, * measured in bytes, is obtained by adding offset bytes to the position * specified by whence. If whence is set to SEEK_SET, SEEK_CUR, or * SEEK_END, the offset is relative to the start of the file, the current * position indicator, or end-of-file, respectively. A successful call * to the seek() method clears the end-of-file indicator for the stream. * * @note The semantics of any implementation of this method are * supposed to match those of ISO C fseek(). * * @param offset the relative offset in bytes * @param whence the seek reference: SEEK_SET, SEEK_CUR, or SEEK_END * @return true on success, false in case of a failure */ virtual bool seek(int32 offset, int whence = SEEK_SET) { if (whence == SEEK_SET) { _stream->seekg(offset, std::ios::beg); } else if (whence == SEEK_CUR) { _stream->seekg(offset, std::ios::cur); } else { _stream->seekg(offset, std::ios::end); } if (_stream->fail()) return false; if (_stream->bad()) return false; return true; }
void Agente::leerLinea(std::fstream& archivo, std::string& linea) { linea.clear(); char buffer[T_BUFFER]; bool seguirLeyendo = true; do { archivo.getline(buffer, T_BUFFER, '\n'); linea.append(buffer, archivo.gcount()); seguirLeyendo = (archivo.gcount() == T_BUFFER); if (archivo.bad()) archivo.clear(); } while (seguirLeyendo); }
//------------------------------------------------------------------------------ /// \brief Reads 3. Data: ITMP /// \brief Reads 4. Data: WELLID Qdes {CapMult} {Cprime} {xyz} //------------------------------------------------------------------------------ void ProcessorMNW2::impl::ReadStressPeriods (std::fstream& a_is, std::fstream& a_os) { CStr str; std::string line; EReadAsciiFile e; e.UseExceptions(); while (!a_is.bad()) { std::getline(a_is, line); if (line.empty()) { return; } int ITMP; a_os << line << "\n"; e.SetLine(line.c_str()); e.ReadData(ITMP); if (ITMP < 1) continue; std::getline(a_is, line); e.SetLine(line.c_str()); e.ReadData(str); if ("GMS_HDF5_01" == str) { CStr fname, path; int sp; e.ReadData(fname); e.ReadData(path); path += "/07. Property"; e.ReadData(sp); std::pair<int, int> myPair(0,1); VEC_INT_PAIR indices(3, myPair); indices[0].second = numMnw2Prop(); indices[1].second = m_MNWMAX; indices[2].first = sp - 1; H5DataSetReader r(fname, path, indices); CAR_DBL2D vals; vals.SetSize(MNW2::NPROP, m_MNWMAX, 0); if (!r.GetData(&vals[0][0], MNW2::NPROP*m_MNWMAX)) { ErrorStack::Get().PutError("Unable to read MNW2 hdf5 data."); throw ioError(""); } for (int i = 0; i<m_MNWMAX; i++) { if (vals.at(MNW2::ACTIVE, i) != 0) { // this well is active // 4a. Data: WELLID Qdes {CapMult} {Cprime} {xyz} CStr ln; ln.Format("'%s' %s", m_wells.at(i).m_WELLID, STR(vals.at(MNW2::QDES, i))); if (m_wells.at(i).m_PUMPCAP > 0) { ln += " "; ln += STR(vals.at(MNW2::CAPMULT, i)); } // don't write Cprime // get AUX for (int j = 0; j<m_nAUX; j++) { ln += " "; ln += STR(vals.at(MNW2::AUX_0+j, i)); } a_os << ln << "\n"; if (m_wells.at(i).m_Qlimit < 0) { double QCUT = vals.at(MNW2::QCUT, i); ln.Format("%s %s", STR(vals.at(MNW2::HLIM, i)), STR(QCUT)); if (QCUT != 0) { CStr ln2; ln2.Format(" %s %s", STR(vals.at(MNW2::QFRCMN, i)), STR(vals.at(MNW2::QFRCMX, i))); ln += ln2; } a_os << ln << "\n"; } } } } } } // ProcessorMNW2::impl::ReadStressPeriods