Imported Robodoc.
[robodoc.git] / Source / path.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 <string.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include "path.h"
27 #include "robodoc.h"
28 #include "headers.h"
29 #include "util.h"
30
31 #ifdef DMALLOC
32 #include <dmalloc.h>
33 #endif
34
35 /* TODO Documentation */
36
37 struct RB_Path     *
38 RB_Get_RB_Path( char *arg_pathname )
39 {
40     struct RB_Path     *rb_path;
41     int                 needs_slash = FALSE;
42
43     if ( strlen( arg_pathname ) == 0 )
44     {
45         RB_Panic( "Trying to use a path with as name \"\"\n" );
46         return 0;               /* Keep the compiler happy. */
47     }
48     else
49     {
50         /* Check if the path ends with a / if not we will need to add one. */
51         needs_slash = ( arg_pathname[strlen( arg_pathname ) - 1] != '/' );
52         rb_path = ( struct RB_Path * ) malloc( sizeof( struct RB_Path ) );
53
54         if ( ! rb_path )
55         {
56             RB_Panic( "Out of memory! %s()\n", "RB_Get_RB_Path" );
57         }
58
59         /* 2 extra for the '/' and '\0'  */
60         rb_path->name =
61             ( char * ) calloc( strlen( arg_pathname ) + 2, sizeof( char ) );
62
63         if ( ! rb_path->name )
64         {
65             RB_Panic( "Out of memory! %s()\n", "RB_Get_RB_Path" );
66         }
67
68         *( rb_path->name ) = '\0';
69         rb_path->parent = NULL;
70         rb_path->next = NULL;
71         strcat( rb_path->name, arg_pathname );
72         if ( needs_slash )
73         {
74             strcat( rb_path->name, "/" );
75         }
76         rb_path->docname = NULL;
77     }
78     return rb_path;
79 }
80
81 /*x**f* ROBODoc/RB_Get_RB_Path2
82  * NAME
83  *   RB_Get_RB_Path2 -- create a new RB_Path structure.
84  * FUNCTION
85  * NOTE
86  *   Has a wrong name...
87  *****
88  */
89
90 /* TODO Documentation */
91 struct RB_Path     *
92 RB_Get_RB_Path2( char *arg_current_path, char *arg_subdirectory )
93 {
94     struct RB_Path     *rb_path;
95     rb_path = ( struct RB_Path * ) malloc( sizeof( struct RB_Path ) );
96     /* allocate memory for the path name,
97        it will consist of the current_pathname plus the
98        subdirectory plus a '\0' */
99     rb_path->name =
100         ( char * ) malloc( strlen( arg_current_path ) +
101                            strlen( arg_subdirectory ) + 2 );
102
103     if ( ! rb_path->name )
104     {
105         RB_Panic( "Out of memory! %s()\n", "RB_Get_RB_Path2" );
106     }
107
108     strcpy( rb_path->name, arg_current_path );
109     strcat( rb_path->name, arg_subdirectory );
110     if ( arg_subdirectory[strlen( arg_subdirectory ) - 1] != '/' )
111     {
112         strcat( rb_path->name, "/" );
113     }
114     rb_path->docname = NULL;
115     rb_path->parent = NULL;
116     rb_path->next = 0;
117     return rb_path;
118 }
119
120 /*x**f* ROBODoc/RB_Free_RB_Path
121  * NAME
122  *   RB_Free_RB_Path -- free a RB_Path structure.
123  *****
124  * TODO Documentation
125  */
126
127 void
128 RB_Free_RB_Path( struct RB_Path *arg_rb_path )
129 {
130     free( arg_rb_path->name );
131     if ( arg_rb_path->docname )
132     {
133         free( arg_rb_path->docname );
134     }
135     free( arg_rb_path );
136 }
137