home *** CD-ROM | disk | FTP | other *** search
- #include "File.h"
-
- #include "Tokenizer.h"
-
-
- File::File(){
- filename=NULL;
- file=NULL;
- line=0;
- }
-
- File::File(const char* filename, const char* mode){
- this->filename=NULL;
- file=NULL;
- line=0;
-
- open(filename, mode);
- }
-
- File::~File(){
- if(file!=NULL){
- close();
- }
- }
-
- bool File::open(const char* filename, const char* mode){
- if(file!=NULL){
- close();
- }
-
- file=fopen(filename, mode);
- if(file!=NULL){
- this->filename=newString(filename);
- line=0;
- return true;
- }else{
- return false;
- }
- }
-
- void File::close(){
- if(file!=NULL){
- fclose(file);
- file = NULL;
- delete[] filename;
- filename=NULL;
- line=0;
- }
- }
-
- bool File::isOpen(){
- return (file!=NULL);
- }
-
- int File::readLine(int numCharsToRead, char* buff, bool stripComments/*=false*/){
- if(file==NULL)
- return -1;
-
- if(fgets(buff, numCharsToRead, file)){
- for(int i=0;i<(signed int)strlen(buff);i++){
- if(buff[i]=='\r' || buff[i]=='\n'
- || (stripComments && buff[i]=='/' && buff[i+1]=='/') ){
- buff[i]='\0';
- break;
- }
- }
- }else{ // an error occured -> can be eof or error
- return -1;
-
- }
-
- line++;
- return strlen(buff);
- }
-
-
- int File::writeLine(const char* string){
- return -1;
- }
-
-
- bool File::exists(const char* filename){
- FILE* f=fopen(filename, "r");
-
- if(f){
- fclose(f);
- return true;
- }else{
- return false;
- }
- }
-
- bool File::exists(const char* filename, const char* searchPath){
- char* t=searchAndCreatePath(filename, searchPath);
- if(t!=NULL){
- delete[] t;
- return true;
- }else{
- return false;
- }
- }
-
- char* File::appendPath(const char* path, const char* filename){
- char* ret=new char[strlen(path)+1+strlen(filename)+1];
-
- strcpy(ret, path);
- if(path[strlen(path)-1]!='/')
- strcat(ret, "/");
- strcat(ret, filename);
-
- return ret;
- }
-
- char* File::extractPath(const char* filename){
- Tokenizer t(filename, "/\\", "");
-
- if(t.tokc<=1) // only filename
- return NULL;
-
- char* ret=new char[strlen(filename)+1];
- ret[0]='\0';
- for(int i=0;i<t.tokc-1;i++){
- strcat(ret, t.tokv[i]);
- strcat(ret, "/");
- }
-
- return ret;
- }
-
- char* File::searchAndCreatePath(const char* filename, const char* searchPath){
- int i;
- char* candidate;
-
- Tokenizer tf(filename, "/\\", "");
- Tokenizer tp(searchPath, ";", "");
-
- if(exists(filename))
- return newString(filename);
-
- char* file=tf.tokv[tf.tokc-1];
- for(i=0;i<tp.tokc;i++){
- candidate=appendPath(tp.tokv[i], file);
- if(exists(candidate)){
- return candidate;
- }else{
- delete[] candidate;
- }
- }
-
- return NULL;
- // return newString(filename); //THINKABOUTME
- }
-
-