Subversion Repositories tendra.SVN

Rev

Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2 Rev 7
Line -... Line 1...
-
 
1
/*
-
 
2
 * Copyright (c) 2002-2006 The TenDRA Project <http://www.tendra.org/>.
-
 
3
 * All rights reserved.
-
 
4
 *
-
 
5
 * Redistribution and use in source and binary forms, with or without
-
 
6
 * modification, are permitted provided that the following conditions are met:
-
 
7
 *
-
 
8
 * 1. Redistributions of source code must retain the above copyright notice,
-
 
9
 *    this list of conditions and the following disclaimer.
-
 
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
-
 
11
 *    this list of conditions and the following disclaimer in the documentation
-
 
12
 *    and/or other materials provided with the distribution.
-
 
13
 * 3. Neither the name of The TenDRA Project nor the names of its contributors
-
 
14
 *    may be used to endorse or promote products derived from this software
-
 
15
 *    without specific, prior written permission.
-
 
16
 *
-
 
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
-
 
18
 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
-
 
19
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
-
 
20
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
-
 
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
-
 
22
 * EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-
 
23
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
-
 
24
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
-
 
25
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
-
 
26
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
-
 
27
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
 
28
 *
-
 
29
 * $Id$
-
 
30
 */
1
/*
31
/*
2
    		 Crown Copyright (c) 1997
32
    		 Crown Copyright (c) 1997
3
    
33
 
4
    This TenDRA(r) Computer Program is subject to Copyright
34
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
35
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
36
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
37
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
38
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
39
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
40
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
41
    shall be deemed to be acceptance of the following conditions:-
12
    
42
 
13
        (1) Its Recipients shall ensure that this Notice is
43
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
44
        reproduced upon any copies or amended versions of it;
15
    
45
 
16
        (2) Any amended version of it shall be clearly marked to
46
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
47
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
48
        for the relevant amendment or amendments;
19
    
49
 
20
        (3) Its onward transfer from a recipient to another
50
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
51
        party shall be deemed to be that party's acceptance of
22
        these conditions;
52
        these conditions;
23
    
53
 
24
        (4) DERA gives no warranty or assurance as to its
54
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
55
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
56
        no liability whatsoever in relation to any use to which
27
        it may be put.
57
        it may be put.
28
*/
58
*/
Line 60... Line 90...
60
#include "cstring.h"
90
#include "cstring.h"
61
 
91
 
62
/*--------------------------------------------------------------------------*/
92
/*--------------------------------------------------------------------------*/
63
 
93
 
64
CStringP
94
CStringP
65
file_name_basename PROTO_N ((path))
95
file_name_basename(CStringP path)
66
		   PROTO_T (CStringP path)
-
 
67
{
96
{
68
    CStringP last = cstring_find_reverse (path, '/');
97
    CStringP last = cstring_find_reverse(path, '/');
69
 
98
 
70
    if (last) {
99
    if (last) {
71
	return (cstring_duplicate (last + 1));
100
	return(cstring_duplicate(last + 1));
72
    } else {
101
    } else {
73
	return (cstring_duplicate (path));
102
	return(cstring_duplicate(path));
74
    }
103
    }
75
}
104
}
76
 
105
 
77
CStringP
106
CStringP
78
file_name_dirname PROTO_N ((path))
107
file_name_dirname(CStringP path)
79
		  PROTO_T (CStringP path)
-
 
80
{
108
{
81
    CStringP last = cstring_find_reverse (path, '/');
109
    CStringP last = cstring_find_reverse(path, '/');
82
 
110
 
83
    if (last) {
111
    if (last) {
84
	return (cstring_duplicate_prefix (path, (unsigned) (last - path)));
112
	return(cstring_duplicate_prefix(path,(unsigned)(last - path)));
85
    } else {
113
    } else {
86
	return (cstring_duplicate (path));
114
	return(cstring_duplicate(path));
87
    }
115
    }
88
}
116
}
89
 
117
 
90
CStringP
118
CStringP
91
file_name_expand PROTO_N ((dir, name, suffix))
119
file_name_expand(CStringP dir,			  CStringP name, 
92
		 PROTO_T (CStringP dir X
-
 
93
			  CStringP name X
-
 
94
			  CStringP suffix)
120
			  CStringP suffix)
