/** * $Id: mfile.cpp,v 1.6 2002/10/21 08:37:21 plasmahh Exp $ * $Revision: 1.6 $ * $Author: plasmahh $ * $Date: 2002/10/21 08:37:21 $ */ #include "mfile.h" mfile::mfile( const mstring& ne ) : fd(0)/*{{{*/ { char * cwd; name = ne; if (! name.matches("/")) // if filenam does not begin with / we have a relative one (only file in cwd, or ../ before it, or even not a valid one) set cwd in front, perhaps we change directory { cwd = new char[4096]; getcwd(cwd, 4096); name = cwd + mstring("/") + name; delete[] cwd; } }/*}}}*/ BOOL mfile::Open( sint32 oflag )/*{{{*/ { fd = open(name, oflag); return ( fd > 0); }/*}}}*/ BOOL mfile::Close( void )/*{{{*/ { fd = close(fd); return (fd >= 0); fd = -1; }/*}}}*/ mstring mfile::ReadLine( void )/*{{{*/ { uint32 opos = lseek(fd, 0L, SEEK_CUR); mstring line; uchar8 * buffer; buffer = new uchar8[2049]; // most lines will not be greater than 2048 bytes sint32 byt; byt = read ( fd, buffer, 2048); if ( byt <= 0 ) { delete [] buffer; return line; } buffer[byt] = '\0'; register uint32 i=2048; while (i == 2048) { for (i=0; i < 2048;i++) { if ( buffer[i] == '\n' || buffer[i] == '\0') { buffer[i] = '\0'; break; } } if ( i == 2048) { line += buffer; } opos+=i; } line += buffer; lseek(fd, opos + 1 , SEEK_SET); delete[] buffer; return line; }/*}}}*/ mstring mfile::Read( uint32 bytes )/*{{{*/ { uint32 opos = lseek(fd, 0L, SEEK_CUR); mstring line; sint32 byt; uchar8 * buffer; buffer = new uchar8[bytes + 1]; // most lines will not be greater than 2048 bytes byt = read ( fd, buffer, bytes); if ( byt <= 0) { delete[] buffer; return line; } buffer[ byt ] = '\0'; register uint32 i=bytes; for (i=0; i < bytes;i++) { if ( buffer[i] == '\0') { break; } } line += buffer; opos += i; lseek(fd, opos + 1 , SEEK_SET); delete [] buffer; return line; }/*}}}*/ BOOL mfile::Eof( void )/*{{{*/ { // try to read 1 byte, if we can't we are at EOF // reset file pointer afterwards uchar8 dummy[1]; BOOL eof; sint32 bytes; bytes = read( fd, &dummy, 1); eof = (bytes != 1); lseek(fd, -1L, SEEK_CUR); return eof; }/*}}}*/ /** *$Log: mfile.cpp,v $ *Revision 1.6 2002/10/21 08:37:21 plasmahh *beginning cleanup for smtpmap release 0.8-stable * *Revision 1.5 2002/05/23 19:48:08 plasmahh *removed some compiler warnings. *fixed one or two bugs. *refined include structure, for faster compiling *hm, what else ? don't know, I hope this works all ;) * *Revision 1.4 2002/05/15 07:01:54 plasmahh *Fixed memory leak. * *Revision 1.3 2002/05/14 16:31:13 plasmahh *Removed some compiler warnings *Added more code, hopefully fast ;) * *Revision 1.2 2002/05/13 21:32:57 plasmahh *implemented file class & readline *implemented additional in mstring * *Revision 1.1 2002/05/13 14:33:23 plasmahh *added file class * */