OSDN Git Service

Merge tree-ssa-20020619-branch into mainline.
[pf3gnuchains/gcc-fork.git] / libgfortran / runtime / string.c
1 /* Copyright (C) 2002-2003 Free Software Foundation, Inc.
2    Contributed by Paul Brook
3
4 This file is part of the GNU Fortran 95 runtime library (libgfor).
5
6 Libgfor 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.
10
11 Libgfor 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 You should have received a copy of the GNU General Public License
17 along with libgfor; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21 #include "config.h"
22 #include <string.h>
23
24 #include "libgfortran.h"
25
26
27 /* Compare a C-style string with a fortran style string in a case-insensitive
28    manner.  Used for decoding string options to various statements.  Returns
29    zero if not equal, nonzero if equal.  */
30
31 static int
32 compare0 (const char *s1, int s1_len, const char *s2)
33 {
34   int i;
35
36   if (strncasecmp (s1, s2, s1_len) != 0)
37     return 0;
38
39   /* The rest of s1 needs to be blanks for equality.  */
40
41   for (i = strlen (s2); i < s1_len; i++)
42     if (s1[i] != ' ')
43       return 0;
44
45   return 1;
46 }
47
48
49 /* Given a fortran string, return its length exclusive of the trailing
50    spaces.  */
51 int
52 fstrlen (const char *string, int len)
53 {
54
55   for (len--; len >= 0; len--)
56     if (string[len] != ' ')
57       break;
58
59   return len + 1;
60 }
61
62
63
64 void
65 fstrcpy (char *dest, int destlen, const char *src, int srclen)
66 {
67
68   if (srclen >= destlen)
69     {
70       /* This will truncate if too long.  */
71       memcpy (dest, src, destlen);
72     }
73   else
74     {
75       memcpy (dest, src, srclen);
76       /* Pad with spaces.  */
77       memset (&dest[srclen], ' ', destlen - srclen);
78     }
79 }
80
81
82 void
83 cf_strcpy (char *dest, int dest_len, const char *src)
84 {
85   int src_len;
86
87   src_len = strlen (src);
88
89   if (src_len >= dest_len)
90     {
91       /* This will truncate if too long.  */
92       memcpy (dest, src, dest_len);
93     }
94   else
95     {
96       memcpy (dest, src, src_len);
97       /* Pad with spaces.  */
98       memset (&dest[src_len], ' ', dest_len - src_len);
99     }
100 }
101
102
103 /* Given a fortran string and an array of st_option structures, search through
104    the array to find a match.  If the option is not found, we generate an error
105    if no default is provided.  */
106
107 int
108 find_option (const char *s1, int s1_len, st_option * opts,
109              const char *error_message)
110 {
111
112   for (; opts->name; opts++)
113     if (compare0 (s1, s1_len, opts->name))
114       return opts->value;
115
116   generate_error (ERROR_BAD_OPTION, error_message);
117
118   return -1;
119 }
120