📄 ysfile.cpp
字号:
#include "YsFile.h"static YsString g_currDir;/////////////////////////////////////////////////////////////////////////////// YsFileYsFile::YsFile(){ m_fp = 0;}YsFile::~YsFile(){ close();}bool YsFile::close(){ if(m_fp) { fclose(m_fp); m_fp = 0; } return true;}bool YsFile::open(OpenMode mode){ if(m_fp) { return false; } YsString m=""; if(mode == IO_Append) m="w+"; else if(mode == IO_ReadOnly) m="r"; else if(mode == IO_ReadWrite) m= "ar+"; else if(mode == IO_Translate) m= "rt"; else if(mode == IO_Truncate) m="w"; else if(mode == IO_WriteOnly) m="w"; m_fp = fopen(m_fileName.data(), m.data()); if(m_fp == 0) return false; return true;}bool YsFile::remove(){ return remove(m_fileName.data()) == 0;}bool YsFile::rename(YsString newName){ return rename(m_fileName, newName) == 0;}long YsFile::size(){ YsFile file(m_fileName); if(!file.open(IO_ReadOnly)) return -1; fseek(file.getHandle(),0,SEEK_END); long len=ftell(file.getHandle()); file.close(); return len; }bool YsFile::exists(){ return exists(m_fileName.data());}bool YsFile::copy(YsString newFile){ newFile=newFile;return true;}long YsFile::readBlock(BYTE *pBuff, int maxSize){ if(m_fp == 0) return -1; return fread(pBuff,1, maxSize,m_fp);}long YsFile::writeBlock(BYTE *pBuff, int size){ if(m_fp == 0) return -1; return fwrite(pBuff,1, size,m_fp);}long YsFile::readLine(char *pBuff, int maxSize){ if(m_fp == 0) return -1; for(int i=0; i<maxSize-1; i++) { fread(pBuff+i,1,1,m_fp); if(pBuff[i] == '\n' || feof(m_fp)) { pBuff[i+1] = '\0'; return i+1; } }return -1;}int YsFile::at(int pos){ if(m_fp == 0) return -1; return fseek(m_fp, SEEK_SET, pos);}void YsFile::setName(YsString name){ m_fileName = name; if(name.left(1) != "/") { if(g_currDir.right(1) != "/") g_currDir += "/"; m_fileName = g_currDir+m_fileName; }}///staticbool YsFile::copy(YsString fileName, YsString newFile){ YsFile f1(fileName); YsFile f2(newFile); if(!f1.open(IO_ReadOnly) || !f2.open(IO_WriteOnly)) { f1.close(); f2.close(); return false; } BYTE buf[1025]; while(1) { int ret = f1.readBlock(buf,1025); if(ret <= 0) break; f2.writeBlock(buf,ret); } f1.close(); f2.close(); return true;}bool YsFile::exists(YsString fileName){ YsFile f(fileName); bool b=f.open(IO_ReadOnly); f.close(); return b;}bool YsFile::remove(YsString fileName){ return ::remove(fileName.data()) == 0;}bool YsFile::rename(YsString fileName,YsString newFile){ return ::rename(fileName.data(),newFile.data()) == 0;}//////////////////////////////////////////////////////////////////////////////////////////////////////YsTextStreamYsTextStream::YsTextStream(YsFile *pFile){ m_pFile = pFile;}YsTextStream::~YsTextStream(){}//operatorYsTextStream& YsTextStream::operator<<(const YsString &str){ m_pFile->write(str.data(),str.length()); return *this;}YsTextStream& YsTextStream::operator<<(const char* pStr){ YsString str=pStr; m_pFile->write(str.data(),str.length()); return *this;}YsString YsTextStream::readLine(){ return m_pFile->readLine();}bool YsTextStream::atEnd(){ return m_pFile->atEnd();}////////////////////////////////////////////////////////////////////////////////////// YsDirbool YsDir::rename(YsString oldName, YsString newName){ if(m_sDirName.length() > 0 && oldName.left(1) != "/") { oldName = m_sDirName+"/"+oldName; newName = m_sDirName+"/"+newName; } return ::rename(oldName.data(),newName.data()) == 0;}bool YsDir::remove(YsString name){ return YsFile::remove(m_sDirName+"/"+name);}bool YsDir::exists(YsString name){ return YsFile::exists(m_sDirName+"/"+name);}YsString YsDir::currentPath(){ YsString str; char currentName[PATH_MAX+1]; if(getcwd(currentName,PATH_MAX)) str = currentName; else str = ""; return str;}void YsDir::setCurrent(YsString dir){ g_currDir = dir;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -