ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/crossedit/Str.c
Revision: 1.1
Committed: Fri Feb 3 07:11:44 2006 UTC (18 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
Branch point for: UPSTREAM
Log Message:
Initial revision

File Contents

# User Rev Content
1 root 1.1 /*
2     * CrossEdit - game world editor
3     * Copyright (C) 1993 Jarkko Sonninen & Petri Heinila
4     *
5     * This program is free software; you can redistribute it and/or modify
6     * it under the terms of the GNU General Public License as published by
7     * the Free Software Foundation; either version 2 of the License, or
8     * (at your option) any later version.
9     *
10     * This program is distributed in the hope that it will be useful,
11     * but WITHOUT ANY WARRANTY; without even the implied warranty of
12     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13     * GNU General Public License for more details.
14     *
15     * You should have received a copy of the GNU General Public License
16     * along with this program; if not, write to the Free Software
17     * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18     *
19     * The authors can be reached via e-mail to Jarkko.Sonninen@lut.fi
20     * or Petri.Heinila@lut.fi .
21     */
22    
23     #include <Posix.h>
24     #include <Ansi.h>
25     #include <Str.h>
26    
27     #include <global.h>
28     #include <debug.h>
29    
30     #ifdef StrHasGeneratedSource
31     #include <StrG.c>
32     #endif
33    
34     /**********************************************************************
35     * private
36     **********************************************************************/
37    
38    
39     /**********************************************************************
40     * public
41     **********************************************************************/
42    
43     /*
44     * member: makes absolute pathname from relative
45     * src : migration ???
46     * return: result
47     */
48     char *StrPathNormalize (char *this,char *src) {
49     char *p, *q;
50     char buf[MAX_BUF];
51     static char path[MAX_BUF];
52    
53     LOG(llevDebug,"path before normalization >%s<>%s<\n", src, this);
54    
55     if (*this == '/') {
56     strcpy (buf, this);
57    
58     } else {
59     strcpy (buf, src);
60     if ((p = strrchr (buf, '/')))
61     p[1] = '\0';
62     else
63     strcpy (buf, "/");
64     strcat (buf, this);
65     }
66    
67     q = p = buf;
68     while ((q = strstr (q, "//")))
69     p = ++q;
70    
71     *path = '\0';
72     q = path;
73     p = strtok (p, "/");
74     while (p) {
75     if (!strcmp (p, "..")) {
76     q = strrchr (path, '/');
77     if (q)
78     *q = '\0';
79     else {
80     *path = '\0';
81     LOG (llevError, "Illegal path.\n");
82     }
83     } else {
84     strcat (path, "/");
85     strcat (path, p);
86     }
87     p = strtok (NULL, "/");
88     }
89     LOG(llevDebug,"path after normalization >%s<\n", path);
90    
91     return (path);
92     }
93    
94     /*
95     * function: make new abs. path after cd
96     * cwd : current working directory, this have to be abs. path
97     * this have to be size PATH_MAX + 1, result is put to this
98     * cd : migration
99     * return : pointer to cwd, or NULL on error
100     */
101     char *StrPathCd(char *cwd,const char *cd)
102     {
103     char *fields[PATH_MAX / 2 - 1],*fld,cdMove[PATH_MAX+1];
104     char clt[PATH_MAX+1];
105     int numFields,i;
106    
107     if(*cwd != '/') return NULL;
108     if(*cd == '/') return strcpy(cwd,cd);
109    
110     /*** load cwd by parts ***/
111     numFields = 0;
112     if((fld = strtok(cwd,"/"))) {
113     do {
114     fields[numFields] = fld;
115     numFields++;
116     } while((fld = strtok(NULL,"/")));
117     }
118     /*** traverse path by cd ***/
119     numFields--; /* filename in cwd off */
120     strcpy(cdMove,cd);
121     if((fld = strtok(cdMove,"/"))) {
122     do {
123     if(!strcmp(fld,".")) continue;
124     if(!strcmp(fld,"..")) {
125     numFields--;
126     } else {
127     fields[numFields] = fld;
128     numFields++;
129     }
130     } while((fld = strtok(NULL,"/")));
131     }
132     /*** collect them ***/
133     strcpy(clt,"");
134     for(i=0; i < numFields; i++) {
135     strcat(clt,"/");
136     strcat(clt,fields[i]);
137     }
138     return strcpy(cwd,clt);
139     }
140    
141     /*
142     * function: genrestes reletive cd argument, in which can move from
143     * cwd to dst
144     * cwd : currenntr working directory, gerenerated values is subtituted to
145     * this
146     * dst : destination directory
147     * return : pointer to cwd, or NULL on error
148     */
149     char *StrPathGenCd(char *cwd,const char *dstArg)
150     {
151     #if 0
152     int i,j,cnt;
153     char buf[BUFSIZ];
154    
155     for(i=0; dst[i] && cwd[i] && dst[i] == cwd[i]; i++);
156    
157     debug2("cwd:%s dst:%s\n",cwd + i,dst + i);
158    
159     if(strtok(cwd + i,"/")) {
160     cnt += 1;
161     while(strtok(NULL,"/")) cnt++;
162     }
163     buf[0] = 0;
164     if(cnt > 0) {
165     for(j=0; j < cnt; j++) strcat(buf,"../");
166     strcat(buf,dst + i);
167     } else {
168     strcpy(buf,dst + i + 1);
169     }
170     #else
171     char dst[PATH_MAX+1];
172     char *cwdFields[PATH_MAX / 2 - 1],*dstFields[PATH_MAX / 2 - 1];
173     int cwdNum,dstNum,i,start;
174     char *fld,buf[PATH_MAX+1];
175    
176     /*** save dst ***/
177     strcpy(dst,dstArg);
178     /*** load cwd by parts ***/
179     cwdNum = 0;
180     if((fld = strtok(cwd,"/"))) {
181     do {
182     cwdFields[cwdNum] = fld;
183     cwdNum++;
184     } while((fld = strtok(NULL,"/")));
185     }
186     /*** load dst by parts ***/
187     dstNum = 0;
188     if((fld = strtok(dst,"/"))) {
189     do {
190     dstFields[dstNum] = fld;
191     dstNum++;
192     } while((fld = strtok(NULL,"/")));
193     }
194     strcpy(buf,".");
195     /*** wile same ***/
196     for(start=0;
197     start < cwdNum && start < dstNum &&
198     !strcmp(cwdFields[start],dstFields[start]);
199     start++);
200     /*** whilke guing up***/
201     for(i=start; i < cwdNum - 1; i++)
202     strcat(buf,"/..");
203     #if 0
204     if(*buf)
205     buf[strlen(buf)-1] = '\0';
206     #endif
207     /*** while gong down ***/
208     for(i=start; i < dstNum; i++) {
209     strcat(buf,"/");
210     strcat(buf,dstFields[i]);
211     }
212     #endif
213     return strcpy(cwd,buf);
214     }
215    
216     /*
217     * function: gets base-part of filename as basename(1)
218     * basename: allocated space for name
219     * filename: destination directory
220     * return : pointer to basename, or NULL on error
221     */
222     char *StrBasename(char *basename,const char *filename)
223     {
224     char buf[PATH_MAX+1],*fields[PATH_MAX / 2 - 1],*fld;
225     int num;
226    
227     strcpy(buf,filename);
228     /*** load cwd by parts ***/
229     num = 0;
230     if((fld = strtok(buf,"/"))) {
231     do {
232     fields[num] = fld;
233     num++;
234     } while((fld = strtok(NULL,"/")));
235     } else {
236     return strcpy(basename,"");
237     }
238     return strcpy(basename,fields[num - 1]);
239     }
240    
241     /*** end of Str.c ***/