/[ascend]/trunk/ascend/general/ospath.c
ViewVC logotype

Diff of /trunk/ascend/general/ospath.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 534 by johnpye, Tue Apr 25 13:25:04 2006 UTC revision 535 by johnpye, Tue Apr 25 14:55:48 2006 UTC
# Line 1  Line 1 
1  #include <stdio.h>  #include <stdio.h>
2    #include <ctype.h>
3    
4  #include "ospath.h"  #include "ospath.h"
5    
# Line 23  struct FilePath{ Line 24  struct FilePath{
24  #define M(MSG) fprintf(stderr,"%s:%d: (%s) %s\n",__FILE__,__LINE__,__FUNCTION__,MSG)  #define M(MSG) fprintf(stderr,"%s:%d: (%s) %s\n",__FILE__,__LINE__,__FUNCTION__,MSG)
25  #define E(MSG) fprintf(stderr,"%s:%d: (%s) ERROR: %s\n",__FILE__,__LINE__,__FUNCTION__,MSG)  #define E(MSG) fprintf(stderr,"%s:%d: (%s) ERROR: %s\n",__FILE__,__LINE__,__FUNCTION__,MSG)
26  #define X(VAR) fprintf(stderr,"%s:%d: (%s) %s=%s\n",__FILE__,__LINE__,__FUNCTION__,#VAR,VAR)  #define X(VAR) fprintf(stderr,"%s:%d: (%s) %s=%s\n",__FILE__,__LINE__,__FUNCTION__,#VAR,VAR)
27    #define V(VAR) fprintf(stderr,"%s:%d: (%s) %s=%d\n",__FILE__,__LINE__,__FUNCTION__,#VAR,VAR)
28  #define D(VAR) ospath_debug(VAR,#VAR)  #define D(VAR) ospath_debug(VAR,#VAR)
29    
30  /**  /**
# Line 36  struct FilePath *ospath_gethomepath(void Line 38  struct FilePath *ospath_gethomepath(void
38    
39         ie. "///usr/bin///hello/////there// --> "/usr/bin/hello/there/"         ie. "///usr/bin///hello/////there// --> "/usr/bin/hello/there/"
40    
41      2. Resolve any ~ directory found to the current user's HOME path      2. Resolve a leading tilde (~) to the current user's HOME path
42    
43      3. @TODO what about "dir/../dir2/dir3" --> "dir2/dir3" ?      3. Remove redundant /./ in middle of path
44    
45        4. Remove reduncant dir/.. in path
46    
47        5. Environment substitution??
48    
49        6. On windows, drive reference if not specified
50    
51        7. What about \\server\path and URLs, gnomefs, etc?
52  */  */
53  void ospath_cleanup(struct FilePath *);  void ospath_cleanup(struct FilePath *);
54    
# Line 71  struct FilePath *ospath_new(const char * Line 81  struct FilePath *ospath_new(const char *
81      strcpy(fp->path, path);      strcpy(fp->path, path);
82    
83  #ifdef WINPATHS  #ifdef WINPATHS
84        X(fp->path);
85      ospath_extractdriveletter(fp);      ospath_extractdriveletter(fp);
86  #endif  #endif
87    
# Line 80  struct FilePath *ospath_new(const char * Line 91  struct FilePath *ospath_new(const char *
91  }  }
92    
93  /**  /**
94        This function will serve to allow #include-style file paths
95        to be specified with platform-independent forward slashes then
96        translated into the local filesystem format for subsequent use.
97    
98        This method should be identical to ospath_new on posix, right?
99    
100        @NOTE: on windows, we also want:
101                        C:dir/file --> c:$PWD\dir\file
102                          e:/hello --> e:\hello
103                         here/i/am --> here\i\am
104    
105        @NOTE:
106        A path-search function should create full file paths by
107        appending relative file to each component of the search path
108        then performing a callback on each one to determine if the
109        match is OK or not.
110    */
111    struct FilePath *ospath_new_from_posix(char *posixpath){
112        struct FilePath *fp = MALLOC(sizeof(struct FilePath));
113        char *p;
114        strcpy(fp->path,posixpath);
115    #ifdef WINPATHS
116        X(fp->path);
117        ospath_extractdriveletter(fp);
118    #endif
119    
120        char path[PATHMAX];
121        strcpy(path,fp->path);
122    
123        //X(path);
124    
125        int startslash = (strlen(path) > 0 && path[0] == '/');
126        int endslash = (strlen(path) > 1 && path[strlen(path) - 1] == '/');
127    
128        //V(startslash);
129        //V(endslash);
130    
131        // reset fp->path as required.
132        strcpy(fp->path, (startslash ? PATH_SEPARATOR_STR : ""));
133    
134        for(p = strtok(path, "/");
135                p!=NULL;
136                p = strtok(NULL,"/")
137        ){
138            // add a separator if we've already got some stuff
139            if(
140                strlen(fp->path) > 0
141                && fp->path[strlen(fp->path) - 1] != PATH_SEPARATOR_CHAR
142            ){
143                strcat(fp->path,PATH_SEPARATOR_STR);
144            }
145    
146            strcat(fp->path,p);
147        }
148    
149        // put / on end as required, according to what the starting path had
150        if(endslash && (strlen(fp->path) > 0 ? (fp->path[strlen(fp->path) - 1] != PATH_SEPARATOR_CHAR) : 1))
151        {
152            //M("adding endslash!");
153    
154            strcat(fp->path, PATH_SEPARATOR_STR);
155        }
156    
157        //X(fp->path);
158    
159        ospath_cleanup(fp);
160    
161        return(fp);
162    }
163    
164    /**
165      As for ospath_new but the 'cleanup' call is now optional      As for ospath_new but the 'cleanup' call is now optional
166  */  */
167  struct FilePath *ospath_new_noclean(const char *path){  struct FilePath *ospath_new_noclean(const char *path){
# Line 87  struct FilePath *ospath_new_noclean(cons Line 169  struct FilePath *ospath_new_noclean(cons
169      strcpy(fp->path,path);      strcpy(fp->path,path);
170    
171  #ifdef WINPATHS  #ifdef WINPATHS
172        X(fp->path);
173      ospath_extractdriveletter(fp);      ospath_extractdriveletter(fp);
174  #endif  #endif
175    
# Line 118  struct FilePath *ospath_gethomepath(void Line 201  struct FilePath *ospath_gethomepath(void
201  #ifdef WINPATHS  #ifdef WINPATHS
202  void ospath_extractdriveletter(struct FilePath *fp)  void ospath_extractdriveletter(struct FilePath *fp)
203  {  {
204        X(fp->path);
205      // extract the drive the path resides on...      // extract the drive the path resides on...
206      if(strlen(fp->path) >= 2 && fp->path[1] == ':')      if(strlen(fp->path) >= 2 && fp->path[1] == ':')
207      {      {
# Line 126  void ospath_extractdriveletter(struct Fi Line 210  void ospath_extractdriveletter(struct Fi
210          char firsttwo[2];          char firsttwo[2];
211          strncpy(firsttwo,fp->path,2);          strncpy(firsttwo,fp->path,2);
212    
213          if(sscanf(firsttwo, "%c:", &driveletter) == 1)          if(sscanf(firsttwo, "%c:", &driveletter) == 1){
         {  
             //M("FOUND LEADING DRIVE: %c\n",driveletter);  
214              strncpy(fp->drive,fp->path,2);              strncpy(fp->drive,fp->path,2);
215                fp->drive[0]=tolower(fp->drive[0]);
216              strcpy(fp->path, fp->path+2);              strcpy(fp->path, fp->path+2);
217            }else{
218                E("WHY HERE?");
219          }          }
220        }else{
221            strcpy(fp->drive,"");
222      }      }
223        X(fp->drive);
224  }  }
225  #endif  #endif
226    
# Line 148  void ospath_cleanup(struct FilePath *fp) Line 236  void ospath_cleanup(struct FilePath *fp)
236      int startslash = (strlen(fp->path) > 0 && fp->path[0] == PATH_SEPARATOR_CHAR);      int startslash = (strlen(fp->path) > 0 && fp->path[0] == PATH_SEPARATOR_CHAR);
237      int endslash = (strlen(fp->path) > 1 && fp->path[strlen(fp->path) - 1] == PATH_SEPARATOR_CHAR);      int endslash = (strlen(fp->path) > 1 && fp->path[strlen(fp->path) - 1] == PATH_SEPARATOR_CHAR);
238    
239      fprintf(stderr,"FS ON START = %d\n",startslash);      //fprintf(stderr,"FS ON START = %d\n",startslash);
240      fprintf(stderr,"FS ON END   = %d\n",endslash);      //fprintf(stderr,"FS ON END   = %d\n",endslash);
241      fprintf(stderr,"FIRST CHAR = %c\n",fp->path[0]);      //fprintf(stderr,"FIRST CHAR = %c\n",fp->path[0]);
242    
243      home = ospath_gethomepath();      home = ospath_gethomepath();
244    
# Line 160  void ospath_cleanup(struct FilePath *fp) Line 248  void ospath_cleanup(struct FilePath *fp)
248      // reset fp->path as required.      // reset fp->path as required.
249      strcpy(fp->path, (startslash ? PATH_SEPARATOR_STR : ""));      strcpy(fp->path, (startslash ? PATH_SEPARATOR_STR : ""));
250    
251      X(fp->path);      //X(fp->path);
252    
253      // split path into it tokens, using strtok which is NOT reentrant      // split path into it tokens, using strtok which is NOT reentrant
254      // so be careful!      // so be careful!
# Line 169  void ospath_cleanup(struct FilePath *fp) Line 257  void ospath_cleanup(struct FilePath *fp)
257              p!=NULL;              p!=NULL;
258              p = strtok(NULL,PATH_SEPARATOR_STR)              p = strtok(NULL,PATH_SEPARATOR_STR)
259      ){      ){
260          X(p);          //X(p);
261          if(strcmp(p, "~")==0){          if(strcmp(p, "~")==0){
262    
263              if(p == path){ // check that the ~ is the first character in the path              if(p == path){ // check that the ~ is the first character in the path
# Line 188  void ospath_cleanup(struct FilePath *fp) Line 276  void ospath_cleanup(struct FilePath *fp)
276                  M("EXPANDING LEADING '.' IN PATH");                  M("EXPANDING LEADING '.' IN PATH");
277                  // get current working directory                  // get current working directory
278                  pBuff = (char *)getcwd(NULL, 0);                  pBuff = (char *)getcwd(NULL, 0);
279                  X(pBuff);                  //X(pBuff);
280    
281                  // create new path with resolved working directory                  // create new path with resolved working directory
282                  workingPath = ospath_new_noclean(pBuff != NULL ? pBuff : ".");                  workingPath = ospath_new_noclean(pBuff != NULL ? pBuff : ".");
# Line 505  int ospath_cmp(struct FilePath *fp1, str Line 593  int ospath_cmp(struct FilePath *fp1, str
593      }      }
594    
595      // now, both are valid...      // now, both are valid...
596      M("BOTH ARE VALID");      //M("BOTH ARE VALID");
597    
598      char *temp[2];      char temp[2][PATHMAX];
599  #ifdef WINPATHS  #ifdef WINPATHS
600      temp[0] = CALLOC(1+strlen(fp1->drive)+strlen(fp1->path), sizeof(char));      X(fp1->drive);
601      strcpy(temp[0],fp1->drive);      strcpy(temp[0],fp1->drive);
602        X(temp[0]);
603        X(fp1->path);
604      strcat(temp[0],fp1->path);      strcat(temp[0],fp1->path);
605      temp[1] = CALLOC(1+strlen(fp2->drive)+strlen(fp2->path), sizeof(char));      X(temp[0]);
606      strcpy(temp[1],fp2->drive);      strcpy(temp[1],fp2->drive);
607      strcat(temp[1],fp2->path);      strcat(temp[1],fp2->path);
608  #else  #else
     temp[0] = CALLOC(1+strlen(fp1->path), sizeof(char));  
609      strcpy(temp[0], fp1->path);      strcpy(temp[0], fp1->path);
     temp[1] = CALLOC(1+strlen(fp2->path), sizeof(char));  
610      strcpy(temp[1], fp2->path);      strcpy(temp[1], fp2->path);
611  #endif  #endif
612    
# Line 532  int ospath_cmp(struct FilePath *fp1, str Line 620  int ospath_cmp(struct FilePath *fp1, str
620          strcat(temp[1],PATH_SEPARATOR_STR);          strcat(temp[1],PATH_SEPARATOR_STR);
621      }      }
622    
623      //X(temp[0]);      X(temp[0]);
624      //X(temp[1]);      X(temp[1]);
625    
626      return strcmp(temp[0],temp[1]);      return strcmp(temp[0],temp[1]);
627  }  }
# Line 680  void ospath_debug(struct FilePath *fp, c Line 768  void ospath_debug(struct FilePath *fp, c
768  int main(void){  int main(void){
769      struct FilePath *fp1, *fp2, *fp3, *fp4;      struct FilePath *fp1, *fp2, *fp3, *fp4;
770    
771        fp1 = ospath_new_from_posix("models/johnpye/extfn/extfntest");
772        D(fp1);
773        fp2 = ospath_new("models\\johnpye\\extfn\\extfntest");
774        D(fp2);
775        assert(ospath_cmp(fp1,fp2)==0);
776        M("Passed 'new_from_posix' test");
777    
778        FREE(fp1); FREE(fp2);
779    
780        //------------------------
781      fp1 = ospath_new(".\\src\\.\\images\\..\\\\movies\\");      fp1 = ospath_new(".\\src\\.\\images\\..\\\\movies\\");
782      fp2 = ospath_new(".\\src\\movies");      fp2 = ospath_new(".\\src\\movies");
783    
# Line 697  int main(void){ Line 795  int main(void){
795      assert(ospath_cmp(fp1,fp3)==0);      assert(ospath_cmp(fp1,fp3)==0);
796      M("Passed 'parent' test");      M("Passed 'parent' test");
797    
798      FREE(fp2); FREE(fp3);      FREE(fp1); FREE(fp2); FREE(fp3);
799        //------------------------
800    
801      fp2 = ospath_new("\\home\\john");      fp2 = ospath_new("\\home\\john");
802      fp3 = ospath_new("where\\mojo");      fp3 = ospath_new("where\\mojo");
# Line 708  int main(void){ Line 806  int main(void){
806    
807      ospath_append(fp2,fp3);      ospath_append(fp2,fp3);
808    
809      //fp4 = ospath_new("\\home\\john\\where\\mojo\\");      fp4 = ospath_new("\\home\\john\\where\\mojo\\");
810    
811      D(fp2);      D(fp2);
812      //assert(ospath_cmp(fp2,fp4)==0);      assert(ospath_cmp(fp2,fp4)==0);
813      //M("Passed 'append' test");      M("Passed 'append' test");
814    
 /*  
815      ospath_append(fp2,fp3);      ospath_append(fp2,fp3);
816      fp4 = ospath_root(fp2);      fp4 = ospath_root(fp2);
817      D(fp2);      D(fp2);
# Line 722  int main(void){ Line 819  int main(void){
819    
820      assert(ospath_cmp(fp2,fp4)==0);      assert(ospath_cmp(fp2,fp4)==0);
821      M("Passed 'concat' test");      M("Passed 'concat' test");
822  */  
 /*  
823      D(fp2);      D(fp2);
824    
825      FREE(fp2);      FREE(fp2);
# Line 742  int main(void){ Line 838  int main(void){
838    
839      D(fp1);      D(fp1);
840      D(fp2);      D(fp2);
841  */  
842    
843  }  }
844    

Legend:
Removed from v.534  
changed lines
  Added in v.535

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22