/[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 606 by johnpye, Thu May 11 03:10:43 2006 UTC revision 607 by johnpye, Tue May 16 04:19:20 2006 UTC
# Line 1  Line 1 
1    /*  ASCEND modelling environment
2        Copyright (C) 2006 Carnegie Mellon University
3    
4        This program is free software; you can redistribute it and/or modify
5        it under the terms of the GNU General Public License as published by
6        the Free Software Foundation; either version 2, or (at your option)
7        any later version.
8    
9        This program is distributed in the hope that it will be useful,
10        but WITHOUT ANY WARRANTY; without even the implied warranty of
11        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12        GNU General Public License for more details.
13    
14        You should have received a copy of the GNU General Public License
15        along with this program; if not, write to the Free Software
16        Foundation, Inc., 59 Temple Place - Suite 330,
17        Boston, MA 02111-1307, USA.
18    *//**
19        @file
20        Platform-agnostic filesystem path manipulation functions.
21        By John Pye.
22    */
23    
24  #include <string.h>  #include <string.h>
25  #include <malloc.h>  #include <malloc.h>
26  #include <stdio.h>  #include <stdio.h>
# Line 475  void ospath_strcpy(struct FilePath *fp, Line 498  void ospath_strcpy(struct FilePath *fp,
498    
499  void ospath_strcat(struct FilePath *fp, char *dest, int destsize){  void ospath_strcat(struct FilePath *fp, char *dest, int destsize){
500      int remaining = destsize - strlen(dest);      int remaining = destsize - strlen(dest);
501      V(remaining);        V(remaining);
502  #ifdef WINPATHS  #ifdef WINPATHS
503      STRNCAT(dest,fp->drive,remaining);      STRNCAT(dest,fp->drive,remaining);
504      STRNCAT(dest,fp->path,remaining-strlen(dest));      STRNCAT(dest,fp->path,remaining-strlen(dest));
# Line 1146  struct FilePath *ospath_searchpath_itera Line 1169  struct FilePath *ospath_searchpath_itera
1169      }      }
1170      return NULL;      return NULL;
1171  }  }
   
   
 /*--------------------------------  
     some simple test routines...  
 */  
 #ifdef TEST  
   
 FilePathTestFn ospath_searchpath_testexists;  
   
 /**  
     This is a sample searchpath test function. Assumes the 'userfdata' is a  
     relative FilePath which is appended to path, and then if it matches  
     the path \GTK\bin\johnpye\extfn, returns true. This is of  
     course a fairly useless test function, so it's just for testing.  
 */  
 int ospath_searchpath_testexists(struct FilePath *path,void *file){  
     struct FilePath *fp, *fp1, *fp2;  
     fp = (struct FilePath *)file;  
     D(fp);  
     fp1 = ospath_concat(path,fp);  
     D(fp1);  
   
 #ifdef WINPATHS  
     fp2 = ospath_new("c:\\GTK\\bin\\johnpye\\extfn");  
 #else  
     fp2 = ospath_new("/GTK/bin/johnpye/extfn");  
 #endif  
   
     char *t=ospath_str(fp1);  
     MC("1",t);  
     FREE(t);  
   
     t=ospath_str(fp2);  
     MC("31;1",t);  
     FREE(t);  
   
     if(ospath_cmp(fp1,fp2)==0){  
         MC("32","MATCH");  
         return 1;  
     }  
     MC("31","NO MATCH");  
     return 0;  
 }  
   
 #include <assert.h>  
   
 // switch to boldface for messages in 'main'  
 #undef D  
 #define D DD  
 #undef M  
 #define M MM  
   
 int main(void){  
     struct FilePath *fp1, *fp2, *fp3, *fp4;  
     char *s1;  
     struct FilePath **pp, **p1;// will be returned null-terminated  
 #ifdef WINPATHS  
     char pathtext[]="c:\\Program Files\\GnuWin32\\bin;c:\\GTK\\bin;e:\\ascend\\;..\\..\\pygtk";  
     char pathtext2[]="c:\\Program Files\\ASCEND\\models";  
 #else  
     char pathtext[]="\\Program Files\\GnuWin32\\bin:\\GTK\\bin:\\ascend\\:..\\..\\pygtk";  
     char pathtext2[]="/usr/local/ascend/models";  
 #endif  
   
     //------------------------  
   
     fp1 = ospath_new_from_posix("/usr/local/hello/");  
     fp2 = ospath_getparent(fp1);  
     fp3 = ospath_new_from_posix("/usr/local");  
   
     D(fp1);  
     D(fp2);  
     D(fp3);  
     assert(ospath_cmp(fp2,fp3)==0);  
     M("Passed 'getparent' test\n");  
   
     ospath_free(fp1); ospath_free(fp2); ospath_free(fp3);  
   
     //------------------------  
   
     fp1 = ospath_new_from_posix("models/johnpye/extfn/extfntest");  
     D(fp1);  
     fp2 = ospath_new("models\\johnpye\\extfn\\extfntest");  
     D(fp2);  
     D(fp1);  
     assert(ospath_cmp(fp1,fp2)==0);  
     M("Passed 'new_from_posix' test\n");  
   
     ospath_free(fp1);  
     ospath_free(fp2);  
   
     //------------------------  
     fp1 = ospath_new(".\\src/.\\images\\..\\\\movies\\");  
     fp2 = ospath_new(".\\src\\movies");  
   
     D(fp1);  
     D(fp2);  
   
     assert(ospath_cmp(fp1,fp2)==0);  
     M("Passed mid-path '..' cleanup test\n");  
   
     ospath_free(fp2);  
   
     fp2 = ospath_new("./src/movies\\kubrick");  
     fp3 = ospath_getparent(fp2);  
   
     D(fp2);  
     D(fp3);  
   
     assert(ospath_cmp(fp1,fp3)==0);  
     M("Passed 'second cleanup' test\n");  
   
     //------------------------  
   
     fp2 = ospath_new("\\home\\john");  
     fp3 = ospath_new("where\\mojo");  
   
     D(fp2);  
     D(fp3);  
   
     ospath_append(fp2,fp3);  
   
     D(fp2);  
   
     fp4 = ospath_new("\\home\\john\\where\\mojo\\");  
   
     D(fp2);  
     assert(ospath_cmp(fp2,fp4)==0);  
     M("Passed 'append' test\n");  
   
     ospath_free(fp3);  
     ospath_free(fp2);  
   
     //---------------------------  
   
     fp3 = ospath_new_noclean("../..");  
     D(fp3);  
   
     // test with appending ../.. to an existing path  
     fp2 = ospath_new("\\home\\john");  
     M("ORIGINAL PATH");  
     D(fp2);  
     ospath_append(fp2,fp3);  
     M("AFTER APPENDING ../..");  
     D(fp2);  
   
     M("GETTING ROOT");  
     fp4 = ospath_root(fp2);  
     M("ROOT FOUND:");  
     D(fp4);  
   
     assert(ospath_cmp(fp2,fp4)==0);  
     M("Passed 'append ../..' test\n");  
   
     ospath_free(fp2);  
     ospath_free(fp3);  
     ospath_free(fp4);  
   
     //-------------------------  
   
     fp1 = ospath_new("~\\somewhere\\..");  
     fp2 = ospath_new("~/.");  
   
     assert(ospath_cmp(fp1,fp2)==0);  
   
     D(fp2);  
   
     ospath_free(fp1);  
     ospath_free(fp2);  
   
     fp1 = ospath_new("/usr/local/include");  
     fp2 = ospath_new("/usr/include/../local/include");  
   
     D(fp1);  
     D(fp2);  
   
     assert(ospath_cmp(fp1,fp2)==0);  
     M("Passed another mid-path '..' test\n");  
   
     ospath_free(fp1);  
     ospath_free(fp2);  
   
     //---------------------------  
   
     fp1 = ospath_new("/home");  
     fp2 = ospath_new("john");  
     fp3 = ospath_concat(fp1, fp2);  
   
     fp4 = ospath_new("/home/john");  
   
     assert(ospath_cmp(fp3,fp4)==0);  
     M("Passed 'ospath_concat' test\n");  
   
     ospath_free(fp1); ospath_free(fp2); ospath_free(fp3); ospath_free(fp4);  
   
     //---------------------------  
   
     fp1 = ospath_new("c:/Program Files");  
     fp2 = ospath_new("GnuWin32\\bin");  
     fp3 = ospath_concat(fp1, fp2);  
   
     fp4 = ospath_new("c:/Program Files/GnuWin32/bin");  
   
     assert(ospath_cmp(fp3,fp4)==0);  
     M("Passed 'ospath_concat' test\n");  
   
     ospath_free(fp1); ospath_free(fp2); ospath_free(fp3); ospath_free(fp4);  
   
     //---------------------------  
   
     fp1 = ospath_new("c:/Program Files/");  
     fp2 = ospath_new("GnuWin32\\bin");  
     fp3 = ospath_concat(fp1, fp2);  
   
     fp4 = ospath_new("c:/Program Files/GnuWin32/bin");  
   
     assert(ospath_cmp(fp3,fp4)==0);  
     M("Passed trailing-slash 'ospath_concat' test\n");  
   
     ospath_free(fp1); ospath_free(fp2); ospath_free(fp3); ospath_free(fp4);  
   
     //---------------------------  
   
     fp1 = ospath_new("c:/Program Files/GnuWin32/bin");  
     fp2 = ospath_new("johnpye/extfn");  
     fp3 = ospath_concat(fp1, fp2);  
   
     fp4 = ospath_new("c:/Program Files/GnuWin32/bin/johnpye/extfn");  
   
     assert(ospath_cmp(fp3,fp4)==0);  
     M("Passed trailing-slash 'ospath_concat' test\n");  
   
     ospath_free(fp1); ospath_free(fp2); ospath_free(fp3); ospath_free(fp4);  
   
     //---------------------------  
   
     pp = ospath_searchpath_new(pathtext);  
   
     for(p1=pp; *p1!=NULL; ++p1){  
         D(*p1);  
     }  
   
 #ifdef WINPATHS  
     fp1 = ospath_new("c:\\program files\\GnuWin32\\bin");  
 #else  
     fp1 = ospath_new("\\Program Files\\GnuWin32\\bin");  
 #endif  
   
     assert(pp[0]!=NULL);  
     assert(fp1!=NULL);  
   
     D(fp1);  
     D(pp[0]);  
   
     assert(ospath_cmp(pp[0],fp1)==0);  
   
   
     fp2 = ospath_new_noclean("johnpye/extfn");  
     D(fp2);  
   
     fflush(stderr);  
   
     fp3 = ospath_searchpath_iterate(pp,&ospath_searchpath_testexists,(void*)fp2);  
   
     assert(fp3!=NULL);  
     D(fp3);  
     D(pp[1]);  
   
     assert(ospath_cmp(fp3,pp[1])==0);  
     M("Passed path-search test\n");  
   
     ospath_free(fp1);  
     ospath_free(fp2);  
     ospath_searchpath_free(pp);  
   
     //-------------------------------  
   
     M("Path-search test 2...");  
   
     pp = ospath_searchpath_new(pathtext2);  
   
     assert(pp!=NULL);  
   
     for (p1=pp; *p1!=NULL; ++p1){  
         D(*p1);  
     }  
   
     fp2 = ospath_new_noclean("johnpye/extfn/extfntest");  
     D(fp2);  
   
     fp3 = ospath_searchpath_iterate(pp,&ospath_searchpath_testexists,(void*)fp2);  
     assert(fp3==NULL);  
   
     M("Passed path-search test 2\n");  
   
     ospath_free(fp2);  
     ospath_free(fp3);  
     ospath_searchpath_free(pp);  
   
     //-------------------------------  
   
     fp1 = ospath_new("/usr/share/data/ascend/models/johnpye/extfn/extfntest.a4c");  
     D(fp1);  
     s1 = ospath_getbasefilename(fp1);  
     X(s1);  
     assert(strcmp(s1,"extfntest.a4c")==0);  
     M("Passed getbasefilename test\n");  
   
     ospath_free(fp1);  
     FREE(s1);  
   
     //-------------------------------  
   
     fp1 = ospath_new("extfntest.a4c");  
     D(fp1);  
     s1 = ospath_getbasefilename(fp1);  
     X(s1);  
     assert(strcmp(s1,"extfntest.a4c")==0);  
     M("Passed getbasefilename test 2\n");  
   
     ospath_free(fp1);  
     FREE(s1);  
   
   
     //-------------------------------  
   
     fp1 = ospath_new("/here/is/my/path.dir/");  
     D(fp1);  
     s1 = ospath_getbasefilename(fp1);  
     X(s1);  
     assert(NULL==s1);  
     M("Passed getbasefilename test 3\n");  
   
     ospath_free(fp1);  
     FREE(s1);  
   
     //-------------------------------  
   
 #ifdef WINPATHS  
     fp1 = ospath_new("c:extfntest.a4c");  
     D(fp1);  
     s1 = ospath_getbasefilename(fp1);  
     X(s1);  
     assert(strcmp(s1,"extfntest.a4c")==0);  
     M("Passed getbasefilename test WINPATHS\n");  
   
     ospath_free(fp1);  
     FREE(s1);  
 #endif  
   
     //-------------------------------  
   
     fp1 = ospath_new("/usr/share/data/ascend/models/johnpye/extfn/extfntest.a4c");  
     D(fp1);  
     s1 = ospath_getfilestem(fp1);  
     X(s1);  
     assert(strcmp(s1,"extfntest")==0);  
     M("Passed getfilestem test\n");  
   
     ospath_free(fp1);  
     FREE(s1);  
   
     //-------------------------------  
   
     fp1 = ospath_new("/usr/share/data/ascend/models/johnpye/extfn/extfntest");  
     D(fp1);  
     s1 = ospath_getfilestem(fp1);  
     X(s1);  
     assert(strcmp(s1,"extfntest")==0);  
     M("Passed getfilestem test 2\n");  
   
     ospath_free(fp1);  
     FREE(s1);  
   
     //-------------------------------  
   
     fp1 = ospath_new("/usr/share/data/ascend/.ascend.ini");  
     D(fp1);  
     s1 = ospath_getfilestem(fp1);  
     X(s1);  
     assert(strcmp(s1,".ascend")==0);  
     M("Passed getfilestem test 3\n");  
   
     ospath_free(fp1);  
     FREE(s1);  
   
     //-------------------------------  
   
     fp1 = ospath_new("~/.vimrc");  
     D(fp1);  
     s1 = ospath_getfilestem(fp1);  
     X(s1);  
     assert(strcmp(s1,".vimrc")==0);  
     M("Passed getfilestem test 3\n");  
   
     ospath_free(fp1);  
     FREE(s1);  
   
     //-------------------------------  
   
     fp1 = ospath_new("~/src/ascend-0.9.5-1.jdpipe.src.rpm");  
     D(fp1);  
     s1 = ospath_getfilestem(fp1);  
     X(s1);  
     assert(strcmp(s1,"ascend-0.9.5-1.jdpipe.src")==0);  
     M("Passed getfilestem test 4\n");  
   
     ospath_free(fp1);  
     FREE(s1);  
   
     //-------------------------------  
   
     fp1 = ospath_new("~/dir1/dir2/");  
     D(fp1);  
     s1 = ospath_getfilestem(fp1);  
     X(s1);  
     assert(NULL==s1);  
     M("Passed getfilestem test 5\n");  
   
     ospath_free(fp1);  
     FREE(s1);  
   
     //-------------------------------  
   
     fp1 = ospath_new("~/src/ascend-0.9.5-1.jdpipe.src.rpm");  
     D(fp1);  
     s1 = ospath_getfileext(fp1);  
     X(s1);  
     assert(strcmp(s1,".rpm")==0);  
     M("Passed getbasefileext test\n");  
   
     ospath_free(fp1);  
     FREE(s1);  
   
     //-------------------------------  
   
     fp1 = ospath_new("~/.vimrc");  
     D(fp1);  
     s1 = ospath_getfileext(fp1);  
     X(s1);  
     assert(s1==NULL);  
     M("Passed getbasefileext test 2\n");  
   
     ospath_free(fp1);  
     FREE(s1);  
   
     //-------------------------------  
   
     fp1 = ospath_new("./ascend4");  
     D(fp1);  
     s1 = ospath_getfileext(fp1);  
     X(s1);  
     assert(s1==NULL);  
     M("Passed getbasefileext test 3\n");  
   
     ospath_free(fp1);  
     FREE(s1);  
   
     //-------------------------------  
   
     fp1 = ospath_new("/home/myfile");  
     fp2 = ospath_getdir(fp1);  
     fp3 = ospath_new("/home");  
     assert(ospath_cmp(fp2,fp3)==0);  
     M("Passed ospath_getdir test\n");  
   
     ospath_free(fp1);  
     ospath_free(fp2);  
     ospath_free(fp3);  
   
     //-------------------------------  
   
     fp1 = ospath_new("/home/myfile.ext");  
     fp2 = ospath_getdir(fp1);  
     fp3 = ospath_new("/home");  
     assert(ospath_cmp(fp2,fp3)==0);  
     M("Passed ospath_getdir test 2\n");  
   
     ospath_free(fp1);  
     ospath_free(fp2);  
     ospath_free(fp3);  
   
     //-------------------------------  
   
     fp1 = ospath_new("/home/mydir/");  
     fp2 = ospath_getdir(fp1);  
     fp3 = ospath_new("/home/mydir");  
     assert(ospath_cmp(fp2,fp3)==0);  
     M("Passed ospath_getdir test 3\n");  
   
     ospath_free(fp1);  
     ospath_free(fp2);  
     ospath_free(fp3);  
   
     //---------------------------------  
     M("ALL TESTS PASSED");  
     return 0;  
 }  
   
 #define DONE_TEST  
   
 #endif  

Legend:
Removed from v.606  
changed lines
  Added in v.607

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