]>
Commit | Line | Data |
---|---|---|
f0bcf628 | 1 | /* Copyright (C) 2002-2014 Free Software Foundation, Inc. |
6de9cd9a DN |
2 | Contributed by Paul Brook |
3 | ||
57dea9f6 | 4 | This file is part of the GNU Fortran 95 runtime library (libgfortran). |
6de9cd9a | 5 | |
57dea9f6 | 6 | Libgfortran is free software; you can redistribute it and/or modify |
6de9cd9a | 7 | it under the terms of the GNU General Public License as published by |
748086b7 | 8 | the Free Software Foundation; either version 3, or (at your option) |
6de9cd9a DN |
9 | any later version. |
10 | ||
57dea9f6 | 11 | Libgfortran is distributed in the hope that it will be useful, |
6de9cd9a DN |
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 | ||
748086b7 JJ |
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/>. */ | |
6de9cd9a | 24 | |
6de9cd9a | 25 | #include "libgfortran.h" |
36ae8a61 | 26 | #include <string.h> |
6de9cd9a | 27 | |
6de9cd9a DN |
28 | |
29 | /* Given a fortran string, return its length exclusive of the trailing | |
30 | spaces. */ | |
88fdfd5a JB |
31 | |
32 | gfc_charlen_type | |
33 | fstrlen (const char *string, gfc_charlen_type len) | |
6de9cd9a | 34 | { |
88fdfd5a JB |
35 | for (; len > 0; len--) |
36 | if (string[len-1] != ' ') | |
6de9cd9a DN |
37 | break; |
38 | ||
88fdfd5a | 39 | return len; |
6de9cd9a DN |
40 | } |
41 | ||
42 | ||
88fdfd5a JB |
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. */ | |
46 | ||
47 | gfc_charlen_type | |
48 | fstrcpy (char *dest, gfc_charlen_type destlen, | |
49 | const char *src, gfc_charlen_type srclen) | |
6de9cd9a | 50 | { |
6de9cd9a DN |
51 | if (srclen >= destlen) |
52 | { | |
53 | /* This will truncate if too long. */ | |
54 | memcpy (dest, src, destlen); | |
88fdfd5a | 55 | return destlen; |
6de9cd9a DN |
56 | } |
57 | else | |
58 | { | |
59 | memcpy (dest, src, srclen); | |
60 | /* Pad with spaces. */ | |
61 | memset (&dest[srclen], ' ', destlen - srclen); | |
88fdfd5a | 62 | return srclen; |
6de9cd9a DN |
63 | } |
64 | } | |
65 | ||
66 | ||
88fdfd5a JB |
67 | /* Copy a null-terminated C string to a non-null-terminated Fortran |
68 | string. Returns the non-padded length of the destination string. */ | |
69 | ||
70 | gfc_charlen_type | |
71 | cf_strcpy (char *dest, gfc_charlen_type dest_len, const char *src) | |
6de9cd9a | 72 | { |
88fdfd5a | 73 | size_t src_len; |
6de9cd9a DN |
74 | |
75 | src_len = strlen (src); | |
76 | ||
88fdfd5a | 77 | if (src_len >= (size_t) dest_len) |
6de9cd9a DN |
78 | { |
79 | /* This will truncate if too long. */ | |
80 | memcpy (dest, src, dest_len); | |
88fdfd5a | 81 | return dest_len; |
6de9cd9a DN |
82 | } |
83 | else | |
84 | { | |
85 | memcpy (dest, src, src_len); | |
86 | /* Pad with spaces. */ | |
87 | memset (&dest[src_len], ' ', dest_len - src_len); | |
88fdfd5a | 88 | return src_len; |
6de9cd9a DN |
89 | } |
90 | } | |
91 | ||
92 | ||
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. */ | |
96 | ||
97 | int | |
88fdfd5a | 98 | find_option (st_parameter_common *cmp, const char *s1, gfc_charlen_type s1_len, |
5e805e44 | 99 | const st_option * opts, const char *error_message) |
6de9cd9a | 100 | { |
a5ad78bb FXC |
101 | /* Strip trailing blanks from the Fortran string. */ |
102 | size_t len = (size_t) fstrlen (s1, s1_len); | |
103 | ||
6de9cd9a | 104 | for (; opts->name; opts++) |
a5ad78bb | 105 | if (len == strlen(opts->name) && strncasecmp (s1, opts->name, len) == 0) |
6de9cd9a DN |
106 | return opts->value; |
107 | ||
d74b97cc | 108 | generate_error (cmp, LIBERROR_BAD_OPTION, error_message); |
6de9cd9a DN |
109 | |
110 | return -1; | |
111 | } |