Licensing changes to GPLv3 resp. GPLv3 with GCC Runtime Exception.
[platform/upstream/gcc.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   if (len != strlen(s2)) return 0; /* don't match */
40   return strncasecmp (s1, s2, len) == 0;
41 }
42
43
44 /* Given a fortran string, return its length exclusive of the trailing
45    spaces.  */
46
47 gfc_charlen_type
48 fstrlen (const char *string, gfc_charlen_type len)
49 {
50   for (; len > 0; len--)
51     if (string[len-1] != ' ')
52       break;
53
54   return len;
55 }
56
57
58 /* Copy a Fortran string (not null-terminated, hence length arguments
59    for both source and destination strings. Returns the non-padded
60    length of the destination.  */
61
62 gfc_charlen_type
63 fstrcpy (char *dest, gfc_charlen_type destlen, 
64          const char *src, gfc_charlen_type srclen)
65 {
66   if (srclen >= destlen)
67     {
68       /* This will truncate if too long.  */
69       memcpy (dest, src, destlen);
70       return destlen;
71     }
72   else
73     {
74       memcpy (dest, src, srclen);
75       /* Pad with spaces.  */
76       memset (&dest[srclen], ' ', destlen - srclen);
77       return srclen;
78     }
79 }
80
81
82 /* Copy a null-terminated C string to a non-null-terminated Fortran
83    string. Returns the non-padded length of the destination string.  */
84
85 gfc_charlen_type
86 cf_strcpy (char *dest, gfc_charlen_type dest_len, const char *src)
87 {
88   size_t src_len;
89
90   src_len = strlen (src);
91
92   if (src_len >= (size_t) dest_len)
93     {
94       /* This will truncate if too long.  */
95       memcpy (dest, src, dest_len);
96       return dest_len;
97     }
98   else
99     {
100       memcpy (dest, src, src_len);
101       /* Pad with spaces.  */
102       memset (&dest[src_len], ' ', dest_len - src_len);
103       return src_len;
104     }
105 }
106
107
108 /* Given a fortran string and an array of st_option structures, search through
109    the array to find a match.  If the option is not found, we generate an error
110    if no default is provided.  */
111
112 int
113 find_option (st_parameter_common *cmp, const char *s1, gfc_charlen_type s1_len,
114              const st_option * opts, const char *error_message)
115 {
116   for (; opts->name; opts++)
117     if (compare0 (s1, s1_len, opts->name))
118       return opts->value;
119
120   generate_error (cmp, LIBERROR_BAD_OPTION, error_message);
121
122   return -1;
123 }