* runtime/string.c (compare0): Remove.
[official-gcc.git] / libgfortran / runtime / string.c
bloba37272011be81b2c3e1e230b6cb5e69785dc0fa7
1 /* Copyright (C) 2002, 2003, 2005, 2007, 2009 Free Software Foundation, Inc.
2 Contributed by Paul Brook
4 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6 Libgfortran 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 3, or (at your option)
9 any later version.
11 Libgfortran 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 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
25 #include "libgfortran.h"
26 #include <string.h>
29 /* Given a fortran string, return its length exclusive of the trailing
30 spaces. */
32 gfc_charlen_type
33 fstrlen (const char *string, gfc_charlen_type len)
35 for (; len > 0; len--)
36 if (string[len-1] != ' ')
37 break;
39 return len;
43 /* Copy a Fortran string (not null-terminated, hence length arguments
44 for both source and destination strings. Returns the non-padded
45 length of the destination. */
47 gfc_charlen_type
48 fstrcpy (char *dest, gfc_charlen_type destlen,
49 const char *src, gfc_charlen_type srclen)
51 if (srclen >= destlen)
53 /* This will truncate if too long. */
54 memcpy (dest, src, destlen);
55 return destlen;
57 else
59 memcpy (dest, src, srclen);
60 /* Pad with spaces. */
61 memset (&dest[srclen], ' ', destlen - srclen);
62 return srclen;
67 /* Copy a null-terminated C string to a non-null-terminated Fortran
68 string. Returns the non-padded length of the destination string. */
70 gfc_charlen_type
71 cf_strcpy (char *dest, gfc_charlen_type dest_len, const char *src)
73 size_t src_len;
75 src_len = strlen (src);
77 if (src_len >= (size_t) dest_len)
79 /* This will truncate if too long. */
80 memcpy (dest, src, dest_len);
81 return dest_len;
83 else
85 memcpy (dest, src, src_len);
86 /* Pad with spaces. */
87 memset (&dest[src_len], ' ', dest_len - src_len);
88 return src_len;
93 /* Given a fortran string and an array of st_option structures, search through
94 the array to find a match. If the option is not found, we generate an error
95 if no default is provided. */
97 int
98 find_option (st_parameter_common *cmp, const char *s1, gfc_charlen_type s1_len,
99 const st_option * opts, const char *error_message)
101 /* Strip trailing blanks from the Fortran string. */
102 size_t len = (size_t) fstrlen (s1, s1_len);
104 for (; opts->name; opts++)
105 if (len == strlen(opts->name) && strncasecmp (s1, opts->name, len) == 0)
106 return opts->value;
108 generate_error (cmp, LIBERROR_BAD_OPTION, error_message);
110 return -1;