home *** CD-ROM | disk | FTP | other *** search
/ Enter 2005 March / ENTER.ISO / files / fwp-0.0.6-win32-installer.exe / File.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-12-06  |  2.6 KB  |  154 lines

  1. #include "File.h"
  2.  
  3. #include "Tokenizer.h"
  4.  
  5.  
  6. File::File(){
  7.     filename=NULL;
  8.     file=NULL;
  9.     line=0;
  10. }
  11.  
  12. File::File(const char* filename, const char* mode){
  13.     this->filename=NULL;
  14.     file=NULL;
  15.     line=0;
  16.  
  17.     open(filename, mode);
  18. }
  19.  
  20. File::~File(){
  21.     if(file!=NULL){
  22.         close();
  23.     }
  24. }
  25.  
  26. bool File::open(const char* filename, const char* mode){
  27.     if(file!=NULL){
  28.         close();
  29.     }
  30.  
  31.     file=fopen(filename, mode);
  32.     if(file!=NULL){
  33.         this->filename=newString(filename);
  34.         line=0;
  35.         return true;
  36.     }else{
  37.         return false;
  38.     }
  39. }
  40.  
  41. void File::close(){
  42.     if(file!=NULL){
  43.         fclose(file);
  44.         file = NULL;
  45.         delete[] filename;
  46.         filename=NULL;
  47.         line=0;
  48.     }
  49. }
  50.  
  51. bool File::isOpen(){
  52.     return (file!=NULL);
  53. }
  54.  
  55. int File::readLine(int numCharsToRead, char* buff, bool stripComments/*=false*/){
  56.     if(file==NULL)
  57.         return -1;
  58.  
  59.     if(fgets(buff, numCharsToRead, file)){
  60.         for(int i=0;i<(signed int)strlen(buff);i++){
  61.             if(buff[i]=='\r' || buff[i]=='\n'
  62.                 || (stripComments && buff[i]=='/' && buff[i+1]=='/') ){
  63.                 buff[i]='\0';
  64.                 break;
  65.             }
  66.         }
  67.     }else{    // an error occured -> can be eof or error
  68.         return -1;
  69.  
  70.     }
  71.  
  72.     line++;
  73.     return strlen(buff);
  74. }
  75.  
  76.  
  77. int File::writeLine(const char* string){
  78.     return -1;
  79. }
  80.  
  81.  
  82. bool File::exists(const char* filename){
  83.     FILE* f=fopen(filename, "r");
  84.  
  85.     if(f){
  86.         fclose(f);
  87.         return true;
  88.     }else{
  89.         return false;
  90.     }
  91. }
  92.  
  93. bool File::exists(const char* filename, const char* searchPath){
  94.     char* t=searchAndCreatePath(filename, searchPath);
  95.     if(t!=NULL){
  96.         delete[] t;
  97.         return true;
  98.     }else{
  99.         return false;
  100.     }
  101. }
  102.  
  103. char* File::appendPath(const char* path, const char* filename){
  104.     char* ret=new char[strlen(path)+1+strlen(filename)+1];
  105.  
  106.     strcpy(ret, path);
  107.     if(path[strlen(path)-1]!='/')
  108.         strcat(ret, "/");
  109.     strcat(ret, filename);
  110.  
  111.     return ret;
  112. }
  113.  
  114. char* File::extractPath(const char* filename){
  115.     Tokenizer t(filename, "/\\", "");
  116.  
  117.     if(t.tokc<=1)        // only filename
  118.         return NULL;
  119.  
  120.     char* ret=new char[strlen(filename)+1];
  121.     ret[0]='\0';
  122.     for(int i=0;i<t.tokc-1;i++){
  123.         strcat(ret, t.tokv[i]);
  124.         strcat(ret, "/");
  125.     }
  126.     
  127.     return ret;
  128. }
  129.  
  130. char* File::searchAndCreatePath(const char* filename, const char* searchPath){
  131.     int i;
  132.     char* candidate;
  133.  
  134.     Tokenizer tf(filename, "/\\", "");
  135.     Tokenizer tp(searchPath, ";", "");
  136.  
  137.     if(exists(filename))
  138.         return newString(filename);
  139.  
  140.     char* file=tf.tokv[tf.tokc-1];
  141.     for(i=0;i<tp.tokc;i++){
  142.         candidate=appendPath(tp.tokv[i], file);
  143.         if(exists(candidate)){
  144.             return candidate;
  145.         }else{
  146.             delete[] candidate;
  147.         }
  148.     }
  149.  
  150.     return NULL;
  151. //    return newString(filename);    //THINKABOUTME
  152. }
  153.  
  154.