OSDN Git Service

* combine.c (combine_max_regno): Remove. Remove all uses.
[pf3gnuchains/gcc-fork.git] / libgfortran / runtime / string.c
1 /* Copyright (C) 2002, 2003, 2005, 2007 Free Software Foundation, Inc.
2    Contributed by Paul Brook
3
4 This file is part of the GNU Fortran 95 runtime library (libgfortran).
5
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 2, or (at your option)
9 any later version.
10
11 In addition to the permissions in the GNU General Public License, the
12 Free Software Foundation gives you unlimited permission to link the
13 compiled version of this file into combinations with other programs,
14 and to distribute those combinations without any restriction coming
15 from the use of this file.  (The General Public License restrictions
16 do apply in other respects; for example, they cover modification of
17 the file, and distribution when not linked into a combine
18 executable.)
19
20 Libgfortran is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with libgfortran; see the file COPYING.  If not, write to
27 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
28 Boston, MA 02110-1301, USA.  */
29
30 #include "config.h"
31 #include <string.h>
32
33 #include "libgfortran.h"
34
35 /* Compare a C-style string with a fortran style string in a case-insensitive
36    manner.  Used for decoding string options to various statements.  Returns
37    zero if not equal, nonzero if equal.  */
38
39 static int
40 compare0 (const char *s1, gfc_charlen_type s1_len, const char *s2)
41 {
42   size_t len;
43
44   /* Strip trailing blanks from the Fortran string.  */
45   len = fstrlen (s1, s1_len);
46   if (len != strlen(s2)) return 0; /* don't match */
47   return strncasecmp (s1, s2, len) == 0;
48 }
49
50
51 /* Given a fortran string, return its length exclusive of the trailing
52    spaces.  */
53
54 gfc_charlen_type
55 fstrlen (const char *string, gfc_charlen_type len)
56 {
57   for (; len > 0; len--)
58     if (string[len-1] != ' ')
59       break;
60
61   return len;
62 }
63
64
65 /* Copy a Fortran string (not null-terminated, hence length arguments
66    for both source and destination strings. Returns the non-padded
67    length of the destination.  */
68
69 gfc_charlen_type
70 fstrcpy (char *dest, gfc_charlen_type destlen, 
71          const char *src, gfc_charlen_type srclen)
72 {
73   if (srclen >= destlen)
74     {
75       /* This will truncate if too long.  */
76       memcpy (dest, src, destlen);
77       return destlen;
78     }
79   else
80     {
81       memcpy (dest, src, srclen);
82       /* Pad with spaces.  */
83       memset (&dest[srclen], ' ', destlen - srclen);
84       return srclen;
85     }
86 }
87
88
89 /* Copy a null-terminated C string to a non-null-terminated Fortran
90    string. Returns the non-padded length of the destination string.  */
91
92 gfc_charlen_type
93 cf_strcpy (char *dest, gfc_charlen_type dest_len, const char *src)
94 {
95   size_t src_len;
96
97   src_len = strlen (src);
98
99   if (src_len >= (size_t) dest_len)
100     {
101       /* This will truncate if too long.  */
102       memcpy (dest, src, dest_len);
103       return dest_len;
104     }
105   else
106     {
107       memcpy (dest, src, src_len);
108       /* Pad with spaces.  */
109       memset (&dest[src_len], ' ', dest_len - src_len);
110       return src_len;
111     }
112 }
113
114
115 /* Given a fortran string and an array of st_option structures, search through
116    the array to find a match.  If the option is not found, we generate an error
117    if no default is provided.  */
118
119 int
120 find_option (st_parameter_common *cmp, const char *s1, gfc_charlen_type s1_len,
121              const st_option * opts, const char *error_message)
122 {
123   for (; opts->name; opts++)
124     if (compare0 (s1, s1_len, opts->name))
125       return opts->value;
126
127   generate_error (cmp, ERROR_BAD_OPTION, error_message);
128
129   return -1;
130 }