]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libgfortran/lib/contrib/runtime/string.c
update
[l4.git] / l4 / pkg / libgfortran / lib / contrib / runtime / string.c
1 /* Copyright (C) 2002, 2003, 2005, 2007, 2009, 2010
2    Free Software Foundation, Inc.
3    Contributed by Paul Brook
4
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6
7 Libgfortran is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
20
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 <http://www.gnu.org/licenses/>.  */
25
26 #include "libgfortran.h"
27 #include <string.h>
28
29
30 /* Given a fortran string, return its length exclusive of the trailing
31    spaces.  */
32
33 gfc_charlen_type
34 fstrlen (const char *string, gfc_charlen_type len)
35 {
36   for (; len > 0; len--)
37     if (string[len-1] != ' ')
38       break;
39
40   return len;
41 }
42
43
44 /* Copy a Fortran string (not null-terminated, hence length arguments
45    for both source and destination strings. Returns the non-padded
46    length of the destination.  */
47
48 gfc_charlen_type
49 fstrcpy (char *dest, gfc_charlen_type destlen, 
50          const char *src, gfc_charlen_type srclen)
51 {
52   if (srclen >= destlen)
53     {
54       /* This will truncate if too long.  */
55       memcpy (dest, src, destlen);
56       return destlen;
57     }
58   else
59     {
60       memcpy (dest, src, srclen);
61       /* Pad with spaces.  */
62       memset (&dest[srclen], ' ', destlen - srclen);
63       return srclen;
64     }
65 }
66
67
68 /* Copy a null-terminated C string to a non-null-terminated Fortran
69    string. Returns the non-padded length of the destination string.  */
70
71 gfc_charlen_type
72 cf_strcpy (char *dest, gfc_charlen_type dest_len, const char *src)
73 {
74   size_t src_len;
75
76   src_len = strlen (src);
77
78   if (src_len >= (size_t) dest_len)
79     {
80       /* This will truncate if too long.  */
81       memcpy (dest, src, dest_len);
82       return dest_len;
83     }
84   else
85     {
86       memcpy (dest, src, src_len);
87       /* Pad with spaces.  */
88       memset (&dest[src_len], ' ', dest_len - src_len);
89       return src_len;
90     }
91 }
92
93
94 /* Given a fortran string and an array of st_option structures, search through
95    the array to find a match.  If the option is not found, we generate an error
96    if no default is provided.  */
97
98 int
99 find_option (st_parameter_common *cmp, const char *s1, gfc_charlen_type s1_len,
100              const st_option * opts, const char *error_message)
101 {
102   /* Strip trailing blanks from the Fortran string.  */
103   size_t len = (size_t) fstrlen (s1, s1_len);
104
105   for (; opts->name; opts++)
106     if (len == strlen(opts->name) && strncasecmp (s1, opts->name, len) == 0)
107       return opts->value;
108
109   generate_error (cmp, LIBERROR_BAD_OPTION, error_message);
110
111   return -1;
112 }