OSDN Git Service

* Update ChangeLog with the missing entry from my previous commit.
[pf3gnuchains/gcc-fork.git] / libgfortran / runtime / string.c
1 /* Copyright (C) 2002, 2003, 2005 Free Software Foundation, Inc.
2    Contributed by Paul Brook
3
4 This file is part of the GNU Fortran 95 runtime library (libgfortran).
5
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.
10
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.)
19
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.
24
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, 51 Franklin Street, Fifth Floor,
28 Boston, MA 02110-1301, USA.  */
29
30 #include "config.h"
31 #include <string.h>
32
33 #include "libgfortran.h"
34
35
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.  */
39
40 static int
41 compare0 (const char *s1, int s1_len, const char *s2)
42 {
43   int len;
44
45   /* Strip trailing blanks from the Fortran string.  */
46   len = fstrlen (s1, s1_len);
47   return strncasecmp (s1, s2, len) == 0;
48 }
49
50
51 /* Given a fortran string, return its length exclusive of the trailing
52    spaces.  */
53 int
54 fstrlen (const char *string, int len)
55 {
56   for (len--; len >= 0; len--)
57     if (string[len] != ' ')
58       break;
59
60   return len + 1;
61 }
62
63
64 void
65 fstrcpy (char *dest, int destlen, const char *src, int srclen)
66 {
67   if (srclen >= destlen)
68     {
69       /* This will truncate if too long.  */
70       memcpy (dest, src, destlen);
71     }
72   else
73     {
74       memcpy (dest, src, srclen);
75       /* Pad with spaces.  */
76       memset (&dest[srclen], ' ', destlen - srclen);
77     }
78 }
79
80
81 void
82 cf_strcpy (char *dest, int dest_len, const char *src)
83 {
84   int src_len;
85
86   src_len = strlen (src);
87
88   if (src_len >= dest_len)
89     {
90       /* This will truncate if too long.  */
91       memcpy (dest, src, dest_len);
92     }
93   else
94     {
95       memcpy (dest, src, src_len);
96       /* Pad with spaces.  */
97       memset (&dest[src_len], ' ', dest_len - src_len);
98     }
99 }
100
101
102 /* Given a fortran string and an array of st_option structures, search through
103    the array to find a match.  If the option is not found, we generate an error
104    if no default is provided.  */
105
106 int
107 find_option (const char *s1, int s1_len, const st_option * opts,
108              const char *error_message)
109 {
110   for (; opts->name; opts++)
111     if (compare0 (s1, s1_len, opts->name))
112       return opts->value;
113
114   generate_error (ERROR_BAD_OPTION, error_message);
115
116   return -1;
117 }