Imported Robodoc.
[robodoc.git] / Source / file.c
1 /*
2 Copyright (C) 1994-2007  Frans Slothouber, Jacco van Weert, Petteri Kettunen,
3 Bernd Koesling, Thomas Aglassinger, Anthon Pang, Stefan Kost, David Druffner,
4 Sasha Vasko, Kai Hofmann, Thierry Pierron, Friedrich Haase, and Gergely Budai.
5
6 This file is part of ROBODoc
7
8 ROBODoc is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 */
22
23 #include "file.h"
24 #include <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
27
28
29 #ifdef DMALLOC
30 #include <dmalloc.h>
31 #endif
32 #include "util.h"
33
34 /****h* ROBODoc/Filename
35  * NAME
36  *   Functions to deal with keeping track
37  *   of filenames and directory names.
38  *****
39  */
40
41 /****f* Filename/RB_Get_RB_Filename
42  * NAME
43  *   RB_Get_RB_Filename
44  * SYNOPSIS
45  */
46 struct RB_Filename* RB_Get_RB_Filename( char *arg_filename, struct RB_Path *arg_rb_path )
47 /*
48  * INPUTS
49  *   o arg_rb_filename --
50  *   o arg_rb_path --
51  * FUNCTION
52  *   Create a new RB_Filename structure based on arg_filename and
53  *   arg_rb_path.
54  * SOURCE
55  */
56 {
57     struct RB_Filename *rb_filename =
58         ( struct RB_Filename * ) malloc( sizeof( struct RB_Filename ) );
59     rb_filename->name = ( char * ) malloc( strlen( arg_filename ) + 1 );
60     rb_filename->docname = 0;
61     rb_filename->fullname = 0;
62     rb_filename->fulldocname = 0;
63     strcpy( rb_filename->name, arg_filename );
64     rb_filename->path = arg_rb_path;
65     return rb_filename;
66 }
67
68 /*****/
69
70 struct RB_Filename *RB_Copy_RB_Filename( struct RB_Filename* arg_rb_filename )
71 {
72     return RB_Get_RB_Filename( arg_rb_filename->name, arg_rb_filename->path );
73 }
74
75
76 void
77 RB_Filename_Dump( struct RB_Filename *arg_rb_filename )
78 {
79     printf( "[%s %s %s]  ", RB_Get_Path( arg_rb_filename ),
80             RB_Get_Filename( arg_rb_filename ),
81             RB_Get_Extension( arg_rb_filename ) );
82     printf( "%s\n", Get_Fullname( arg_rb_filename ) );
83 }
84
85 /*x**f* Filename/RB_Free_RB_Filename 
86  * NAME
87  *   RB_Free_RB_Filename -- free a RB_Filename structure.
88  *
89  *****
90  * TODO Documentation
91  */
92
93 void
94 RB_Free_RB_Filename( struct RB_Filename *arg_rb_filename )
95 {
96     free( arg_rb_filename->name );
97     if ( arg_rb_filename->docname )
98     {
99         free( arg_rb_filename->docname );
100     }
101     if ( arg_rb_filename->fullname )
102     {
103         free( arg_rb_filename->fullname );
104     }
105     if ( arg_rb_filename->fulldocname )
106     {
107         free( arg_rb_filename->fulldocname );
108     }
109     free( arg_rb_filename );
110 }
111
112 /* Set the fulldoc name, this is used in singledoc mode
113  * since there the docname is preset by the user and not
114  * derived from the sourcefile name.
115  */
116
117 void RB_Set_FullDocname( struct RB_Filename *arg_rb_filename, char* name )
118 {
119     arg_rb_filename->fulldocname = RB_StrDup( name );
120 }
121
122 /* TODO Documentation RB_Get_FullDocname */
123 char               *
124 RB_Get_FullDocname( struct RB_Filename *arg_rb_filename )
125 {
126     char               *result = arg_rb_filename->fulldocname;
127
128     if ( result == NULL )
129     {
130         unsigned int  size = strlen( arg_rb_filename->docname ) +
131             strlen( arg_rb_filename->path->docname ) + 1;
132         result = ( char * ) malloc( size * sizeof( char ) );
133         assert( result );
134         *result = '\0';
135         strcat( result, arg_rb_filename->path->docname );
136         strcat( result, arg_rb_filename->docname );
137         /* Save the result so it can be reused later on, and we can properly deallocate it. */
138         arg_rb_filename->fulldocname = result;
139     }
140     return result;
141 }
142
143
144 /****f* Filename/Get_Fullname
145  * NAME
146  *   Get_Fullname --
147  * SYNOPSIS
148  */
149 char* Get_Fullname( struct RB_Filename *arg_rb_filename )
150 /*
151  * FUNCTION
152  *   Give the full name of the file, that is the name of
153  *   the file including the extension and the path.
154  *   The path can be relative or absolute.
155  * NOTE
156  *   The string returned is owned by this function
157  *   so don't change it.
158  * SOURCE
159  */
160 {
161     char               *result = arg_rb_filename->fullname;
162
163     if ( result == NULL )
164     {
165         unsigned int        size = strlen( arg_rb_filename->name ) +
166             strlen( arg_rb_filename->path->name ) + 1;
167         result = ( char * ) malloc( size * sizeof( char ) );
168         assert( result );
169         *result = '\0';
170         strcat( result, arg_rb_filename->path->name );
171         strcat( result, arg_rb_filename->name );
172         /* Save the result so it can be reused later on, and we can properly deallocate it. */
173         arg_rb_filename->fullname = result;
174     }
175     return result;
176 }
177 /******/
178
179 /****f* Filename/RB_Get_Path
180  * SYNOPSIS
181  */
182 char* RB_Get_Path( struct RB_Filename *arg_rb_filename )
183 /*
184  * FUNCTION
185  *   Give the path for this file.
186  * NOTE
187  *   The string returned is owned by this function
188  *   so don't change it.
189  ******
190  */
191 {
192     return arg_rb_filename->path->name;
193 }
194
195
196 /****f* Filename/RB_Get_Extension
197  * NAME
198  *   RB_Get_Extension --
199  * FUNCTION
200  *   Give the extension of this file. That is the part after
201  *   the last '.' if there is any.
202  * SYNOPSIS
203  */
204 char* RB_Get_Extension( struct RB_Filename *arg_rb_filename )
205 /*
206  * RESULT
207  *   pointer to the extension
208  *   pointer to a '\0' if no extension was found.
209  * NOTE
210  *   The string returned is owned by this function
211  *   so don't change it.
212  * SOURCE
213  */
214 {
215     char               *c = arg_rb_filename->name;
216     int                 i = strlen( c );
217
218     for ( c += i; c != arg_rb_filename->name && ( *c != '.' ); --c )
219     {
220         /* Empty */
221     }
222     if ( *c == '.' )
223     {
224         ++c;
225     }
226     else
227     {
228         c = arg_rb_filename->name;
229         c += i;
230     }
231     return c;
232 }
233 /*****/
234
235 /****f* Filename/RB_Get_Filename
236  * NAME
237  *   RB_Get_Filename --
238  * FUNCTION
239  *   Give the name of this file. That is the name
240  *   of the file without its path but with the
241  *   extension.
242  * SYNOPSIS
243  */
244 char* RB_Get_Filename( struct RB_Filename *arg_rb_filename )
245 /*
246  * RESULT
247  *   pointer to the extension
248  *   pointer to a '\0' if no extension was found.
249  * NOTE
250  *   The string returned is owned by this function
251  *   so don't change it.
252  ******
253  */
254 {
255     return arg_rb_filename->name;
256 }