1 /*
2  * This file is part of XForms.
3  *
4  *  XForms is free software; you can redistribute it and/or modify it
5  *  under the terms of the GNU Lesser General Public License as
6  *  published by the Free Software Foundation; either version 2.1, or
7  *  (at your option) any later version.
8  *
9  *  XForms is distributed in the hope that it will be useful, but
10  *  WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *  Lesser General Public License for more details.
13  *
14  *  You should have received a copy of the GNU Lesser General Public License
15  *  along with XForms.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 
19 /**
20  * \file fd_util.c
21  *
22  * Eliminate the emission of duplicate info. This is necessary as
23  * some #include define data (pixmap for example).
24  *
25  * We should eventually move the functionality of already_emited in
26  * fd_printC.c into this function so callback is also checked. This
27  * probably means we need make struct CodeInfo more efficient.
28  */
29 
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33 
34 #include <ctype.h>
35 
36 #include "fd_main.h"
37 
38 static char ** dup_info_cache = NULL;
39 static size_t n_dup_info = 0;
40 
41 
42 /***************************************
43  ***************************************/
44 
45 unsigned int
check_resize(unsigned int what,int nw,int se)46 check_resize( unsigned int what,
47               int          nw,
48               int          se )
49 {
50     if (    what & FL_RESIZE_X
51          && (    nw == FL_NorthWest
52               || nw == FL_West
53               || nw == FL_SouthWest )
54          && (    se == FL_NorthWest
55               || se == FL_West
56               || se == FL_SouthWest ) )
57         what &= ~ FL_RESIZE_X;
58     else if (    ! ( what & FL_RESIZE_X )
59               && (    nw == FL_NorthWest
60                    || nw == FL_West
61                    || nw == FL_SouthWest )
62               && (    se == FL_NorthEast
63                    || se == FL_East
64                    || se == FL_SouthEast ) )
65         what |= ~ FL_RESIZE_X;
66 
67     if (    what & FL_RESIZE_Y
68          && (    nw == FL_NorthWest
69               || nw == FL_North
70               || nw == FL_NorthEast )
71          && (    se == FL_NorthWest
72               || se == FL_North
73               || se == FL_NorthEast ) )
74         what &= ~ FL_RESIZE_Y;
75     else if (    ! ( what & FL_RESIZE_Y )
76               && (    nw == FL_NorthWest
77                    || nw == FL_North
78                    || nw == FL_NorthEast )
79               && (    se == FL_SouthWest
80                    || se == FL_South
81                    || se == FL_SouthEast ) )
82         what |= ~ FL_RESIZE_Y;
83 
84     return what;
85 }
86 
87 
88 /***************************************
89  ***************************************/
90 
91 void
reset_dupinfo_cache(void)92 reset_dupinfo_cache( void )
93 {
94     size_t i;
95 
96     for ( i = 0; i < n_dup_info; i++ )
97         fl_free( dup_info_cache[ i ] );
98 
99     fli_safe_free( dup_info_cache );
100     n_dup_info = 0;
101 }
102 
103 
104 /***************************************
105  ***************************************/
106 
107 int
is_duplicate_info(const char * s)108 is_duplicate_info( const char * s )
109 {
110     size_t i;
111 
112     for ( i = 0; i < n_dup_info; i++ )
113         if ( ! strcmp( s, dup_info_cache[ i ] ) )
114             return 1;
115 
116     if ( ! (    ( dup_info_cache =
117                          fl_realloc( dup_info_cache,
118                                      ++n_dup_info * sizeof dup_info_cache ) )
119              && ( dup_info_cache[ n_dup_info - 1 ] = fl_strdup( s ) ) ) )
120     {
121         fprintf( stderr, "Running oit of memory\n" );
122         exit( 1 );
123     }
124 
125     return 0;
126 }
127 
128 
129 /***************************************
130  * Returns a newly allocated string with the aboslute path
131  * for the input path
132  ***************************************/
133 
134 char *
rel2abs(const char * rel_path)135 rel2abs( const char * rel_path )
136 {
137     char * abs_path = NULL;
138     char *res;
139 
140     if ( *rel_path == '/' )
141         abs_path = fl_strdup( rel_path );
142     else
143     {
144         long path_max = pathconf( ".", _PC_PATH_MAX );
145         size_t size;
146 
147         if ( path_max == -1 )
148             size = 1024;
149         else
150             size = path_max;
151 
152         while ( 1 )
153         {
154             abs_path = fl_realloc( abs_path, size + strlen( rel_path ) + 2 );
155             if ( ! getcwd( abs_path, size ) )
156                 size += 1024;
157             else
158                 break;
159         }
160 
161         strcat( strcat( abs_path, "/" ), rel_path );
162     }
163 
164     while ( ( res = strstr( abs_path, "/./" ) ) )
165         memmove( res, res + 2, strlen( res ) - 1 );
166 
167     while ( ( res = strstr( abs_path, "/../" ) ) )
168     {
169 		if ( res != abs_path )
170 		{
171 			char * dest = res - 1;
172 
173 			while ( *dest != '/' )
174 				dest--;
175 
176 			memmove( dest, res + 3, strlen( res ) - 2 );
177 		}
178 		else
179 			memmove( abs_path, abs_path + 3, strlen( abs_path ) - 2 );
180     }
181 
182     return fl_realloc( abs_path, strlen( abs_path ) + 1 );
183 }
184 
185 
186 /***************************************
187  * Returns if a string can be used as a valid C identifier
188  ***************************************/
189 
190 int
is_valid_c_name(const char * str)191 is_valid_c_name( const char * str )
192 {
193     const char * sp;
194 
195     if ( fdopt.lax )
196         return 1;
197 
198     if (    ! isascii( ( unsigned char ) *str )
199          || ! ( isalpha( ( unsigned char ) *str ) || *str == '_' ) )
200         return 0;
201 
202     for ( sp = str + 1; *sp; sp++ )
203         if (    ! isascii( ( unsigned char ) *sp )
204              || ! ( isalnum( ( unsigned char ) *sp ) || *sp == '_' ) )
205             return 0;
206 
207     return 1;
208 }
209 
210 
211 /*
212  * Local variables:
213  * tab-width: 4
214  * indent-tabs-mode: nil
215  * End:
216  */
217