| 1 |
/*=========================================================================== |
| 2 |
Copyright (C) 1995-2009 European Southern Observatory (ESO) |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or |
| 5 |
modify it under the terms of the GNU General Public License as |
| 6 |
published by the Free Software Foundation; either version 2 of |
| 7 |
the License, or (at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public |
| 15 |
License along with this program; if not, write to the Free |
| 16 |
Software Foundation, Inc., 675 Massachusetts Ave, Cambridge, |
| 17 |
MA 02139, USA. |
| 18 |
|
| 19 |
Correspondence concerning ESO-MIDAS should be addressed as follows: |
| 20 |
Internet e-mail: [email protected] |
| 21 |
Postal address: European Southern Observatory |
| 22 |
Data Management Division |
| 23 |
Karl-Schwarzschild-Strasse 2 |
| 24 |
D 85748 Garching bei Muenchen |
| 25 |
GERMANY |
| 26 |
===========================================================================*/ |
| 27 |
|
| 28 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 29 |
.TYPE Module |
| 30 |
.NAME str |
| 31 |
.LANGUAGE C |
| 32 |
.AUTHOR IPG-ESO Garching |
| 33 |
.CATEGORY Independant string handling interfaces. |
| 34 |
.COMMENTS |
| 35 |
Handling of character strings. These routines normally return |
| 36 |
a non-negative integer number on successful return. |
| 37 |
Otherwise, a value of -1 is set to indicate an error |
| 38 |
condition and the variable ``oserror'' contains the |
| 39 |
symbolic error code. |
| 40 |
|
| 41 |
.HISTORY [0.0] 25-Aug-1986 Definition J. D. Ponz |
| 42 |
[1.0] 10-Apr-1987 Programmation B. Pirenne |
| 43 |
[1.1] 04-Aug-1987 VMS Includes |
| 44 |
[1.2] 910911 bzero removed CG.(memset can be used instead) |
| 45 |
.VERSION |
| 46 |
|
| 47 |
090324 last modif |
| 48 |
------------------------------------------------------------*/ |
| 49 |
|
| 50 |
#include <fcntl.h> |
| 51 |
#include <stdio.h> |
| 52 |
#include <errno.h> |
| 53 |
#include <proto_os.h> /* ANSI-C prototyping */ |
| 54 |
|
| 55 |
#include <osparms.h> |
| 56 |
|
| 57 |
#ifdef __STDC__ |
| 58 |
int strsglen(char * str , char cha) |
| 59 |
#else |
| 60 |
int strsglen(str,cha) |
| 61 |
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 62 |
.PURPOSE Compute the length of the string up to '\0' or the character "cha" |
| 63 |
(typically a ' '). |
| 64 |
.RETURNS Upon successful completion a positive or zero integer number is |
| 65 |
returned. |
| 66 |
.REMARKS System dependencies: |
| 67 |
-- UNIX: none |
| 68 |
------------------------------------------------------------*/ |
| 69 |
char *str; /* IN : string to check */ |
| 70 |
char cha; /* IN : open mode */ |
| 71 |
#endif |
| 72 |
|
| 73 |
{ |
| 74 |
register int i; |
| 75 |
|
| 76 |
for (i=0;*(str+i) != '\0' && *(str+i) != cha;i++); |
| 77 |
|
| 78 |
*(str+i) = '\0'; |
| 79 |
return(i); |
| 80 |
} |
| 81 |
|
| 82 |
int strtolower(s1,s2) |
| 83 |
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 84 |
.PURPOSE |
| 85 |
translate the s2 character string to lower case and store the |
| 86 |
result in s1. |
| 87 |
.RETURNS The number of characters actually translated. |
| 88 |
|
| 89 |
---------------------------------------------------------------*/ |
| 90 |
|
| 91 |
char *s1; /* OUT: ouptput string */ |
| 92 |
char *s2; /* IN : input string */ |
| 93 |
|
| 94 |
{ |
| 95 |
register int i,c; |
| 96 |
|
| 97 |
c = 0; |
| 98 |
for (i=0;*(s2+i) != '\0';i++) { |
| 99 |
if (*(s2+i) >= 'A' && *(s2+i) <= 'Z') |
| 100 |
{ |
| 101 |
*(s1+i) = *(s2+i) + 32; |
| 102 |
c++; |
| 103 |
} |
| 104 |
else |
| 105 |
*(s1+i) = *(s2+i); |
| 106 |
} |
| 107 |
return(c); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
int strgetline(buf) |
| 112 |
/*+++++++++++++++++++++++++++++ getline +++++++++++++++++++++++++ |
| 113 |
.PURPOSE get one record from standard input. |
| 114 |
.RETURNS number of characters fetched. |
| 115 |
-----------------------------------------------------------------*/ |
| 116 |
char *buf; /* buffer to contain the string read */ |
| 117 |
{ |
| 118 |
int i; |
| 119 |
char c; |
| 120 |
|
| 121 |
for(i=0;(c=getchar()) != '\n';i++) *(buf+i) = c; |
| 122 |
*(buf+i+1) = '\0'; |
| 123 |
return(i); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
int truelen(string) |
| 128 |
/*++++++++++++++++++++++ truelen ++++++++++++++++++++++++++++++++ |
| 129 |
.PURPOSE Compute the 'true length' of a string of character by checking for |
| 130 |
all valid characters. Valid characters taken into account are : |
| 131 |
a..z, A..Z, ., *, -, _, 0..9. |
| 132 |
.RETURNS The number of contiguous characters in the string satisfying the |
| 133 |
above condition. |
| 134 |
------------------------------------------------------------------*/ |
| 135 |
char *string; /* The character string to check */ |
| 136 |
{ |
| 137 |
register int i,c; |
| 138 |
|
| 139 |
for (i = 0; (c = *(string + i)) == '.' || c == '-' || c == '_' || |
| 140 |
(c >= 'a' && c <= 'z') || |
| 141 |
(c >= 'A' && c <= 'Z') || |
| 142 |
(c >= '0' && c <= '9') ; |
| 143 |
i++) |
| 144 |
; |
| 145 |
*(string+i) = '\0'; |
| 146 |
return(i); |
| 147 |
} |
| 148 |
|
| 149 |
int strin(s1,s2) |
| 150 |
/*+++++++++++++++++++++++++++++ strin ++++++++++++++++++++++++++++++++++++++ |
| 151 |
.PURPOSE Pattern matching |
| 152 |
.RETURNS 0 if pattern is not in string, 1 if found. |
| 153 |
-------------------------------------------------------------------------*/ |
| 154 |
|
| 155 |
char *s1 /* IN : string to search */; |
| 156 |
char *s2 /* IN : pattern to find */; |
| 157 |
|
| 158 |
{ |
| 159 |
register char *t1,*t2; |
| 160 |
int found; |
| 161 |
|
| 162 |
found = 0; |
| 163 |
t2 = s2; |
| 164 |
t1 = s1; |
| 165 |
for (;*t1 != '\0' && *t2 != '\0' && !found;t1++) { |
| 166 |
if (*t1 == *t2) |
| 167 |
t2++; |
| 168 |
else { |
| 169 |
if (t2 != s2) |
| 170 |
{ |
| 171 |
t1 -= t2 - s2; |
| 172 |
t2 = s2; |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
if (*t2 == '\0') found = 1; |
| 177 |
return(found); |
| 178 |
} |
| 179 |
|
| 180 |
char * strstrs(s1,s2) |
| 181 |
/*+++++++++++++++++++++++++++++ strstrs +++++++++++++++++++++++++++++++++++ |
| 182 |
.PURPOSE Pattern matching |
| 183 |
.RETURNS returns a pointer to the first occurrence of the pattern string s2 |
| 184 |
in s1. Otherwise returns NULL |
| 185 |
-------------------------------------------------------------------------*/ |
| 186 |
char *s1 /* IN : string to search */; |
| 187 |
char *s2 /* IN : pattern to find */; |
| 188 |
|
| 189 |
{ |
| 190 |
register char *t1,*t2; |
| 191 |
char *ptr; |
| 192 |
|
| 193 |
ptr = (char *)0; |
| 194 |
t2 = s2; |
| 195 |
t1 = s1; |
| 196 |
for (;*t1 != '\0' && *t2 != '\0' ;t1++) { |
| 197 |
if (*t1 == *t2) t2++; |
| 198 |
else { |
| 199 |
if (t2 != s2) { |
| 200 |
t1 -= t2 - s2; |
| 201 |
t2 = s2; |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
if (*t2 == '\0') ptr = t1 - strlen(s2); |
| 206 |
return(ptr); |
| 207 |
} |