Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libgfortran / runtime / string.c
blob07ed99bab366327d980c37e8d7b7de79b2211d3f
1 /* Copyright (C) 2002-2003 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 2, or (at your option)
9 any later version.
11 In addition to the permissions in the GNU General Public License, the
12 Free Software Foundation gives you unlimited permission to link the
13 compiled version of this file into combinations with other programs,
14 and to distribute those combinations without any restriction coming
15 from the use of this file. (The General Public License restrictions
16 do apply in other respects; for example, they cover modification of
17 the file, and distribution when not linked into a combine
18 executable.)
20 Libgfortran is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with libgfortran; see the file COPYING. If not, write to
27 the Free Software Foundation, 59 Temple Place - Suite 330,
28 Boston, MA 02111-1307, USA. */
30 #include "config.h"
31 #include <string.h>
33 #include "libgfortran.h"
36 /* Compare a C-style string with a fortran style string in a case-insensitive
37 manner. Used for decoding string options to various statements. Returns
38 zero if not equal, nonzero if equal. */
40 static int
41 compare0 (const char *s1, int s1_len, const char *s2)
43 int i;
45 if (strncasecmp (s1, s2, s1_len) != 0)
46 return 0;
48 /* The rest of s1 needs to be blanks for equality. */
50 for (i = strlen (s2); i < s1_len; i++)
51 if (s1[i] != ' ')
52 return 0;
54 return 1;
58 /* Given a fortran string, return its length exclusive of the trailing
59 spaces. */
60 int
61 fstrlen (const char *string, int len)
63 for (len--; len >= 0; len--)
64 if (string[len] != ' ')
65 break;
67 return len + 1;
71 void
72 fstrcpy (char *dest, int destlen, const char *src, int srclen)
74 if (srclen >= destlen)
76 /* This will truncate if too long. */
77 memcpy (dest, src, destlen);
79 else
81 memcpy (dest, src, srclen);
82 /* Pad with spaces. */
83 memset (&dest[srclen], ' ', destlen - srclen);
88 void
89 cf_strcpy (char *dest, int dest_len, const char *src)
91 int src_len;
93 src_len = strlen (src);
95 if (src_len >= dest_len)
97 /* This will truncate if too long. */
98 memcpy (dest, src, dest_len);
100 else
102 memcpy (dest, src, src_len);
103 /* Pad with spaces. */
104 memset (&dest[src_len], ' ', dest_len - src_len);
109 /* Given a fortran string and an array of st_option structures, search through
110 the array to find a match. If the option is not found, we generate an error
111 if no default is provided. */
114 find_option (const char *s1, int s1_len, st_option * opts,
115 const char *error_message)
117 for (; opts->name; opts++)
118 if (compare0 (s1, s1_len, opts->name))
119 return opts->value;
121 generate_error (ERROR_BAD_OPTION, error_message);
123 return -1;