5a635391833f7dbcf1b522d0efbbe7f1a1a75221
[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 <stdarg.h>
30
31 /* PENDING: Hacks as I can't work autoconf */
32 #define BINDIR  PREFIX "/bin"
33
34 /** Given an array of name, value string pairs creates a new hash
35     containing all of the pairs.
36 */
37 hTab *populateStringHash(const char **pin);
38
39 /** Given an array of string pointers and another string, adds the
40     string to the end of the list.  The end of the list is assumed to
41     be the first NULL pointer.
42 */
43 void addToList (const char **list, const char *str);
44
45 /** Given an array of string pointers returns a string containing all
46     of the strings seperated by spaces.  The returned string is on the
47     heap.  The join stops when a NULL pointer is hit.
48 */
49 char *join(const char **pplist);
50
51 /** Given an array of string pointers, returns a string containing all
52     of the strings seperated by spaces.  The returned string is on the
53     heap.  n is the number of strings in the list.
54 */
55 char *joinn(char **pplist, int n);
56
57 /** Returns the characters in the path p2 past the last matching characters in
58     p1.  Processes in a host dependent way.  For example, on win32 the
59     test is case insensitive and treats '/' and '\' as the same.
60 */
61 char *getPathDifference (char *pinto, const char *p1, const char *p2);
62
63 /** Given a file with path information in the binary files directory,
64     returns what PREFIX must be to get this path.  Used for discovery
65     of where SDCC is installed.  Returns NULL if the path is
66     impossible.
67 */
68 char *getPrefixFromBinPath (const char *prel);
69
70 /** Returns true if the given path exists.
71  */
72 bool pathExists (const char *ppath);
73
74 void setMainValue (const char *pname, const char *pvalue);
75
76 void populateMainValues (const char **ppin);
77
78 void buildCmdLine2 (char *pbuffer, const char *pcmd, size_t len);
79
80 /** Returns true if sz starts with the string given in key.
81  */
82 bool startsWith (const char *sz, const char *key);
83
84 /** Removes any newline characters from the string.  Not strictly the
85     same as perl's chomp.
86 */
87 void chomp (char *sz);
88
89 hTab *
90 getRuntimeVariables(void);
91
92 /* strncpy() with guaranteed NULL termination. */
93 char *strncpyz(char *dest, const char *src, size_t n);
94
95 /* like strncat() with guaranteed NULL termination
96  * The passed size should be the size of the dest buffer, not the number of 
97  * bytes to copy.
98  */
99 char *strncatz(char *dest, const char *src, size_t n);
100
101 /* snprintf, by hook or by crook. */
102 size_t SDCCsnprintf(char *, size_t, const char *, ...);
103
104 # if defined(HAVE_VSNPRINTF)
105
106 // best option: we can define our own snprintf which logs errors.
107 #  define SNPRINTF SDCCsnprintf
108
109 # elif defined(HAVE_SPRINTF)
110
111 // if we can't build a safe snprintf for lack of vsnprintf but there
112 // is a native snprintf, use it.
113 #  define SNPRINTF snprintf
114
115 # elif defined(HAVE_VSPRINTF)
116
117 // we can at least define our own unsafe version.
118 #  define SNPRINTF SDCCsnprintf
119
120 # else
121 // We don't have a native snprintf nor the functions we need to write one.
122 #  error "Need at least one of snprintf, vsnprintf, vsprintf!"
123 # endif
124
125
126
127 #endif
128