95
{
121
{
96
    unsigned dir_length  = cstring_length (dir);
122
    unsigned dir_length  = cstring_length(dir);
97
    unsigned name_length = cstring_length (name);
123
    unsigned name_length = cstring_length(name);
98
    unsigned suf_length  = (suffix ? (cstring_length (suffix)) : 0);
124
    unsigned suf_length  = (suffix ?(cstring_length(suffix)): 0);
99
    unsigned suf_extra   = (unsigned) (suffix ? 1 : 0);
125
    unsigned suf_extra   = (unsigned)(suffix ? 1 : 0);
100
    unsigned extra;
126
    unsigned extra;
101
    unsigned length;
127
    unsigned length;
102
    CStringP path;
128
    CStringP path;
103
    CStringP tmp;
129
    CStringP tmp;
104
 
130
 
105
    if ((dir_length > 0) && (dir [dir_length - 1] != '/')) {
131
    if ((dir_length > 0) && (dir[dir_length - 1]!= '/')) {
106
	extra = 1;
132
	extra = 1;
107
    } else {
133
    } else {
108
	extra = 0;
134
	extra = 0;
109
    }
135
    }
110
    length = (dir_length + extra + name_length + suf_extra + suf_length + 1);
136
    length = (dir_length + extra + name_length + suf_extra + suf_length + 1);
111
    path   = ALLOCATE_VECTOR (char, length);
137
    path   = ALLOCATE_VECTOR(char, length);
112
    tmp    = path;
138
    tmp    = path;
113
    if (dir_length > 0) {
139
    if (dir_length > 0) {
114
	(void) memcpy ((GenericP) tmp, (GenericP) dir, (SizeT) dir_length);
140
	(void)memcpy((GenericP)tmp,(GenericP)dir,(SizeT)dir_length);
115
	tmp += dir_length;
141
	tmp += dir_length;
116
	if (dir [dir_length - 1] != '/') {
142
	if (dir[dir_length - 1]!= '/') {
117
	    tmp [0] = '/';
143
	    tmp[0] = '/';
118
	    tmp ++;
144
	    tmp++;
119
	}
145
	}
120
    }
146
    }
121
    (void) memcpy ((GenericP) tmp, (GenericP) name, (SizeT) name_length);
147
   (void)memcpy((GenericP)tmp,(GenericP)name,(SizeT)name_length);
122
    tmp += name_length;
148
    tmp += name_length;
123
    if (suffix) {
149
    if (suffix) {
124
	tmp [0] = '.';
150
	tmp[0] = '.';
125
	tmp ++;
151
	tmp++;
126
	(void) memcpy ((GenericP) tmp, (GenericP) suffix, (SizeT) suf_length);
152
	(void)memcpy((GenericP)tmp,(GenericP)suffix,(SizeT)suf_length);
127
	tmp += suf_length;
153
	tmp += suf_length;
128
    }
154
    }
129
    tmp [0] = '\0';
155
    tmp[0] = '\0';
130
    return (path);
156
    return(path);
131
}
157
}
132
 
158
 
133
BoolT
159
BoolT
134
file_name_is_basename PROTO_N ((path))
160
file_name_is_basename(CStringP path)
135
		      PROTO_T (CStringP path)
-
 
136
{
161
{
137
    return (!cstring_contains (path, '/'));
162
    return(!cstring_contains(path, '/'));
138
}
163
}
139
 
164
 
140
BoolT
165
BoolT
141
file_name_is_absolute PROTO_N ((path))
166
file_name_is_absolute(CStringP path)
142
		      PROTO_T (CStringP path)
-
 
143
{
167
{
144
    return (path [0] == '/');
168
    return(path[0] == '/');
145
}
169
}
146
 
170
 
147
void
171
void
148
file_name_populate PROTO_N ((path))
172
file_name_populate(CStringP path)
149
		   PROTO_T (CStringP path)
-
 
150
{
173
{
151
#ifdef FS_MKDIR
174
#ifdef FS_MKDIR
152
    CStringP new_path = cstring_duplicate (path);
175
    CStringP new_path = cstring_duplicate(path);
153
    CStringP tmp      = cstring_find (new_path, '/');
176
    CStringP tmp      = cstring_find(new_path, '/');
154
 
177
 
155
    if (tmp) {
178
    if (tmp) {
156
	do {
179
	do {
157
	    *tmp = '\0';
180
	    *tmp = '\0';
158
	    (void) mkdir (new_path, 0755);
181
	   (void)mkdir(new_path, 0755);
159
	    *tmp = '/';
182
	    *tmp = '/';
160
	} while ((tmp = cstring_find (tmp + 1, '/')) != NIL (CStringP));
183
	} while ((tmp = cstring_find(tmp + 1, '/')) != NIL(CStringP));
161
    }
184
    }
162
    DEALLOCATE (new_path);
185
    DEALLOCATE(new_path);
163
#else
186
#else
164
    UNUSED (path);
187
    UNUSED(path);
165
#endif /* defined (FS_MKDIR) */
188
#endif /* defined (FS_MKDIR) */
166
}
189
}