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

Diff of /trunk/base/generic/general/ospath.c

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

revision 541 by johnpye, Wed Apr 26 07:32:13 2006 UTC revision 542 by johnpye, Wed Apr 26 15:15:40 2006 UTC
# Line 12  Line 12 
12  #  define __WIN32__  #  define __WIN32__
13  # endif  # endif
14  #endif  #endif
15    
16    //#define VERBOSE
17    
18  #ifndef TEST  #if !defined(TEST) && !defined(VERBOSE)
19  # define NDEBUG  # define NDEBUG
20  #endif  #endif
21    
22    //#define TRY_GETPWUID
23    
24    #define DO_FIXSLASHES
25    
26  #ifndef NDEBUG  #ifndef NDEBUG
27  # include <assert.h>  # include <assert.h>
# Line 53  Line 59 
59  # define GETENV(VAR) getenv(VAR)  # define GETENV(VAR) getenv(VAR)
60  #endif  #endif
61    
62  #define PATHMAX 1024  // PATH_MAX is in ospath.h
63  #define DRIVEMAX 3  #define DRIVEMAX 3
64  #define LISTMAX 256  #define LISTMAX 256
65    
# Line 62  Line 68 
68  #endif  #endif
69    
70  struct FilePath{  struct FilePath{
71      char path[PATHMAX]; /// the string version of the represented POSIX path      char path[PATH_MAX]; /// the string version of the represented POSIX path
72    
73  #ifdef WINPATHS  #ifdef WINPATHS
74      char drive[DRIVEMAX]; /// the drive the path resides on (field is absent in POSIX systems)      char drive[DRIVEMAX]; /// the drive the path resides on (field is absent in POSIX systems)
# Line 76  struct FilePath{ Line 82  struct FilePath{
82  #define CALLOC calloc  #define CALLOC calloc
83    
84  #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)
85    
86  void ospath_fixslash(char *path);  #ifdef DO_FIXSLASHES
87    void ospath_fixslash(char *path);
88    #endif
89    
90  struct FilePath *ospath_getcwd();  struct FilePath *ospath_getcwd();
91    
# Line 116  void ospath_extractdriveletter(struct Fi Line 124  void ospath_extractdriveletter(struct Fi
124  # define PATH_SEPARATOR_STR  "\\"  # define PATH_SEPARATOR_STR  "\\"
125  # define PATH_SEPARATOR_CHAR  '\\'  # define PATH_SEPARATOR_CHAR  '\\'
126  # define PATH_LISTSEP_CHAR ';'  # define PATH_LISTSEP_CHAR ';'
127  # define PATH_LISTSEP_STR ";"  # define PATH_LISTSEP_STR ";"
128    # define PATH_WRONGSLASH_CHAR '/'
129    # define PATH_WRONGSLASH_STR "/"
130  #else  #else
131  # define PATH_SEPARATOR_STR "/"  # define PATH_SEPARATOR_STR "/"
132  # define PATH_SEPARATOR_CHAR '/'  # define PATH_SEPARATOR_CHAR '/'
133  # define PATH_LISTSEP_CHAR ':'  # define PATH_LISTSEP_CHAR ':'
134  # define PATH_LISTSEP_STR ":"  # define PATH_LISTSEP_STR ":"
135    # define PATH_WRONGSLASH_CHAR '\\'
136    # define PATH_WRONGSLASH_STR "\\"
137  #endif  #endif
138    
139  /**  /**
# Line 130  void ospath_extractdriveletter(struct Fi Line 142  void ospath_extractdriveletter(struct Fi
142  struct FilePath *ospath_new(const char *path){  struct FilePath *ospath_new(const char *path){
143      struct FilePath *fp;      struct FilePath *fp;
144      fp = MALLOC(sizeof(struct FilePath));      fp = MALLOC(sizeof(struct FilePath));
145      STRNCPY(fp->path, path, PATHMAX);      STRNCPY(fp->path, path, PATH_MAX);
146      //X(fp->path);      //X(fp->path);
147      //X(path);      //X(path);
148      assert(strcmp(fp->path,path)==0);      assert(strcmp(fp->path,path)==0);
# Line 141  struct FilePath *ospath_new(const char * Line 153  struct FilePath *ospath_new(const char *
153    
154      //X(fp->drive);      //X(fp->drive);
155    
156  #ifdef __MINGW32__  #ifdef DO_FIXSLASHES
157      ospath_fixslash(fp->path);      ospath_fixslash(fp->path);
158  #endif  #endif
159    
# Line 176  void ospath_free(struct FilePath *fp){ Line 188  void ospath_free(struct FilePath *fp){
188  */  */
189  struct FilePath *ospath_new_from_posix(char *posixpath){  struct FilePath *ospath_new_from_posix(char *posixpath){
190      struct FilePath *fp = MALLOC(sizeof(struct FilePath));      struct FilePath *fp = MALLOC(sizeof(struct FilePath));
191      STRNCPY(fp->path,posixpath,PATHMAX);      STRNCPY(fp->path,posixpath,PATH_MAX);
192  #ifdef WINPATHS  #ifdef WINPATHS
193      X(fp->path);      X(fp->path);
194      ospath_extractdriveletter(fp);      ospath_extractdriveletter(fp);
195  #endif  #endif
196    
197  #ifdef __WIN32__  #ifdef DO_FIXSLASHES
198      ospath_fixslash(fp->path);      ospath_fixslash(fp->path);
199  #endif  #endif
200    
# Line 192  struct FilePath *ospath_new_from_posix(c Line 204  struct FilePath *ospath_new_from_posix(c
204    
205      return fp;      return fp;
206  }  }
207    
208    #ifdef DO_FIXSLASHES
209  void ospath_fixslash(char *path){  void ospath_fixslash(char *path){
210    
211      char *p;      char *p;
212      char temp[PATHMAX];      char temp[PATH_MAX];
213      int startslash;      int startslash;
214      int endslash;      int endslash;
215      STRTOKVAR(nexttok);      STRTOKVAR(nexttok);
216    
217      STRNCPY(temp,path,PATHMAX);      STRNCPY(temp,path,PATH_MAX);
218    
219      //X(path);      //X(path);
220    
221      startslash = (strlen(temp) > 0 && temp[0] == '/');      startslash = (strlen(temp) > 0 && temp[0] == PATH_WRONGSLASH_CHAR);
222      endslash = (strlen(temp) > 1 && temp[strlen(temp) - 1] == '/');      endslash = (strlen(temp) > 1 && temp[strlen(temp) - 1] == PATH_WRONGSLASH_CHAR);
223    
224      //V(startslash);      //V(startslash);
225      //V(endslash);      //V(endslash);
226    
227      // reset fp->path as required.      // reset fp->path as required.
228      STRNCPY(path, (startslash ? PATH_SEPARATOR_STR : ""), PATHMAX);      STRNCPY(path, (startslash ? PATH_SEPARATOR_STR : ""), PATH_MAX);
229    
230      //M("STARTING STRTOK");      //M("STARTING STRTOK");
231      for(p = STRTOK(temp, "/",nexttok);      for(p = STRTOK(temp, PATH_WRONGSLASH_STR,nexttok);
232              p!=NULL;              p!=NULL;
233              p = STRTOK(NULL,"/",nexttok)              p = STRTOK(NULL,PATH_WRONGSLASH_STR,nexttok)
234      ){      ){
235          // add a separator if we've already got some stuff          // add a separator if we've already got some stuff
236          if(          if(
# Line 240  void ospath_fixslash(char *path){ Line 253  void ospath_fixslash(char *path){
253      }      }
254    
255      //X(path);      //X(path);
256  }  }
257    #endif
258    
259    
260  /// Create but with no 'cleanup' call  /// Create but with no 'cleanup' call
261  struct FilePath *ospath_new_noclean(const char *path){  struct FilePath *ospath_new_noclean(const char *path){
262      struct FilePath *fp = MALLOC(sizeof(struct FilePath));      struct FilePath *fp = MALLOC(sizeof(struct FilePath));
263      STRNCPY(fp->path,path,PATHMAX);      STRNCPY(fp->path,path,PATH_MAX);
264    
265  #ifdef WINPATHS  #ifdef WINPATHS
266      //X(fp->path);      //X(fp->path);
# Line 255  struct FilePath *ospath_new_noclean(cons Line 270  struct FilePath *ospath_new_noclean(cons
270      //D(fp);      //D(fp);
271    
272  /*  /*
273  #ifdef __MINGW32__  #ifdef DO_FIXSLASHES
274      ospath_fixslash(fp->path);      ospath_fixslash(fp->path);
275      D(fp);      D(fp);
276  #endif  #endif
# Line 284  struct FilePath *ospath_getcwd(void){ Line 299  struct FilePath *ospath_getcwd(void){
299  */  */
300  struct FilePath *ospath_gethomepath(void){  struct FilePath *ospath_gethomepath(void){
301    
302      const char *pfx = getenv("HOME");      const char *pfx = (const char *)getenv("HOME");
303      struct FilePath *fp;      struct FilePath *fp;
304    
305  #ifndef __WIN32__  #ifndef __WIN32__
306    # ifdef TRY_GETPWUID
307      struct passwd *pw;      struct passwd *pw;
308    
309      if(pfx==NULL){      if(pfx==NULL){
310           pw = getpwuid(getuid());           pw = (struct passwd*)getpwuid(getuid());
311    
312          if(pw){          if(pw){
313              pfx = pw -> pw_dir;              pfx = pw->pw_dir;
314          }          }
315      }      }
316    # endif
317  #endif  #endif
318    
319      // create path object from HOME, but don't compress it      // create path object from HOME, but don't compress it
320      // (because that would lead to an infinite loop)      // (because that would lead to an infinite loop)
321      fp = ospath_new_noclean(pfx ? pfx : "");      fp = ospath_new_noclean(pfx ? pfx : "");
322    
323  #ifdef __MINGW32__  #ifdef DO_FIXSLASHES
324      ospath_fixslash(fp->path);      ospath_fixslash(fp->path);
325  #endif  #endif
326    
# Line 337  void ospath_extractdriveletter(struct Fi Line 354  void ospath_extractdriveletter(struct Fi
354  #endif  #endif
355    
356  void ospath_cleanup(struct FilePath *fp){  void ospath_cleanup(struct FilePath *fp){
357      char path[PATHMAX];      char path[PATH_MAX];
358      char *p;      char *p;
359      struct FilePath *home;      struct FilePath *home;
360      struct FilePath *working;      struct FilePath *working;
# Line 394  void ospath_cleanup(struct FilePath *fp) Line 411  void ospath_cleanup(struct FilePath *fp)
411                  working = ospath_getcwd();                  working = ospath_getcwd();
412    
413                  D(working);                  D(working);
414                  X(working->drive);  #ifdef WINPATHS
415                    X(working->drive);
416    #endif
417                  X(p);                  X(p);
418                  X(path);                  X(path);
419    
# Line 443  void ospath_cleanup(struct FilePath *fp) Line 462  void ospath_cleanup(struct FilePath *fp)
462  }  }
463    
464    
465  int ospath_isvalid(struct FilePath *fp){  int ospath_isvalid(struct FilePath *fp){
466        //if(fp==NULL) return 0;
467      return strlen(fp->path) > 0 ? 1 : 0;      return strlen(fp->path) > 0 ? 1 : 0;
468  }  }
469    
# Line 460  char *ospath_str(struct FilePath *fp){ Line 480  char *ospath_str(struct FilePath *fp){
480  #endif  #endif
481      return s;      return s;
482  }  }
483    
484    void ospath_strcpy(struct FilePath *fp, char *dest, int destsize){
485    #ifdef WINPATHS
486        STRNCPY(dest,fp->drive,destsize);
487        STRNCAT(dest,fp->path,destsize-strlen(dest));
488    #else
489        STRNCPY(dest,fp->path,destsize);
490    #endif
491    }
492    
493  void ospath_fwrite(struct FilePath *fp, FILE *dest){  void ospath_fwrite(struct FilePath *fp, FILE *dest){
494  #ifdef WINPATHS  #ifdef WINPATHS
# Line 485  struct FilePath *ospath_getparent(struct Line 514  struct FilePath *ospath_getparent(struct
514      int offset;      int offset;
515      char *pos;      char *pos;
516      int len1;      int len1;
517      char sub[PATHMAX];      char sub[PATH_MAX];
518      struct FilePath *fp1;      struct FilePath *fp1;
519    
520      D(fp);      D(fp);
# Line 514  struct FilePath *ospath_getparent(struct Line 543  struct FilePath *ospath_getparent(struct
543      if(*pos==PATH_SEPARATOR_CHAR){      if(*pos==PATH_SEPARATOR_CHAR){
544  #ifdef WINPATHS  #ifdef WINPATHS
545          STRCPY(sub,fp->drive);          STRCPY(sub,fp->drive);
546            STRNCAT(sub,fp->path,len1);
547    #else
548            STRNCPY(sub,fp->path,len1);
549            sub[len1]='\0';
550  #endif  #endif
         STRNCAT(sub,fp->path,len1);  
551          X(sub);          X(sub);
552          if(strcmp(sub,"")==0){          if(strcmp(sub,"")==0){
553              M("DIRECTORY IS EMPTY");              M("DIRECTORY IS EMPTY");
# Line 534  struct FilePath *ospath_getparent(struct Line 566  struct FilePath *ospath_getparent(struct
566  struct FilePath *ospath_getparentatdepthn(struct FilePath *fp, unsigned depth)  struct FilePath *ospath_getparentatdepthn(struct FilePath *fp, unsigned depth)
567  {  {
568      int startslash;      int startslash;
569      char path[PATHMAX];      char path[PATH_MAX];
570      char *temp;      char *temp;
571      char *p;      char *p;
572      STRTOKVAR(nexttok);      STRTOKVAR(nexttok);
573  #ifdef WINPATHS  #ifdef WINPATHS
574      char temp2[PATHMAX];      char temp2[PATH_MAX];
575  #endif  #endif
576    
577      if(      if(
# Line 567  struct FilePath *ospath_getparentatdepth Line 599  struct FilePath *ospath_getparentatdepth
599      {      {
600          if(strlen(temp) > 0 && temp[strlen(temp) - 1] != PATH_SEPARATOR_CHAR)          if(strlen(temp) > 0 && temp[strlen(temp) - 1] != PATH_SEPARATOR_CHAR)
601          {          {
602              temp += PATH_SEPARATOR_CHAR;              strcat(temp,PATH_SEPARATOR_STR);
603          }          }
604    
605          STRCAT(temp,p);          STRCAT(temp,p);
# Line 580  struct FilePath *ospath_getparentatdepth Line 612  struct FilePath *ospath_getparentatdepth
612      // put / on end as required      // put / on end as required
613      if(strlen(temp) > 0 ? (temp[strlen(temp) - 1] != PATH_SEPARATOR_CHAR) : 1)      if(strlen(temp) > 0 ? (temp[strlen(temp) - 1] != PATH_SEPARATOR_CHAR) : 1)
614      {      {
615          temp += PATH_SEPARATOR_CHAR;          strcat(temp,PATH_SEPARATOR_STR);
616      }      }
617    
618  #ifdef WINPATHS  #ifdef WINPATHS
# Line 601  char *ospath_getbasefilename(struct File Line 633  char *ospath_getbasefilename(struct File
633          // return empty name.          // return empty name.
634          return "";          return "";
635      }      }
636    
637      // reverse find a / ignoring the end / if it exists.      if(fp->path[strlen(fp->path)-1]==PATH_SEPARATOR_CHAR){
638            return NULL;
639        }
640    
641        // reverse find '/' but DON'T ignore a trailing slash
642        // (this is changed from the original implementation)
643      length = strlen(fp->path);      length = strlen(fp->path);
644      offset = (      offset = length;
             fp->path[length - 1] == PATH_SEPARATOR_CHAR  
             && length - 2 > 0  
         ) ? length - 2  
         : length;  
645    
646      pos = strrchr(fp->path, PATH_SEPARATOR_CHAR); /* OFFSET! */      pos = strrchr(fp->path, PATH_SEPARATOR_CHAR); /* OFFSET! */
647    
648      // extract filename given position of find / and return it.      // extract filename given position of find / and return it.
649      if(pos != fp->path + length){      if(pos != NULL){
650          int length1 = length - ((pos - fp->path) + 1) - (offset != length ? 1 : 0);          unsigned length1 = length - ((pos - fp->path) + 1);
651          temp = CALLOC(length1,sizeof(char));          temp = CALLOC(length1,sizeof(char));
652            
653            V(length1);
654          STRNCPY(temp, pos + 1, length1);          STRNCPY(temp, pos + 1, length1);
655          return temp;          return temp;
656      }else{      }else{
# Line 626  char *ospath_getbasefilename(struct File Line 660  char *ospath_getbasefilename(struct File
660      }      }
661  }  }
662    
663  char *ospath_getbasefiletitle(struct FilePath *fp){  char *ospath_getfilestem(struct FilePath *fp){
664      char *temp;      char *temp;
665      char *pos;      char *pos;
666    
# Line 634  char *ospath_getbasefiletitle(struct Fil Line 668  char *ospath_getbasefiletitle(struct Fil
668          return NULL;          return NULL;
669      }      }
670    
671      temp = ospath_getbasefilename(fp);      temp = ospath_getbasefilename(fp);
672      pos = strrchr(temp,'.');      if(temp==NULL){
673            // it's a directory
674      if(pos != NULL){          return NULL;
675          // remove extension.      }
         *pos = '\0';  
     }  
676    
677        pos = strrchr(temp,'.');
678    
679        if(pos==NULL || pos==temp){
680            // no extension, or a filename starting with '.'
681            // -- return the whole filename
682            return temp;
683        }
684    
685        // remove extension.
686        *pos = '\0';
687    
688      return temp;      return temp;
689  }  }
690    
691  char *ospath_getbasefileextension(struct FilePath *fp){  char *ospath_getfileext(struct FilePath *fp){
692      char *temp, *temp2, *pos;      char *temp, *temp2, *pos;
693      int len1;      int len1;
694    
# Line 653  char *ospath_getbasefileextension(struct Line 696  char *ospath_getbasefileextension(struct
696          return NULL;          return NULL;
697      }      }
698    
699      temp = ospath_getbasefilename(fp);      temp = ospath_getbasefilename(fp);
700        if(temp==NULL){
701            // it's a directory
702            return NULL;
703        }
704    
705      // make sure there is no / on the end.      // make sure there is no / on the end.
706        /// FIXME: is this good policy, removing a trailing slash?
707      if(temp[strlen(temp) - 1] == PATH_SEPARATOR_CHAR){      if(temp[strlen(temp) - 1] == PATH_SEPARATOR_CHAR){
708          temp[strlen(temp)-1] = '\0';          temp[strlen(temp)-1] = '\0';
709      }      }
710    
711      pos = strrchr(temp,'.');      pos = strrchr(temp,'.');
712    
713      if(pos != NULL)      if(pos != NULL && pos!=temp){
     {  
714          // extract extension.          // extract extension.
715          len1 = temp + strlen(temp) - pos;          len1 = temp + strlen(temp) - pos;
716          temp2 = CALLOC(len1, sizeof(char));          temp2 = CALLOC(len1, sizeof(char));
# Line 733  struct FilePath *ospath_root(struct File Line 780  struct FilePath *ospath_root(struct File
780      //M("JUST RETURNING PATH SEP");      //M("JUST RETURNING PATH SEP");
781      return ospath_new(PATH_SEPARATOR_STR);      return ospath_new(PATH_SEPARATOR_STR);
782  #endif  #endif
783  }  }
784    
785    struct FilePath *ospath_getdir(struct FilePath *fp){
786        char *pos;
787        char s[PATH_MAX];
788    
789        pos = strrchr(fp->path,PATH_SEPARATOR_CHAR);
790        if(pos==NULL){
791            return ospath_new(".");
792        }
793    #ifdef WINPATHS
794        strncpy(s,fp->drive,PATH_MAX);
795        strncat(s,fp->path,pos - fp->path);
796    #else
797        strncpy(s,fp->path,pos - fp->path);
798    #endif
799        return ospath_new(s);
800    }
801    
802  int ospath_cmp(struct FilePath *fp1, struct FilePath *fp2){  int ospath_cmp(struct FilePath *fp1, struct FilePath *fp2){
803      char temp[2][PATHMAX];      char temp[2][PATH_MAX];
804  #ifdef WINPATHS  #ifdef WINPATHS
805      char *p;      char *p;
806  #endif  #endif
# Line 798  int ospath_cmp(struct FilePath *fp1, str Line 862  int ospath_cmp(struct FilePath *fp1, str
862  struct FilePath *ospath_concat(struct FilePath *fp1, struct FilePath *fp2){  struct FilePath *ospath_concat(struct FilePath *fp1, struct FilePath *fp2){
863    
864      struct FilePath *fp;      struct FilePath *fp;
865      char *temp[2];      char temp[2][PATH_MAX];
866      char *temp2;      char temp2[PATH_MAX];
867      struct FilePath *r;      struct FilePath *r;
868    
869      fp = MALLOC(sizeof(struct FilePath));      fp = MALLOC(sizeof(struct FilePath));
# Line 828  struct FilePath *ospath_concat(struct Fi Line 892  struct FilePath *ospath_concat(struct Fi
892      // now, both paths are valid...      // now, both paths are valid...
893    
894  #ifdef WINPATHS  #ifdef WINPATHS
895      temp[0] = CALLOC(2 + strlen(fp1->drive)+strlen(fp1->path), sizeof(char));      STRNCPY(temp[0],fp1->drive,PATH_MAX);
896      STRCPY(temp[0],fp1->drive);      STRNCAT(temp[0],fp1->path,PATH_MAX-strlen(temp[0]));
     STRCAT(temp[0],fp1->path);  
897  #else  #else
898      temp[0] = CALLOC(2 + strlen(fp1->path), sizeof(char));      STRNCPY(temp[0], fp1->path,PATH_MAX);
     STRCPY(temp[0], fp1->path);  
899  #endif  #endif
900    
901      temp[1] = CALLOC(2 + strlen(fp2->path), sizeof(char));      STRNCPY(temp[1], fp2->path,PATH_MAX);
     STRCPY(temp[1], fp2->path);  
902    
903      // make sure temp has a / on the end.      // make sure temp has a / on the end.
904      if(temp[0][strlen(temp[0]) - 1] != PATH_SEPARATOR_CHAR)      if(temp[0][strlen(temp[0]) - 1] != PATH_SEPARATOR_CHAR)
905      {      {
906          STRCAT(temp[0],PATH_SEPARATOR_STR);          STRNCAT(temp[0],PATH_SEPARATOR_STR,PATH_MAX-strlen(temp[0]));
907      }      }
908    
909  #ifdef WINPATHS  #ifdef DO_FIXSLASHES
910      ospath_fixslash(temp[0]);      ospath_fixslash(temp[0]);
911      ospath_fixslash(temp[1]);      ospath_fixslash(temp[1]);
912  #endif  #endif
# Line 857  struct FilePath *ospath_concat(struct Fi Line 918  struct FilePath *ospath_concat(struct Fi
918    
919      // make sure rhs path has NOT got a / at the start.      // make sure rhs path has NOT got a / at the start.
920      if(temp[1][0] == PATH_SEPARATOR_CHAR){      if(temp[1][0] == PATH_SEPARATOR_CHAR){
         FREE(temp[0]);  
         FREE(temp[1]);  
921          return NULL;          return NULL;
922      }      }
923    
924      // create a new path object with the two path strings appended together.      // create a new path object with the two path strings appended together.
925      temp2 = CALLOC(strlen(temp[0])+strlen(temp[1]), sizeof(char));      STRNCPY(temp2,temp[0],PATH_MAX);
926      STRCPY(temp2,temp[0]);      STRNCAT(temp2,temp[1],PATH_MAX-strlen(temp2));
     STRCAT(temp2,temp[1]);  
927      //V(strlen(temp2));      //V(strlen(temp2));
928      //X(temp2);      //X(temp2);
929      r = ospath_new_noclean(temp2);      r = ospath_new_noclean(temp2);
     FREE(temp2);  
     FREE(temp[0]);  
     FREE(temp[1]);  
930      D(r);      D(r);
931      /* ospath_cleanup(r);*/      /* ospath_cleanup(r);*/
932      return r;      return r;
# Line 883  void ospath_append(struct FilePath *fp, Line 938  void ospath_append(struct FilePath *fp,
938      struct FilePath fp2;      struct FilePath fp2;
939    
940      ospath_copy(&fp2,fp1);      ospath_copy(&fp2,fp1);
941    #ifdef DO_FIXSLASHES
942      ospath_fixslash(fp2.path);      ospath_fixslash(fp2.path);
943    #endif
944    
945      if(!ospath_isvalid(&fp2)){      if(!ospath_isvalid(&fp2)){
946          M("fp1 invalid");          M("fp1 invalid");
# Line 894  void ospath_append(struct FilePath *fp, Line 951  void ospath_append(struct FilePath *fp,
951          // set this object to be the same as the rhs          // set this object to be the same as the rhs
952          M("fp invalid");          M("fp invalid");
953          ospath_copy(fp,&fp2);          ospath_copy(fp,&fp2);
954          ospath_fixslash(fp->path);  #ifdef DO_FIXSLASHES
955            ospath_fixslash(fp->path);
956    #endif
957          return;          return;
958      }      }
959    
# Line 951  void ospath_debug(struct FilePath *fp){ Line 1010  void ospath_debug(struct FilePath *fp){
1010  #else  #else
1011      fprintf(stderr,"{\"%s\"}\n",fp->path);      fprintf(stderr,"{\"%s\"}\n",fp->path);
1012  #endif  #endif
1013  }  }
1014    
1015    FILE *ospath_fopen(struct FilePath *fp, const char *mode){
1016        char s[PATH_MAX];
1017        if(!ospath_isvalid(fp)){
1018            E("Invalid path");
1019            return NULL;
1020        }
1021        ospath_strcpy(fp,s,PATH_MAX);
1022        FILE *f = fopen(s,mode);
1023        return f;
1024    }
1025    
1026  //------------------------  //------------------------
1027  // SEARCH PATH FUNCTIONS  // SEARCH PATH FUNCTIONS
1028    
1029  struct FilePath **ospath_searchpath_new(char *path){  struct FilePath **ospath_searchpath_new(const char *path){
1030      char *p;      char *p;
1031      char *list[LISTMAX];      char *list[LISTMAX];
1032      unsigned n=0;      unsigned n=0;
1033      char *c;      char *c;
1034      unsigned i;      unsigned i;
1035      struct FilePath **pp;      struct FilePath **pp;
1036      STRTOKVAR(nexttok);      STRTOKVAR(nexttok);
1037    
1038      for(p=STRTOK(path,PATH_LISTSEP_STR,nexttok); p!= NULL; p=STRTOK(NULL,PATH_LISTSEP_STR,nexttok)){      char path1[PATH_MAX];
1039        strncpy(path1,path,PATH_MAX);
1040    
1041        X(path1);
1042        X(PATH_LISTSEP_STR);
1043    
1044        V(strlen(path1));
1045        V(strlen(PATH_LISTSEP_STR));
1046    
1047        /*
1048        c = strstr(path,PATH_LISTSEP_CHAR);
1049        if(c==NULL){
1050            E("NO TOKEN FOUND");
1051        }
1052        */
1053    
1054        p=STRTOK(path1,PATH_LISTSEP_STR,nexttok);
1055        X(p);
1056        for(; p!= NULL; p=STRTOK(NULL,PATH_LISTSEP_STR,nexttok)){
1057          c = CALLOC(strlen(p),sizeof(char));          c = CALLOC(strlen(p),sizeof(char));
1058          X(p);          X(p);
1059          STRCPY(c,p);          STRCPY(c,p);
# Line 992  struct FilePath **ospath_searchpath_new( Line 1080  struct FilePath **ospath_searchpath_new(
1080      }      }
1081      pp[n] = NULL;      pp[n] = NULL;
1082    
1083      for(i=0;i<n;++i){      for(i=0;i<n;++i){
1084    #ifdef DO_FIXSLASHES
1085            ospath_fixslash(pp[i]->path);
1086    #endif
1087          D(pp[i]);          D(pp[i]);
1088      }      }
1089    
1090      return pp;      return pp;
1091    }
1092    
1093    void ospath_searchpath_free(struct FilePath **searchpath){
1094        struct FilePath **p;
1095        for(p=searchpath; *p!=NULL; ++p){
1096            ospath_free(*p);
1097        }
1098        FREE(searchpath);
1099  }  }
1100    
1101  struct FilePath *ospath_searchpath_iterate(  struct FilePath *ospath_searchpath_iterate(
# Line 1006  struct FilePath *ospath_searchpath_itera Line 1105  struct FilePath *ospath_searchpath_itera
1105  ){  ){
1106      struct FilePath **p;      struct FilePath **p;
1107    
1108      p = searchpath;      p = searchpath;
1109    
1110      while(p!=NULL){      M("SEARCHING IN...");
1111          D(*p);      for(p=searchpath; *p!=NULL; ++p){
1112            D(*p);
1113        }
1114        
1115        for(p=searchpath; *p!=NULL; ++p){
1116            D(*p);
1117          if((*testfn)(*p,searchdata)){          if((*testfn)(*p,searchdata)){
1118              return *p;              return *p;
1119          }          }
         ++p;  
1120      }      }
1121      return NULL;      return NULL;
1122  }  }
# Line 1030  FilePathTestFn ospath_searchpath_testexi Line 1133  FilePathTestFn ospath_searchpath_testexi
1133      Return 1 if the file exists relative inside path      Return 1 if the file exists relative inside path
1134  */  */
1135  int ospath_searchpath_testexists(struct FilePath *path,void *file){  int ospath_searchpath_testexists(struct FilePath *path,void *file){
1136      struct FilePath *fp, *fp1;      struct FilePath *fp, *fp1, *fp2;
1137      fp = (struct FilePath *)file;      fp = (struct FilePath *)file;
1138      D(fp);      D(fp);
1139      fp1 = ospath_concat(path,fp);      fp1 = ospath_concat(path,fp);
1140      D(fp1);      D(fp1);
1141    
1142        fp2 = ospath_new("\\GTK\\bin\\johnpye\\extfn");
1143        if(ospath_cmp(fp1,fp2)==0){
1144            return 1;
1145        }
1146      return 0;      return 0;
1147  }  }
1148    
1149  #include <assert.h>  #include <assert.h>
1150    
1151  int main(void){  int main(void){
1152      struct FilePath *fp1, *fp2, *fp3, *fp4;      struct FilePath *fp1, *fp2, *fp3, *fp4;
1153        char *s1, *s2;
1154      struct FilePath **pp, **p1;// will be returned null-terminated      struct FilePath **pp, **p1;// will be returned null-terminated
1155      char pathtext[]="c:\\Program Files\\GnuWin32\\bin;c:\\GTK\\bin;e:\\ascend\\;..\\..\\pygtk";  #ifdef WINPATHS
1156        char pathtext[]="c:\\Program Files\\GnuWin32\\bin;c:\\GTK\\bin;e:\\ascend\\;..\\..\\pygtk";
1157        char pathtext2[]="c:\\Program Files\\ASCEND\\models";
1158    #else
1159        char pathtext[]="\\Program Files\\GnuWin32\\bin:\\GTK\\bin:\\ascend\\:..\\..\\pygtk";
1160        char pathtext2[]="/usr/local/ascend/models";
1161    #endif
1162    
1163      //------------------------      //------------------------
1164    
1165      fp1 = ospath_new_from_posix("/usr/local/hello/");      fp1 = ospath_new_from_posix("/usr/local/hello/");
1166      fp2 = ospath_getparent(fp1);      fp2 = ospath_getparent(fp1);
1167      fp3 = ospath_new_from_posix("/usr/local");      fp3 = ospath_new_from_posix("/usr/local");
1168    
1169        D(fp1);
1170        D(fp2);
1171        D(fp3);
1172      assert(ospath_cmp(fp2,fp3)==0);      assert(ospath_cmp(fp2,fp3)==0);
1173      M("Passed 'getparent' test\n");      M("Passed 'getparent' test\n");
1174    
# Line 1175  int main(void){ Line 1293  int main(void){
1293    
1294      //---------------------------      //---------------------------
1295    
1296      fp1 = ospath_new("c:/program files");      fp1 = ospath_new("c:/Program Files");
1297      fp2 = ospath_new("GnuWin32\\bin");      fp2 = ospath_new("GnuWin32\\bin");
1298      fp3 = ospath_concat(fp1, fp2);      fp3 = ospath_concat(fp1, fp2);
1299    
# Line 1188  int main(void){ Line 1306  int main(void){
1306    
1307      //---------------------------      //---------------------------
1308    
1309      fp1 = ospath_new("c:/program files/");      fp1 = ospath_new("c:/Program Files/");
1310      fp2 = ospath_new("GnuWin32\\bin");      fp2 = ospath_new("GnuWin32\\bin");
1311      fp3 = ospath_concat(fp1, fp2);      fp3 = ospath_concat(fp1, fp2);
1312    
# Line 1201  int main(void){ Line 1319  int main(void){
1319    
1320      //---------------------------      //---------------------------
1321    
1322      fp1 = ospath_new("c:/program files/GnuWin32/bin");      fp1 = ospath_new("c:/Program Files/GnuWin32/bin");
1323      fp2 = ospath_new("johnpye/extfn");      fp2 = ospath_new("johnpye/extfn");
1324      fp3 = ospath_concat(fp1, fp2);      fp3 = ospath_concat(fp1, fp2);
1325    
# Line 1220  int main(void){ Line 1338  int main(void){
1338          D(*p1);          D(*p1);
1339      }      }
1340    
1341      fp1 = ospath_new("c:\\Program Files\\GnuWin32\\bin");  #ifdef WINPATHS
1342        fp1 = ospath_new("c:\\program files\\GnuWin32\\bin");
1343    #else
1344        fp1 = ospath_new("\\Program Files\\GnuWin32\\bin");
1345    #endif
1346    
1347      D(fp1);      D(fp1);
1348      D(pp[0]);      D(pp[0]);
1349    
1350      assert(ospath_cmp(pp[0],fp1)==0);      assert(ospath_cmp(pp[0],fp1)==0);
1351    
1352      fp2 = ospath_new_noclean("johnpye/extfn");      fp2 = ospath_new_noclean("johnpye/extfn");
1353    
1354      ospath_searchpath_iterate(pp,&ospath_searchpath_testexists,(void*)fp2);      fp3 = ospath_searchpath_iterate(pp,&ospath_searchpath_testexists,(void*)fp2);
1355    
1356      assert(0);      assert(ospath_cmp(fp3,pp[1])==0);
1357        M("Passed path-search test\n");
1358    
1359        ospath_free(fp1);  
1360        ospath_free(fp2);
1361        ospath_searchpath_free(pp);
1362    
1363        //-------------------------------
1364    
1365        pp = ospath_searchpath_new(pathtext2);
1366    
1367        for (p1=pp; *p1!=NULL; ++p1){
1368            D(*p1);
1369        }
1370    
1371        fp2 = ospath_new_noclean("johnpye/extfn/extfntest");
1372        fp3 = ospath_searchpath_iterate(pp,&ospath_searchpath_testexists,(void*)fp2);
1373    
1374        D(fp2);
1375        D(fp3);
1376    
1377        assert(fp3==NULL);
1378        M("Passed path-search test 2\n");
1379    
1380        ospath_free(fp2);
1381        ospath_free(fp3);
1382        ospath_searchpath_free(pp);
1383        
1384        //-------------------------------
1385    
1386        fp1 = ospath_new("/usr/share/data/ascend/models/johnpye/extfn/extfntest.a4c");
1387        D(fp1);
1388        s1 = ospath_getbasefilename(fp1);
1389        X(s1);
1390        assert(strcmp(s1,"extfntest.a4c")==0);
1391        M("Passed getbasefilename test\n");
1392    
1393        ospath_free(fp1);
1394        FREE(s1);
1395    
1396        //-------------------------------
1397    
1398        fp1 = ospath_new("extfntest.a4c");
1399        D(fp1);
1400        s1 = ospath_getbasefilename(fp1);
1401        X(s1);
1402        assert(strcmp(s1,"extfntest.a4c")==0);
1403        M("Passed getbasefilename test 2\n");
1404    
1405        ospath_free(fp1);
1406        FREE(s1);
1407    
1408    
1409        //-------------------------------
1410    
1411        fp1 = ospath_new("/here/is/my/path.dir/");
1412        D(fp1);
1413        s1 = ospath_getbasefilename(fp1);
1414        X(s1);
1415        assert(NULL==s1);
1416        M("Passed getbasefilename test 3\n");
1417    
1418        ospath_free(fp1);
1419        FREE(s1);
1420    
1421        //-------------------------------
1422    
1423    #ifdef WINPATHS
1424        fp1 = ospath_new("c:extfntest.a4c");
1425        D(fp1);
1426        s1 = ospath_getbasefilename(fp1);
1427        X(s1);
1428        assert(strcmp(s1,"extfntest.a4c")==0);
1429        M("Passed getbasefilename test WINPATHS\n");
1430    
1431        ospath_free(fp1);
1432        FREE(s1);
1433    #endif
1434    
1435        //-------------------------------
1436    
1437        fp1 = ospath_new("/usr/share/data/ascend/models/johnpye/extfn/extfntest.a4c");
1438        D(fp1);
1439        s1 = ospath_getfilestem(fp1);
1440        X(s1);
1441        assert(strcmp(s1,"extfntest")==0);
1442        M("Passed getfilestem test\n");
1443    
1444        ospath_free(fp1);
1445        FREE(s1);
1446    
1447        //-------------------------------
1448    
1449        fp1 = ospath_new("/usr/share/data/ascend/models/johnpye/extfn/extfntest");
1450        D(fp1);
1451        s1 = ospath_getfilestem(fp1);
1452        X(s1);
1453        assert(strcmp(s1,"extfntest")==0);
1454        M("Passed getfilestem test 2\n");
1455    
1456        ospath_free(fp1);
1457        FREE(s1);
1458    
1459        //-------------------------------
1460    
1461        fp1 = ospath_new("/usr/share/data/ascend/.ascend.ini");
1462        D(fp1);
1463        s1 = ospath_getfilestem(fp1);
1464        X(s1);
1465        assert(strcmp(s1,".ascend")==0);
1466        M("Passed getfilestem test 3\n");
1467    
1468        ospath_free(fp1);
1469        FREE(s1);
1470    
1471        //-------------------------------
1472    
1473        fp1 = ospath_new("~/.vimrc");
1474        D(fp1);
1475        s1 = ospath_getfilestem(fp1);
1476        X(s1);
1477        assert(strcmp(s1,".vimrc")==0);
1478        M("Passed getfilestem test 3\n");
1479    
1480        ospath_free(fp1);
1481        FREE(s1);
1482    
1483        //-------------------------------
1484    
1485        fp1 = ospath_new("~/src/ascend-0.9.5-1.jdpipe.src.rpm");
1486        D(fp1);
1487        s1 = ospath_getfilestem(fp1);
1488        X(s1);
1489        assert(strcmp(s1,"ascend-0.9.5-1.jdpipe.src")==0);
1490        M("Passed getfilestem test 4\n");
1491    
1492        ospath_free(fp1);
1493        FREE(s1);
1494    
1495        //-------------------------------
1496    
1497        fp1 = ospath_new("~/dir1/dir2/");
1498        D(fp1);
1499        s1 = ospath_getfilestem(fp1);
1500        X(s1);
1501        assert(NULL==s1);
1502        M("Passed getfilestem test 5\n");
1503    
1504        ospath_free(fp1);
1505        FREE(s1);
1506    
1507        //-------------------------------
1508    
1509        fp1 = ospath_new("~/src/ascend-0.9.5-1.jdpipe.src.rpm");
1510        D(fp1);
1511        s1 = ospath_getfileext(fp1);
1512        X(s1);
1513        assert(strcmp(s1,".rpm")==0);
1514        M("Passed getbasefileext test\n");
1515    
1516        ospath_free(fp1);
1517        FREE(s1);
1518    
1519        //-------------------------------
1520    
1521        fp1 = ospath_new("~/.vimrc");
1522        D(fp1);
1523        s1 = ospath_getfileext(fp1);
1524        X(s1);
1525        assert(s1==NULL);
1526        M("Passed getbasefileext test 2\n");
1527    
1528        ospath_free(fp1);
1529        FREE(s1);
1530    
1531        //-------------------------------
1532    
1533        fp1 = ospath_new("./ascend4");
1534        D(fp1);
1535        s1 = ospath_getfileext(fp1);
1536        X(s1);
1537        assert(s1==NULL);
1538        M("Passed getbasefileext test 3\n");
1539    
1540        ospath_free(fp1);
1541        FREE(s1);
1542    
1543        //-------------------------------
1544    
1545        fp1 = ospath_new("/home/myfile");
1546        fp2 = ospath_getdir(fp1);
1547        fp3 = ospath_new("/home");
1548        assert(ospath_cmp(fp2,fp3)==0);
1549        M("Passed ospath_getdir test\n");
1550    
1551        ospath_free(fp1);
1552        ospath_free(fp2);
1553        ospath_free(fp3);
1554    
1555        //-------------------------------
1556    
1557        fp1 = ospath_new("/home/myfile.ext");
1558        fp2 = ospath_getdir(fp1);
1559        fp3 = ospath_new("/home");
1560        assert(ospath_cmp(fp2,fp3)==0);
1561        M("Passed ospath_getdir test 2\n");
1562    
1563        ospath_free(fp1);
1564        ospath_free(fp2);
1565        ospath_free(fp3);
1566    
1567        //-------------------------------
1568    
1569        fp1 = ospath_new("/home/mydir/");
1570        fp2 = ospath_getdir(fp1);
1571        fp3 = ospath_new("/home/mydir");
1572        assert(ospath_cmp(fp2,fp3)==0);
1573        M("Passed ospath_getdir test 3\n");
1574    
1575        ospath_free(fp1);
1576        ospath_free(fp2);
1577        ospath_free(fp3);
1578    
1579        //---------------------------------
1580      M("ALL TESTS PASSED");      M("ALL TESTS PASSED");
1581  }  }
1582    

Legend:
Removed from v.541  
changed lines
  Added in v.542

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