File: path.cxx

package info (click to toggle)
curves 0.8.7
  • links: PTS
  • area: main
  • in suites: slink
  • size: 704 kB
  • ctags: 1,001
  • sloc: cpp: 6,197; ansic: 519; makefile: 270; sh: 192; fortran: 149
file content (452 lines) | stat: -rw-r--r-- 9,800 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/* path.cxx
     $Id: path.cxx,v 1.3 1998/07/27 05:39:07 elf Exp $

   written by Marc Singer
   27 October 1996

   This file is part of the project CurVeS.  See the file README for
   more information.

   Copyright (C) 1996 Marc Singer

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   in a file called COPYING along with this program; if not, write to
   the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
   02139, USA.

   -----------
   DESCRIPTION
   -----------

*/

  /* ----- Includes */

#include "std.h"
#include <sys/stat.h>
#include <pwd.h>		// Password file parsing routines
#include "path.h"


  /* ----- Class Globals/Statics */

#define CB_PATH_ALLOC		32
#define CH_PATHSEP		'/'
#define SZ_PATHSEP		"/"

  /* ----- Methods */

/* LPath::alloc

   on return, it guarantees that there are at least cchNeed bytes
   available for path data.  We make sure there is one more byte
   available for a terminating NULL.

*/

void LPath::alloc (int cchNeed)
{
  ++cchNeed;			// Space for a NULL
  if (cchNeed <= m_cchMax)
    return;

  int cchNeedNew = (((cchNeed + CB_PATH_ALLOC - 1)/CB_PATH_ALLOC)
		    *CB_PATH_ALLOC);
  assert_ (cchNeedNew >= cchNeed);
  char* sz = (char*) malloc (cchNeedNew);
  assert_ (sz);
  if (m_sz) {
    memcpy (sz, m_sz, m_cchMax);
    free (m_sz);
    m_sz = NULL;
  }  /* if */
  else
    *sz = 0;
  m_sz = sz;
  m_cchMax = cchNeedNew;
}  /* LPath::alloc */


/* LPath::cat

   appends a complete path element to the end of the current path.
   A complete element is a directory or filename.  To append a piece
   of a pathname, use cat_literal.

*/

void LPath::cat (const char* sz)
{
  int cch = length ();
  assert_ (sz);
  int cchNeed = cch + strlen (sz) + 1;		// +1 for separator
  alloc (cchNeed);

        // Put a path separator at the end
  if (cch && m_sz[cch - 1] != CH_PATHSEP)
    strcat (m_sz + cch, SZ_PATHSEP);

  strcat (m_sz, sz);
  clean ();
}  /* LPath::cat */


void LPath::cat_literal (const char* sz)
{
  alloc (length () + strlen (sz) + 1);
  strcat (m_sz, sz);
}  /* LPath::cat_literal */


/* LPath::clean

   convert path to a connicalized form.  We collapse references to '.'
   and to '..'.  We do permit a leading './' as well as any number of
   leading '../'.

   We also interpret ~user expansions.  In cleaning the path, we
   convert the ~ notation to the full name of the home directory.

*/

void LPath::clean (void)
{
  if (!m_sz)
    return;

				// -- Substitution for ~username
  if (*m_sz == '~') {
    int cchUser = strcspn (m_sz + 1, "/");
    setpwent ();
    struct passwd* pEnt;
    while ((pEnt = getpwent ())) {
      if (cchUser) {
	if (strncmp (pEnt->pw_name, m_sz + 1, cchUser) == 0
	    && !pEnt->pw_name[cchUser])
	  break;
      }
      else if (pEnt->pw_uid == getuid ())
	break;
    }
    assert_ (pEnt);		// FIXME: we really should have a fallback
    int cchDir = strlen (pEnt->pw_dir);
    if (pEnt->pw_dir[cchDir - 1] == '/' && m_sz[cchUser + 1] == '/')
      --cchDir;
    alloc (length () + cchDir - (cchUser + 1));
    memmove (m_sz + cchDir, m_sz + cchUser + 1, length () - (cchUser + 1) + 1);
    memcpy (m_sz, pEnt->pw_dir, cchDir);
  }

#if defined (MSDOS)
        // Make all path separators forward slashes
  int cch = length ();
  char* pch = m_sz;
  for (; cch--; ++pch)
    if (*pch == '\\')
      *pch = CH_PATHSEP;
#endif

  char* pchPrefix = m_sz;
  	// Skip over prefix of "../"'s or "./"
  for (; *pchPrefix && !strncmp (pchPrefix, "../", 3);
       pchPrefix += 3)
    ;
  if (pchPrefix == m_sz && !strncmp (pchPrefix, "./", 2))
    pchPrefix += 2;

        // -- Collapse '.' and '..' elements
  char* pch = m_sz + length ();
  char* pchSep = pch;
  int state = 0;
  int cchCopy = 0;
  for (int cDelete = 0; --pch > pchPrefix;) {
    ++cchCopy;
        // -- Process path elements on encountering path separators
    if (*pch == CH_PATHSEP) {
      ++cchCopy;
      switch (state) {
      case 2:			// Reference to parent directory '..'
	cDelete += 2;		// Delete this `..' and preceeding element
		// -- fall through
      case -1:			// Ordinary path element
      case 0:			// Empty path element, reference to `//'?
	if (!cDelete)
	  break;
	else
	  --cDelete;
                // -- fall through
      case 1:		// Reference to current directory '.'
	cchCopy -= pchSep - pch;
	memmove (pch, pchSep, cchCopy);
	break;
      }  /* switch */
      pchSep = pch;
      state = 0;
      continue;
    }  /* if */

        // -- Non-special path characters
    if (*pch != '.') {
      state = -1;
      continue;
    }  /* if */

        // -- Analyze special directory references
    switch (state) {
      default:
        break;

      case 0:	// Beginning of element
        state = 1;
        break;

      case 1:	// "." element already found
        state = 2;
        break;

      case 2:	// ".." element already found
        state = -1;
        break;
    }  /* switch */
  }  /* for */
}  /* LPath::clean */


