1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/libraries/libmbutil/fn_UNIX.cc,v 1.24 2017/01/12 14:44:04 masarati Exp $ */
2 /*
3  * This library comes with MBDyn (C), a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati  <masarati@aero.polimi.it>
9  *
10  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
11  * via La Masa, 34 - 20156 Milano, Italy
12  * http://www.aero.polimi.it
13  *
14  * Changing this copyright notice is forbidden.
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation (version 2 of the License).
19  *
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29  */
30 
31 /* Codice relativo alla classe File_name, per l'handling di nomi di files
32  * in ambiente DOS e UNIX */
33 
34 #include "mbconfig.h"           /* This goes first in every *.c,*.cc file */
35 
36 #include "filename.h"
37 #include "myassert.h"
38 #include "mynewmem.h"
39 
40 
FileName(const char * sFName,int iExtSepNum)41 FileName::FileName(const char *sFName, int iExtSepNum)
42 : sName(NULL), sExt(NULL), sRef(NULL)
43 {
44    	if (sFName != NULL) {
45       		iInit(sFName, iExtSepNum);
46    	}
47 }
48 
~FileName(void)49 FileName::~FileName(void)
50 {
51    	if (sName != NULL) {
52       		/* delete []sName; */
53       		SAFEDELETEARR(sName);
54    	}
55 
56    	if (sExt != NULL) {
57       		/* delete []sExt; */
58       		SAFEDELETEARR(sExt);
59    	}
60 }
61 
62 int
iInit(const char * sFName,int iExtSepNum)63 FileName::iInit(const char *sFName, int iExtSepNum)
64 {
65    	ASSERT(sFName != NULL);
66 
67    	if (sFName == NULL) {
68       		return 0;
69    	}
70 
71    	unsigned int iNewSize = strlen(sFName);
72    	if ((sName == NULL) || (iNewSize > iMaxSize)) {
73       		if (sName != NULL) {
74 	 		SAFEDELETEARR(sName);
75 			sName = NULL;
76       		}
77       		iMaxSize = iNewSize;
78       		SAFENEWARR(sName, char, iMaxSize + 1);
79    	}
80 
81    	strcpy(sName, sFName);
82    	sRef = (char *)sName + strlen(sName);
83    	ASSERT(sRef[0] == '\0');
84 
85    	if (iExtSepNum > 0) {  /* se si parte dall'inizio */
86       		while (--sRef > sName) {  /* cerca il simbolo "/" */
87 	 		if (sRef[0] == DIR_SEP) {
88 	    			break;
89 	 		}
90       		}
91 
92       		int iCnt = 0;
93       		while (sRef[0] != '\0') {  /* cerca il "." n. iExtSepNum */
94 	 		if (sRef[0] == EXT_SEP) {
95 	    			iCnt++;
96 	    			if (iCnt == iExtSepNum) {
97 	       				goto label;
98 	    			}
99 	 		}
100 	 		sRef++;
101       		}
102 
103 label:
104       		iNewSize = strlen(sRef);
105       		if (sExt == NULL || strlen(sExt) < iNewSize) {
106 			/* se c'e' gia' sExt lo cancella */
107 	 		if (sExt != NULL) {
108 	    			SAFEDELETEARR(sExt);
109 	 		}
110 	 		SAFENEWARR(sExt, char, iNewSize+1);
111       		}
112 
113       		ASSERT(sRef != NULL);
114       		strcpy(sExt, sRef);
115       		sRef[0] = '\0';
116    	} else if (iExtSepNum < 0) {  /* se si parte dalla fine */
117       		iExtSepNum = -iExtSepNum;
118       		while (--sRef > sName) {  /* cerca il simbolo "/" */
119 	 		if (sRef[0] == DIR_SEP) {
120 	    			break;
121 	 		}
122       		}
123 
124       		int iCnt = 0;
125       		char *sLim = sRef;
126       		sRef = (char *)sName + strlen(sName);
127       		while (--sRef > sLim) {  /* cerca il "." n. iExtSepNum */
128 	 		if (sRef[0] == EXT_SEP) {
129 	    			iCnt++;
130 	    			if (iCnt == iExtSepNum) {
131 	       				goto label2;
132 	    			}
133 	 		}
134       		}
135       		sRef = (char *)sName + strlen(sName);
136 
137 label2:
138       		iNewSize = strlen(sRef);
139       		if (sExt == NULL || strlen(sExt) < iNewSize) {
140 	 		if (sExt != NULL) {
141 	    			SAFEDELETEARR(sExt);
142 	 		}
143 	 		SAFENEWARR(sExt, char, iNewSize + 1);
144       		}
145 
146       		ASSERT(sRef != NULL);
147       		strcpy(sExt, sRef);
148       		sRef[0] = '\0';
149    	} else {
150 		if (sExt == 0) {
151       			SAFENEWARR(sExt, char, 1);
152 		}
153       		sExt[0] = '\0';
154    	}
155 
156    	iCurSize = iMaxSize - strlen(sExt);
157    	return iCurSize;
158 }
159 
160 const char *const
_sPutExt(const char * sEName)161 FileName::_sPutExt(const char *sEName)
162 {
163    	if (sEName == NULL) {
164       		sEName = sExt;
165    	}
166 
167    	unsigned int uExtLen = strlen(sEName);
168    	if (sEName[0] != '\0' && sEName[0] != EXT_SEP) {
169      		uExtLen++;
170    	}
171 
172    	if (iCurSize + uExtLen > iMaxSize) {
173       		char *sTmp = NULL;
174       		SAFENEWARR(sTmp, char, iCurSize + uExtLen + 1);
175 
176       		ASSERT(sName != NULL);
177       		strcpy(sTmp, sName);
178       		sRef = sTmp + (sRef - sName);
179       		SAFEDELETEARR(sName);
180       		sName = sTmp;
181 		iMaxSize = iCurSize + uExtLen;
182    	}
183 
184    	ASSERT(sRef != NULL);
185    	ASSERT(sEName != NULL);
186 
187    	if (sEName[0] != '\0') {
188       		char *sTmp = sRef;
189       		if (sEName[0] != EXT_SEP) {
190 	 		sTmp[0] = EXT_SEP;
191 			sTmp++;
192       		}
193       		strcpy(sTmp, sEName);
194    	}
195 
196    	return sName;
197 }
198 
199 const char *const
sGet(void) const200 FileName::sGet(void) const
201 {
202    	return const_cast<FileName *>(this)->_sPutExt(0);
203 }
204 
205 int
is_abs_path(const char * const p)206 is_abs_path(const char *const p)
207 {
208 	ASSERT(p != 0);
209 
210 #ifdef _WIN32
211 	if ((strstr(p, ":\\") != 0) || (strstr(p, ":/") != 0)) {
212 		return 1;
213 	}
214 #else // ! _WIN32
215 	if (p[0] == '/') {
216 		return 1;
217 	}
218 #endif // ! _WIN32
219 
220 	return 0;
221 }
222 
223