4 * Copyright 2000 Jon Griffiths
9 /*******************************************************************
12 * Create a single string from many substrings
14 char *str_create(size_t num_str, ...)
17 size_t len = 1, i = 0;
20 va_start (args, num_str);
21 for (i = 0; i < num_str; i++)
22 if ((t = va_arg(args, char *)))
26 if (!(tmp = (char *) malloc (len)))
27 fatal ("Out of memory");
31 va_start (args, num_str);
32 for (i = 0; i < num_str; i++)
33 if ((t = va_arg(args, char *)))
40 /*******************************************************************
43 * Create a single string from many substrings, terminating in a number
45 char *str_create_num(size_t num_str, int num, ...)
48 size_t len = 8, i = 0;
52 for (i = 0; i < num_str; i++)
53 if ((t = va_arg(args, char *)))
57 if (!(tmp = (char *) malloc (len)))
58 fatal ("Out of memory");
63 for (i = 0; i < num_str; i++)
64 if ((t = va_arg(args, char *)))
67 sprintf (tmp + len - 8, "%d", num);
72 /*******************************************************************
75 * Create a new substring from a string
77 char *str_substring(const char *start, const char *end)
81 assert (start && end && end > start);
83 if (!(newstr = (char *) malloc (end - start + 1)))
84 fatal ("Out of memory");
86 memcpy (newstr, start, end - start);
87 newstr [end - start] = '\0';
93 /*******************************************************************
96 * Swap two strings in another string, in place
97 * Modified PD code from 'snippets'
99 char *str_replace (char *str, const char *oldstr, const char *newstr)
104 if (!(p = strstr(str, oldstr)))
106 oldlen = strlen (oldstr);
107 newlen = strlen (newstr);
108 memmove (q = p + newlen, p + oldlen, strlen (p + oldlen) + 1);
109 memcpy (p, newstr, newlen);
114 /*******************************************************************
117 * Locate one string in another, ignoring spaces
119 const char *str_match (const char *str, const char *match, int *found)
121 assert(str && match && found);
123 while (*str == ' ') str++;
124 if (!strncmp (str, match, strlen (match)))
127 str += strlen (match);
128 while (*str == ' ') str++;
136 /*******************************************************************
139 * Locate the first occurence of a set of characters in a string
141 const char *str_find_set (const char *str, const char *findset)
143 assert(str && findset);
147 const char *p = findset;
157 /*******************************************************************
162 char *str_toupper (char *str)
167 *str = toupper (*str);
174 /*******************************************************************
177 * Open a file returning only on success
179 FILE *open_file (const char *name, const char *ext, const char *mode)
184 if (((unsigned)snprintf (fname, sizeof (fname), "%s%s%s",
185 *mode == 'w' ? "./" : "", name, ext) > sizeof (fname)))
186 fatal ("File name too long");
189 printf ("Open file %s\n", fname);
191 fp = fopen (fname, mode);
193 fatal ("Cant open file");
198 /*******************************************************************
201 * Fatal error handling
203 void fatal (const char *message)