2004-12-22 Daniel Berlin <dberlin@dberlin.org>
[official-gcc.git] / libgfortran / runtime / string.c
blobeade7c17a8a1add9d38540c1fbdcdfa05a1deed8
1 /* Copyright (C) 2002-2003 Free Software Foundation, Inc.
2 Contributed by Paul Brook
4 This file is part of the GNU Fortran 95 runtime library (libgfor).
6 Libgfor is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 Libgfor 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.
16 You should have received a copy of the GNU General Public License
17 along with libgfor; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "config.h"
22 #include <string.h>
24 #include "libgfortran.h"
27 /* Compare a C-style string with a fortran style string in a case-insensitive
28 manner. Used for decoding string options to various statements. Returns
29 zero if not equal, nonzero if equal. */
31 static int
32 compare0 (const char *s1, int s1_len, const char *s2)
34 int i;
36 if (strncasecmp (s1, s2, s1_len) != 0)
37 return 0;
39 /* The rest of s1 needs to be blanks for equality. */
41 for (i = strlen (s2); i < s1_len; i++)
42 if (s1[i] != ' ')
43 return 0;
45 return 1;
49 /* Given a fortran string, return its length exclusive of the trailing
50 spaces. */
51 int
52 fstrlen (const char *string, int len)
54 for (len--; len >= 0; len--)
55 if (string[len] != ' ')
56 break;
58 return len + 1;
62 void
63 fstrcpy (char *dest, int destlen, const char *src, int srclen)
65 if (srclen >= destlen)
67 /* This will truncate if too long. */
68 memcpy (dest, src, destlen);
70 else
72 memcpy (dest, src, srclen);
73 /* Pad with spaces. */
74 memset (&dest[srclen], ' ', destlen - srclen);
79 void
80 cf_strcpy (char *dest, int dest_len, const char *src)
82 int src_len;
84 src_len = strlen (src);
86 if (src_len >= dest_len)
88 /* This will truncate if too long. */
89 memcpy (dest, src, dest_len);
91 else
93 memcpy (dest, src, src_len);
94 /* Pad with spaces. */
95 memset (&dest[src_len], ' ', dest_len - src_len);
100 /* Given a fortran string and an array of st_option structures, search through
101 the array to find a match. If the option is not found, we generate an error
102 if no default is provided. */
105 find_option (const char *s1, int s1_len, st_option * opts,
106 const char *error_message)
108 for (; opts->name; opts++)
109 if (compare0 (s1, s1_len, opts->name))
110 return opts->value;
112 generate_error (ERROR_BAD_OPTION, error_message);
114 return -1;