/* PIP - Platform Independent Primitives File Copyright (C) 2013 Ivan Pelipenko peri4ko@gmail.com This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "pifile.h" bool PIFile::openDevice() { if (opened_) close(); if (path_.isEmpty()) return false; //cout << "fopen " << path_.data() << ": " << strType(mode_).data() << endl; fd = fopen(path_.data(), strType(mode_).data()); opened_ = (fd != 0); seekToBegin(); return opened_; } bool PIFile::closeDevice() { if (!opened_ || fd == 0) return true; return (fclose(fd) == 0); } PIString PIFile::readLine() { PIString str; if (!opened_) return str; int cc, cp = 0; while (!isEnd() && cp < 4095) { cc = fgetc(fd); if (char(cc) == '\n' || cc == EOF) break; str.push_back(char(cc)); } //cout << "readline: " << str << endl; return str; } llong PIFile::readAll(void * data) { llong cp = pos(), s = size(), i = -1; seekToBegin(); if (s < 0) { while (!isEnd()) read(&(((char*)data)[++i]), 1); } else read((char * )data, s); seek(cp); return s; } PIByteArray PIFile::readAll(bool forceRead) { PIByteArray a; llong cp = pos(); if (forceRead) { seekToBegin(); while (!isEnd()) a.push_back(readChar()); seek(cp); return a; } llong s = size(); if (s < 0) return a; a.resize(s); s = readAll(a.data()); seek(cp); if (s >= 0) a.resize(s); return a; } llong PIFile::size() { if (!opened_) return -1; llong s, cp = pos(); seekToEnd(); s = pos(); seek(cp); return s; } void PIFile::resize(llong new_size, char fill_) { llong ds = new_size - size(); if (ds == 0) return; if (ds > 0) { char * buff = new char[ds]; memset(buff, fill_, ds); write(buff, ds); delete[] buff; return; } piCoutObj << "[PIFile] Downsize is not support yet :-("; } bool PIFile::isExists(const PIString & path) { FILE * f = fopen(PIString(path).data(), "r"); bool ok = (f != 0); if (ok) fclose(f); return ok; }