* configure.in,
[fw/sdcc] / src / SDCCutil.h
1 /*-------------------------------------------------------------------------
2   SDCCutil.c - Small utility functions.
3
4              Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1999)
5
6    This program is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by the
8    Free Software Foundation; either version 2, or (at your option) any
9    later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20    In other words, you are welcome to use, share and improve this program.
21    You are forbidden to forbid anyone else to use, share and improve
22    what you give them.   Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
24
25 #ifndef SDCCUTIL_H
26 #define SDCCUTIL_H
27
28 #include "SDCChasht.h"
29 #include "dbuf.h"
30 #include <stdarg.h>
31
32 /** Given an array of name, value string pairs creates a new hash
33     containing all of the pairs.
34 */
35 hTab *populateStringHash (const char **pin);
36
37 /** Prints elements of the set to the file, each element on new line
38 */
39 void fputStrSet (FILE *fp, set *list);
40
41 /** Prepend / append given strings to each item of string set. The result is in a
42     new string set.
43 */
44 set *appendStrSet (set *list, const char *pre, const char *post);
45
46 /** Given a set returns a string containing all of the strings seperated
47     by spaces. The returned string is on the heap.
48 */
49 const char *joinStrSet (set *list);
50
51 /** Split the path string to the directory and file name (including extension) components.
52     The directory component doesn't contain trailing directory separator.
53     Returns true if the path contains the directory separator. */
54 int dbuf_splitPath (const char *path, struct dbuf_s *dir, struct dbuf_s *file);
55
56 /** Split the path string to the file name (including directory) and file extension components.
57     The file name component doesn't contain trailing extension separator.
58     Returns true if the path contains the extension separator. */
59 int dbuf_splitFile (const char *path, struct dbuf_s *file, struct dbuf_s *ext);
60
61 /** Combile directory and the file name to a path string using the DIR_SEPARATOR_CHAR.
62  */
63 void dbuf_makePath (struct dbuf_s *path, const char *dir, const char *file);
64
65 /** Given a file with path information in the binary files directory,
66     returns the directory component. Used for discovery of bin
67     directory of SDCC installation. Returns NULL if the path is
68     impossible.
69 */
70 const char *getBinPath (const char *prel);
71
72 /** Returns true if the given path exists.
73  */
74 bool pathExists (const char *ppath);
75
76 void setMainValue (const char *pname, const char *pvalue);
77
78 void populateMainValues (const char **ppin);
79
80 void buildCmdLine2 (char *pbuffer, size_t len, const char *pcmd, ...);
81
82 /** Returns true if sz starts with the string given in key.
83  */
84 bool startsWith (const char *sz, const char *key);
85
86 /** Removes any newline characters from the string.  Not strictly the
87     same as perl's chomp.
88 */
89 void chomp (char *sz);
90
91 hTab *getRuntimeVariables (void);
92
93 /* strncpy() with guaranteed NULL termination. */
94 char *strncpyz (char *dest, const char *src, size_t n);
95
96 /* like strncat() with guaranteed NULL termination
97  * The passed size should be the size of the dest buffer, not the number of 
98  * bytes to copy.
99  */
100 char *strncatz (char *dest, const char *src, size_t n);
101
102 /* return SDCC build number */
103 const char *getBuildNumber (void);
104
105 /* return environment used to build SDCC */
106 const char *getBuildEnvironment(void);
107
108 /* snprintf, by hook or by crook. */
109 size_t SDCCsnprintf (char *, size_t, const char *, ...);
110
111 # if defined(HAVE_VSNPRINTF)
112
113 // best option: we can define our own snprintf which logs errors.
114 #  define SNPRINTF SDCCsnprintf
115
116 # elif defined(HAVE_SPRINTF)
117
118 // if we can't build a safe snprintf for lack of vsnprintf but there
119 // is a native snprintf, use it.
120 #  define SNPRINTF snprintf
121
122 # elif defined(HAVE_VSPRINTF)
123
124 // we can at least define our own unsafe version.
125 #  define SNPRINTF SDCCsnprintf
126
127 # else
128 // We don't have a native snprintf nor the functions we need to write one.
129 #  error "Need at least one of snprintf, vsnprintf, vsprintf!"
130 # endif
131
132 /** Pragma tokenizer
133  */
134 enum pragma_token_e { TOKEN_UNKNOWN, TOKEN_STR, TOKEN_INT, TOKEN_EOL };
135
136 struct pragma_token_s {
137   enum pragma_token_e type;
138   struct dbuf_s dbuf;
139   union {
140     int int_val;
141   } val;
142 };
143
144 void init_pragma_token (struct pragma_token_s *token);
145 char *get_pragma_token (const char *s, struct pragma_token_s *token);
146 const char *get_pragma_string (struct pragma_token_s *token);
147 void free_pragma_token (struct pragma_token_s *token);
148
149 unsigned char hexEscape (const char **src);
150 unsigned char octalEscape (const char **src);
151 int copyStr (char *dest, const char *src);
152
153 #endif