/* LPath::dup

   duplicates the contents of the given string in this->m_sz.

*/

void LPath::dup (const char* sz)
{
  erase ();
  if (!sz || !*sz) {
    alloc (0);
    *m_sz = 0;
  }
  else {
    int cch = strlen (sz);
    alloc (cch);
    strncpy (m_sz, sz, cch + 1);
  }  /* if */
}  /* LPath::dup */

void LPath::dup (const char* sz, int cch)
{
  erase ();
  if (!sz || !*sz || !cch) {
    alloc (0);
    *m_sz = 0;
  }
  else {
    alloc (cch);
    strncpy (m_sz, sz, cch);
    m_sz[cch] = 0;
  }  /* if */
}  /* LPath::dup */


/* LPath::extension

   returns a pointer to the filename extension, NULL if there isn't
   one.  The return value points to the '.' of the extension if one is
   found.

*/

const char* LPath::extension (void)
{
  char* pch = m_sz;

	// Find the last path element
  int cch;
  while (pch[cch = strcspn (pch, SZ_PATHSEP)])
    pch += cch + 1;

	// Find the extension
  for (; *pch; ++pch)
    if (*pch == '.')
      return pch;

  return NULL;
}  /* LPath::extension */


/* LPath::init

   zeroes the path.

*/

void LPath::init (void)
{
  m_sz = NULL;
  m_cchMax = 0;
}  /* LPath::init */


/* LPath::is_relative

   returns true if the path is NOT absolute.  Absolute paths have a
   driver letter prefix (MSDOS) or a leading '/' that specifies a path
   starting at the filesystem root.

*/

bool LPath::is_relative (void) const
{
  return m_sz && !(   *m_sz == CH_PATHSEP
#if defined (MSDOS) && defined (CH_DRIVESEP)
		   || (m_cchMax && m_sz[1] == CH_DRIVESEP && isalpha(*m_sz))
#endif
		   );
}  /* LPath::is_relative */


/* LPath::relative_to

   make this->m_sz path relative to the directory szDir.  It returns
   true if the path has changed and is valid.

*/

bool LPath::relative_to (const char* szDir)
{
  char* szPath = m_sz;

	// -- Degenerative case of CWD as szDir
  if (!strcmp (szDir, "."))
    return true;

	// -- Strip `./' from CWD relative paths
  if (!strncmp (szPath, "./", 2))
      szPath += 2;
  if (!strncmp (szDir, "./", 2))
      szDir += 2;

	// -- Check for absolute/relative mismatch
  if ((*szPath == CH_PATHSEP || *szDir == CH_PATHSEP) && *szDir != *szPath)
    return false;

//  int cchPath = strlen (szPath);
  int cchDir = strlen (szDir);

  	// -- Degenerative case: no directory
  if (cchDir == 0)
    return true;

  	// -- Find length of common prefix
  int cchPrefix;
  for (cchPrefix = 0;
       szPath[cchPrefix] && szDir[cchPrefix]
       && szPath[cchPrefix] == szDir[cchPrefix];
       ++cchPrefix)
    ;
  if (!szDir[cchPrefix] && szPath[cchPrefix] == CH_PATHSEP)
    ++cchPrefix;

  	// -- Degenerative case: directory equivalent to path
//  if (cchPrefix == cchPath)
//    return false;

	// -- Determine length of common root path (includes final sep. or \0)
  int cchRoot;
  for (cchRoot = cchPrefix; cchRoot; --cchRoot) {
    if (  (szDir [cchRoot - 1] == CH_PATHSEP || !szDir[cchRoot - 1])
	&& szPath[cchRoot - 1] == CH_PATHSEP)
      break;
  }  /* for */

	// -- Count ascending directories from szDir to common root
  int iDirAscend = 0;
  if (cchRoot < cchDir) {
    if (!cchRoot)
      ++iDirAscend;
    for (int ich = cchRoot ? cchRoot - 1 : 0; szDir[ich]; ++ich)
      if (szDir[ich] == CH_PATHSEP && szDir[ich + 1])
	++iDirAscend;
  }  /* if */

  LPath pathNew;
  if (iDirAscend) {
    while (iDirAscend--)
      pathNew.cat ("..");
  }  /* if */
  else
    pathNew.cat (".");

  pathNew.cat (szPath + cchRoot);
  *this = pathNew;
  return true;
}  /* LPath::relative_to */


/* LPath::release

   release memory.

*/

void LPath::release (void)
{
  if (m_sz)
    free (m_sz);
  m_sz = NULL;
  m_cchMax = 0;
}  /* LPath::release */


/* LPath::tail

   returns a const char* the last path element.

*/

const char* LPath::tail (void)
{
  char* pch = m_sz;

  int cch;
  while (pch[cch = strcspn (pch, SZ_PATHSEP)])
    pch += cch + 1;

  return pch;
}  /* LPath::tail */


/* LPath::trim

   trims the last path element, file or directory, from the path.

*/

void LPath::trim (void)
{
  char* pch = m_sz;
  char* pchSep = NULL;


  int cch;
  while (pch[cch = strcspn (pch, SZ_PATHSEP)]) {
    pchSep = pch + cch;
    pch += cch + 1;
  }  /* while */

#if defined (MSDOS) && defined (CH_DRIVESEP)
  if (pchSep && pchSep != m_sz && *(pchSep - 1) == CH_DRIVESEP)
    ++pchSep;
#endif

  *pchSep = 0;
}  /* LPath::trim */