Update version info for release v4.6.1 (#2122)
[WRF.git] / var / da / makedepf90-2.8.8 / utils.c
blob08f6905bd7870dfa14409f090836ead2ee05d6df
1 /*
2 * Copyright (C) 2000-2005 Erik Edelmann <Erik.Edelmann@iki.fi>
4 * This program is free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License as published by the Free Software Foundation;
7 * either version 2 of the License, or (at your option) any
8 * later version.
10 * This program is distributed in the hope that it will be
11 * useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 * PURPOSE. See the GNU General Public License for more
14 * details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307 USA
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include "errormesg.h"
27 #include "global.h"
28 #include "xmalloc.h"
30 /* Copy src to dest, converting uppercase letters to lowercase. */
32 void strtolower(char *dest, const char *src)
34 int i;
36 for (i = 0; src[i]; i++)
37 dest[i] = tolower(src[i]);
41 /* replace the suffix (everything after the last '.'), including the '.', with
42 * 'new_suffix'. If there is no suffix in 'filename', concatenate 'new_suffix'
43 * to 'filename'. */
45 char *replace_suffix(const char *filename, const char *new_suffix)
47 char *rs;
48 int fl, n, sl;
50 sl = strlen(new_suffix);
52 /* Search for last '.' in filename */
53 fl = n = strlen(filename);
54 while (filename[n] != '.' && n >= 0) n--;
56 if (n == -1) {
57 /* if there was no '.' */
58 rs = (char *)xmalloc ((fl+sl+2)*sizeof(char));
59 strcpy(rs, filename);
60 strcat(rs, new_suffix);
61 } else {
62 rs = (char *)xmalloc ((n+sl+2)*sizeof(char));
63 strncpy(rs, filename, n);
64 rs[n] = '\0';
65 strcat(rs, new_suffix);
68 return rs;
72 /* If filename has no path, append 'path' to the beginning of the filename,
73 * else replace the existing path (everything before the first '/') with 'path'.
76 char *set_path(const char *filename, const char *path)
78 char *rs;
79 int fl, n, pl, nl;
81 pl = strlen(path);
83 fl = n = strlen(filename);
84 while (filename[n] != '/' && n >= 0) n--;
85 nl = fl - n - 1;
87 if (n == -1) {
88 /* if there was no '/' */
89 rs = (char *)xmalloc((fl+pl+2)*sizeof(char));
90 strcpy(rs, path);
91 strcat(rs, filename);
92 } else {
93 rs = (char *)xmalloc((nl+pl+2)*sizeof(char));
94 strcpy(rs, path);
95 strcat(rs, &filename[n+1]);
98 return rs;
102 /* Create and return a copy of s with all citationmarks (" and ') removed. */
104 char *remove_citation(const char *s)
106 char *d;
107 int i, j;
109 d = (char *)xmalloc((strlen(s)+1)*sizeof(char));
110 for (i = j = 0; s[i]; i++)
111 if (s[i] != '"' && s[i] != '\'') d[j++] = s[i];
112 d[j] = '\0';
114 return d;
118 char *expand_rule(const char *r, const char *srcfile)
120 char *rule;
121 int i, j, k, rlen, slen;
123 rule = (char *)xmalloc(RULE_LENGTH*sizeof(char));
124 rlen = strlen(r);
126 /* set slen = length of srcfile without suffix */
127 for (slen = strlen(srcfile); slen > 0 && srcfile[slen] != '.'; slen--);
129 /* If there was no suffix, set slen = strlen(srcfile) */
130 if (slen == 0) slen = strlen(srcfile);
132 rule[0] = '\t';
133 k = 1;
134 for (i = 0; i < rlen; i++) {
135 if (r[i] != '%')
136 rule[k++] = r[i];
137 else {
138 i++;
139 switch (r[i]) {
140 case 'f':
141 for (j = 0; j < slen; j++) rule[k++] = srcfile[j];
142 break;
143 case '%':
144 rule[k++] = '%';
145 break;
146 default:
147 warning("Unknown modifier '%%%c' in rule '%s'", r[i], r);
148 break;
152 rule[k] = '\0';
154 return rule;
158 /* Open 'fname' for reading; first look for 'fname' in the current working
159 * directory, and, if not found, in the list of paths in 'path'. Return NULL if
160 * 'fname' isn't found anywhere. */
162 FILE *open_src_file(const char *fname, const List *path)
164 FILE *fd;
165 char *fn;
166 const List *h;
168 fd = fopen(fname, "r");
169 if (fd == NULL && path) {
170 for (h = path; h && fd == NULL; h = h->next) {
171 fn = xmalloc(strlen(h->data) + strlen(fname) + 2);
172 strcpy(fn, (char *)h->data);
173 strcat(fn, "/");
174 strcat(fn, fname);
175 fd = fopen(fn, "r");
176 free(fn);
180 return fd;