1 /* Copyright (C) 2002, 2003, 2005, 2007, 2009 Free Software Foundation, Inc.
2 Contributed by Paul Brook
4 This file is part of the GNU Fortran 95 runtime library (libgfortran).
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)
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.
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.
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/>. */
25 #include "libgfortran.h"
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. */
33 compare0 (const char *s1
, gfc_charlen_type s1_len
, const char *s2
)
37 /* Strip trailing blanks from the Fortran string. */
38 len
= fstrlen (s1
, s1_len
);
40 if ((size_t) len
!= strlen(s2
))
41 return 0; /* don't match */
43 return strncasecmp (s1
, s2
, len
) == 0;
47 /* Given a fortran string, return its length exclusive of the trailing
51 fstrlen (const char *string
, gfc_charlen_type len
)
53 for (; len
> 0; len
--)
54 if (string
[len
-1] != ' ')
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. */
66 fstrcpy (char *dest
, gfc_charlen_type destlen
,
67 const char *src
, gfc_charlen_type srclen
)
69 if (srclen
>= destlen
)
71 /* This will truncate if too long. */
72 memcpy (dest
, src
, destlen
);
77 memcpy (dest
, src
, srclen
);
78 /* Pad with spaces. */
79 memset (&dest
[srclen
], ' ', destlen
- srclen
);
85 /* Copy a null-terminated C string to a non-null-terminated Fortran
86 string. Returns the non-padded length of the destination string. */
89 cf_strcpy (char *dest
, gfc_charlen_type dest_len
, const char *src
)
93 src_len
= strlen (src
);
95 if (src_len
>= (size_t) dest_len
)
97 /* This will truncate if too long. */
98 memcpy (dest
, src
, dest_len
);
103 memcpy (dest
, src
, src_len
);
104 /* Pad with spaces. */
105 memset (&dest
[src_len
], ' ', dest_len
- src_len
);
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. */
116 find_option (st_parameter_common
*cmp
, const char *s1
, gfc_charlen_type s1_len
,
117 const st_option
* opts
, const char *error_message
)
119 for (; opts
->name
; opts
++)
120 if (compare0 (s1
, s1_len
, opts
->name
))
123 generate_error (cmp
, LIBERROR_BAD_OPTION
, error_message
);