Added support for MString in File
This commit is contained in:
parent
2a349c6ceb
commit
88d51cb598
25
File.cpp
25
File.cpp
@ -46,9 +46,28 @@ namespace coreutils {
|
|||||||
this->size = size;
|
this->size = size;
|
||||||
}
|
}
|
||||||
|
|
||||||
void File::read() {
|
int File::read() {
|
||||||
size = ::read(fd, buffer, size);
|
size = ::read(fd, buffer, size);
|
||||||
setBufferSize(size);
|
setBufferSize(size);
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
int File::readLine() {
|
||||||
|
setBufferSize(4096);
|
||||||
|
size = 0;
|
||||||
|
int len = ::read(fd, buffer + size, 1);
|
||||||
|
if(len == 0)
|
||||||
|
mEof = true;
|
||||||
|
while(!mEof && (*(buffer + size) != '\n')) {
|
||||||
|
if(*(buffer + size) != '\r') {
|
||||||
|
// setBufferSize(size);
|
||||||
|
++size;
|
||||||
|
}
|
||||||
|
len = ::read(fd, buffer + size, 1);
|
||||||
|
if(len == 0)
|
||||||
|
mEof = true;
|
||||||
|
}
|
||||||
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
void File::write(std::string data) {
|
void File::write(std::string data) {
|
||||||
@ -68,4 +87,8 @@ namespace coreutils {
|
|||||||
return zstring;
|
return zstring;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool File::eof() {
|
||||||
|
return mEof;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
5
File.h
5
File.h
@ -20,13 +20,15 @@ namespace coreutils {
|
|||||||
File(ZString &fileName, int mode = O_RDONLY, int authority = 0664);
|
File(ZString &fileName, int mode = O_RDONLY, int authority = 0664);
|
||||||
~File();
|
~File();
|
||||||
void setBufferSize(size_t size);
|
void setBufferSize(size_t size);
|
||||||
void read();
|
int read();
|
||||||
|
int readLine();
|
||||||
void write(std::string data);
|
void write(std::string data);
|
||||||
void write(coreutils::ZString &data);
|
void write(coreutils::ZString &data);
|
||||||
std::string asString();
|
std::string asString();
|
||||||
coreutils::ZString& asZString();
|
coreutils::ZString& asZString();
|
||||||
char *buffer;
|
char *buffer;
|
||||||
size_t size;
|
size_t size;
|
||||||
|
bool eof();
|
||||||
|
|
||||||
std::string fileName;
|
std::string fileName;
|
||||||
|
|
||||||
@ -34,6 +36,7 @@ namespace coreutils {
|
|||||||
void open(std::string fileName, int mode, int authority);
|
void open(std::string fileName, int mode, int authority);
|
||||||
int fd;
|
int fd;
|
||||||
coreutils::ZString zstring;
|
coreutils::ZString zstring;
|
||||||
|
bool mEof = false;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
14
MString.cpp
14
MString.cpp
@ -56,7 +56,7 @@ namespace coreutils {
|
|||||||
|
|
||||||
MString::~MString() {
|
MString::~MString() {
|
||||||
if(data)
|
if(data)
|
||||||
free(data);
|
free(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
// MString& MString::operator=(coreutils::ZString& value) {
|
// MString& MString::operator=(coreutils::ZString& value) {
|
||||||
@ -77,12 +77,12 @@ namespace coreutils {
|
|||||||
// return *this;
|
// return *this;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
MString& MString::operator=(coreutils::ZString value) {
|
MString& MString::operator=(coreutils::ZString &value) {
|
||||||
if(*this == value)
|
// if(*this == value)
|
||||||
return *this;
|
// return *this;
|
||||||
int len = length;
|
setSize(value.getLength());
|
||||||
setSize(length + value.getLength());
|
memcpy(data, value.getData(), value.getLength());
|
||||||
memcpy(data + len, value.getData(), value.getLength());
|
length = value.getLength();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,7 +72,7 @@ namespace coreutils {
|
|||||||
|
|
||||||
// MString& operator=(coreutils::ZString& data);
|
// MString& operator=(coreutils::ZString& data);
|
||||||
|
|
||||||
MString& operator=(coreutils::ZString data);
|
MString& operator=(coreutils::ZString &data);
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Assignment operator will copy data to backing store.
|
/// Assignment operator will copy data to backing store.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user