OSDN Git Service

2010-06-18 Jerry DeLisle <jvdelisle@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libgfortran / runtime / string.c
1 /* Copyright (C) 2002, 2003, 2005, 2007, 2009 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 3, or (at your option)
9 any later version.
10
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.
15
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.
19
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/>.  */
24
25 #include "libgfortran.h"
26 #include <string.h>
27
28 /* Compare a C-style string with a fortran style string in a case-insensitive
29    manner.  Used for decoding string options to various statements.  Returns
30    zero if not equal, nonzero if equal.  */
31
32 static int
33 compare0 (const char *s1, gfc_charlen_type s1_len, const char *s2)
34 {
35   gfc_charlen_type len;
36
37   /* Strip trailing blanks from the Fortran string.  */
38   len = fstrlen (s1, s1_len);
39
40   if ((size_t) len != strlen(s2))
41     return 0; /* don't match */
42
43   return strncasecmp (s1, s2, len) == 0;
44 }
45
46
47 /* Given a fortran string, return its length exclusive of the trailing
48    spaces.  */
49
50 gfc_charlen_type
51 fstrlen (const char *string, gfc_charlen_type len)
52 {
53   for (; len > 0; len--)
54     if (string[len-1] != ' ')
55       break;
56
57   return len;
58 }
59
60
61 /* Copy a Fortran string (not null-terminated, hence length arguments
62    for both source and destination strings. Returns the non-padded
63    length of the destination.  */
64
65 gfc_charlen_type
66 fstrcpy (char *dest, gfc_charlen_type destlen, 
67          const char *src, gfc_charlen_type srclen)
68 {
69   if (srclen >= destlen)
70     {
71       /* This will truncate if too long.  */
72       memcpy (dest, src, destlen);
73       return destlen;
74     }
75   else
76     {
77       memcpy (dest, src, srclen);
78       /* Pad with spaces.  */
79       memset (&dest[srclen], ' ', destlen - srclen);
80       return srclen;
81     }
82 }
83
84
85 /* Copy a null-terminated C string to a non-null-terminated Fortran
86    string. Returns the non-padded length of the destination string.  */
87
88 gfc_charlen_type
89 cf_strcpy (char *dest, gfc_charlen_type dest_len, const char *src)
90 {
91   size_t src_len;
92
93   src_len = strlen (src);
94
95   if (src_len >= (size_t) dest_len)
96     {
97       /* This will truncate if too long.  */
98       memcpy (dest, src, dest_len);
99       return dest_len;
100     }
101   else
102     {
103       memcpy (dest, src, src_len);
104       /* Pad with spaces.  */
105       memset (&dest[src_len], ' ', dest_len - src_len);
106       return src_len;
107     }
108 }
109
110
111 /* Given a fortran string and an array of st_option structures, search through
112    the array to find a match.  If the option is not found, we generate an error
113    if no default is provided.  */
114
115 int
116 find_option (st_parameter_common *cmp, const char *s1, gfc_charlen_type s1_len,
117              const st_option * opts, const char *error_message)
118 {
119   for (; opts->name; opts++)
120     if (compare0 (s1, s1_len, opts->name))
121       return opts->value;
122
123   generate_error (cmp, LIBERROR_BAD_OPTION, error_message);
124
125   return -1;
126 }