convert line ends
[canaan.git] / prj / tech / libsrc / lg / dpathdir.c
blob8dfe6aeaeb31fee7990d6ef93b91adfc7c0fab63
1 /*
2 * $Header: x:/prj/tech/libsrc/lg/RCS/dpathdir.c 1.8 1998/05/07 10:05:13 DAVET Exp $
4 * Datapath style opendir and readdir commands
6 */
8 #include <datapath.h>
9 #include <lg.h>
10 #include <string.h>
12 EXTERN int _dp_find_flags_tab[4];
14 // stuffs current name
15 char *DatapathDirGetName(DatapathDir *dpd)
17 return dpd->find.name;
20 // stuffs current path into buffer
21 void DatapathDirGetPath(DatapathDir *dpd,char *s)
23 char *p;
25 if (dpd->dp->datapath[dpd->curp])
26 strcpy(s,dpd->dp->datapath[dpd->curp]);
27 else
28 s[0]=0;
30 strcat(s,dpd->path);
32 // work back to start or delineation
33 p=s+strlen(s);
34 while (p>=s && *p!='/' && *p!='\\' && *p!=':')
35 --p;
37 ++p;
38 strcpy(p,dpd->find.name);
41 DatapathDir *DatapathOpenDir(Datapath *dpath,char *name,int flags)
43 DatapathDir *dpd;
45 dpd = (DatapathDir *)Malloc(sizeof(DatapathDir));
47 // set data path and name
48 dpd->dp = dpath;
49 strcpy(dpd->path,name);
50 dpd->curp = 0;
51 dpd->cur = 0;
52 dpd->flags = flags;
54 return dpd;
57 char *DatapathReadDir(DatapathDir *dpd)
59 int err;
60 char path[128];
62 // works on a null datapath
63 while((dpd->dp->datapath[dpd->curp]!=NULL) || (dpd->curp==0))
65 // first time for this one
66 if (dpd->cur == 0) {
67 if (dpd->dp->datapath[dpd->curp]) {
68 strcpy(path,dpd->dp->datapath[dpd->curp]);
69 } else {
70 path[0] = 0;
73 strcat(path,dpd->path);
74 #if defined(__WATCOMC__) || defined(__SC__)
75 err = _dos_findfirst(path,_dp_find_flags_tab[dpd->dp->find_flags],&(dpd->find));
76 #else
77 // old code was
78 // err = (dpd->findfp =_findfirst(path, &(dpd->find)) != -1)?0:1;
79 // which should have been
80 // err = ( (dpd->findfp =_findfirst(path, &(dpd->find))) != -1)?0:1;
81 // but we will write it to be readable
82 dpd->findfp =_findfirst(path, &(dpd->find));
83 err = dpd->findfp != -1 ? 0 : 1 ;
84 #endif
85 } else {
86 #if defined(__WATCOMC__) || defined(__SC__)
87 err = _dos_findnext(&(dpd->find));
88 #else
89 err = _findnext(dpd->findfp,&(dpd->find));
90 #endif
93 // if there was not a read error
94 if (err==0)
95 { // Screen out dot and double dot, because it's dumb.
96 dpd->cur++;
97 if (dpd->flags & DP_SCREEN_DOT)
98 if (strcmp(dpd->find.name,".")==0 || strcmp(dpd->find.name,"..")==0)
99 continue; // got dot, so we want to scan past it
100 break; // break out, since we have found a real file
103 #if defined(__WATCOMC__) || defined(__SC__)
104 _dos_findclose(&(dpd->find));
105 #else
106 _findclose(dpd->findfp);
107 #endif
108 dpd->curp++;
109 dpd->cur=0;
112 if (err!=0) return NULL;
113 return dpd->find.name;
116 void DatapathCloseDir(DatapathDir *dpd)
118 if (dpd->cur!=0) {
119 #if defined(__WATCOMC__) || defined(__SC__)
120 _dos_findclose(&dpd->find);
121 #else
122 _findclose(dpd->findfp);
123 #endif
126 Free(dpd);