1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
|
/*
* $Id: epr_string.c,v 1.1.1.1 2004-10-28 19:22:23 norman Exp $
*
* Copyright (C) 2002 by Brockmann Consult (info@brockmann-consult.de)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation. This program is distributed in the hope it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "epr_api.h"
#include "epr_core.h"
#include "epr_string.h"
char* epr_assign_string(char** str_clone, const char* str)
{
assert(str_clone != NULL);
epr_free_string(*str_clone);
*str_clone = epr_clone_string(str);
return *str_clone;
}
char* epr_create_string(unsigned int length)
{
return (char*) calloc(length + 1, sizeof (char));
}
char* epr_clone_string(const char* str)
{
char* str_clone = NULL;
if (str != NULL) {
str_clone = epr_create_string(strlen(str));
strcpy(str_clone, str);
}
return str_clone;
}
void epr_cut_string(char** sub_str, const char* str, int start, int length)
{
*sub_str = epr_create_string(length);
strncpy(*sub_str, str + start, length);
*sub_str[length] = '\0';
}
char* epr_sub_string(const char* str, int start, int length)
{
char* str_sub_string = NULL;
if (str != NULL) {
str_sub_string = epr_create_string(length);
strncpy(str_sub_string, str + start, length);
str_sub_string[length] = '\0';
}
return str_sub_string;
}
/*
Function: epr_get_last_err_code
Access: private API implementation helper
Changelog: 2002/01/05 nf initial version
*/
/**
* Compares the two given names and returns <code>TRUE</code> if
* they are equal ignoring the case of each letter.
*
* <p> This function is used to compare names throughout the
* ENVISAT product reader API.
*
* @param name1 the first name, must not be NULL
* @param name2 the second name, must not be NULL
* @return <code>TRUE</code> if the names are equal,
* <code>FALSE</code> otherwise
*/
epr_boolean epr_equal_names(const char* name1, const char* name2)
{
assert(name1 != NULL);
assert(name2 != NULL);
return stricmp(name1, name2) == 0;
}
void epr_free_string(char* str)
{
if (str == NULL)
return;
free(str);
}
/*
Function: epr_str_tok
Access: private API implementation helper
Changelog: 2002/01/10 mp initial version
*/
/**
* Finds substrings between separators.
*
* @param str the string to search
* @param seps the separator symbols string
* @param pos position with a search begin
*
* @return the next substring (or own string) of the found name or
* <code>(uint)NULL</code> if an error occurred.
*/
char* epr_str_tok(const char* str, const char* seps, int* pos)
{
char* token = NULL;
int i, old_pos;
int token_len = 0;
assert(str != NULL);
if (*pos >= (int)strlen(str)) return NULL;
old_pos = *pos;
for (i = *pos; str[i] != '\0'; i++) {
if (strchr(seps, str[i]) != NULL)
{
token_len = i - *pos;
token = epr_create_string(token_len);
strncpy(token, str + *pos, token_len);
token[token_len] = '\0';
*pos = i + 1;
return token;
}
}
if (strlen(str) > 0) {
if (old_pos == 0) {
*pos = i + 1;
token = epr_clone_string(str);
return token;
} else if (old_pos > 0) {
token_len = i - old_pos;
token = epr_create_string(token_len);
strncpy(token, str + *pos, token_len);
token[token_len] = '\0';
*pos = (int)strlen(str);
return token;
}
}
return NULL;
}
/*
Function: epr_str_tok_tok
Access: private API implementation helper
Changelog: 2002/01/10 mp initial version
*/
/**
* Findes substrings between double separators.
*
* @param str the string to search
* @param seps the separator symbols string
* @param pos position with a search begin
*
* @return the next substring (or own string) of the found name or
* <code>(uint)NULL</code> if an error occurred.
*/
char* epr_str_tok_tok(const char* str, const char* seps, const char* exceptions, uint* pos)
{
char* token = NULL;
uint i, old_pos;
uint token_len = 0;
assert(str != NULL);
if (*pos >= (uint)strlen(str)) return NULL;
old_pos = *pos;
for (i = *pos; str[i] != '\0'; i++) {
if (((strchr(seps, str[i]) != NULL)
&& (i == 0))
|| ((strchr(seps, str[i]) != NULL)
&& (i > 0)
&& (strchr(exceptions, str[i - 1]) == NULL))) {
token_len = i - *pos;
token = epr_create_string(token_len);
strncpy(token, str + *pos, token_len);
token[token_len] = '\0';
*pos = i + 1;
return token;
}
}
if (strlen(str) > 0) {
if (old_pos == 0) {
*pos = i + 1;
token = epr_clone_string(str);
return token;
} else if (old_pos > 0) {
token_len = i - old_pos;
token = epr_create_string(token_len);
strncpy(token, str + *pos, token_len);
token[token_len] = '\0';
*pos = (int)strlen(str);
return token;
}
}
return NULL;
}
int epr_find_first_not_white(const char* str)
{
int i;
char white[] = {" "};
for (i = 0; i < (int)strlen(str); i ++) {
if (strchr(white, str[i]) == NULL) return i;
}
return (int)strlen(str);
}
int epr_find_last_not_white(const char* str)
{
int i;
char white[] = {" "};
if ((int)strlen(str) == 0)
return -1;
for (i = (int)strlen(str) - 1; i >= 0; i --) {
if (strchr(white, str[i]) == NULL) return i;
}
return -1;
}
char* epr_trim_string(char* str)
{
int i, i1, i2, n;
assert(str != NULL);
n = strlen(str);
if (n == 0)
return str;
i1 = -1;
for (i = 0; str[i] != '\0'; i++) {
if (!isspace(str[i])) {
i1 = i;
break;
}
}
if (i1 == -1) {
str[0] = '\0';
return str;
}
i2 = -1;
for (i = n - 1; i >= 0; i--) {
if (!isspace(str[i])) {
i2 = i;
break;
}
}
assert(i1 > -1 && i2 > -1);
if (i1 > 0) {
int j = 0;
for (i = i1; i <= i2; i++) {
str[j] = str[i];
j++;
}
}
str[i2 - i1 + 1] = '\0';
return str;
}
char* epr_strip_string_r(char* str)
{
int i, i1, n;
assert(str != NULL);
n = strlen(str);
if (n == 0) {
return str;
}
i1 = -1;
for (i = n - 1; i >= 0; i--) {
if (33 <= str[i] && str[i] <= 126) {
i1 = i;
break;
}
}
if (i1 == -1) {
str[0] = '\0';
} else {
str[i1+1] = '\0';
}
return str;
}
int epr_if_no_letters(const char* str)
{
int i;
char ciffer[] = {"0123456789+- "};
for (i = 0; i < (int)strlen(str); i ++) {
if (strchr(ciffer, str[i]) == NULL) return 0;
}
return 1;
}
int epr_get_positive_int(const char* str)
{
int i;
char ciffer[] = {"0123456789 "};
for (i = 0; i < (int)strlen(str); i ++) {
if (strchr(ciffer, str[i]) == NULL) return -1;
}
return atoi(str);
}
/*'Verdacht': if float-or-double*/
int epr_numeral_suspicion(const char* str)
{
int i;
char ciffer[] = {"0123456789+- .eE"};
for (i = 0; i < (int)strlen(str); i ++) {
if (strchr(ciffer, str[i]) == NULL) return 0;
}
/*if YES*/
return 1;
}
int epr_if_no_scaling(const char* str)
{
int result;
result = strrchr(str, EPR_IRRELEVANCE_SYMBOL) - str + 1;
if (result == 1) return 1;
return 0;
}
void epr_free_and_null_string(char** str)
{
if (*str == NULL) return;
epr_free_string(*str);
*str = NULL;
}
int stricmp(const char* s1, const char* s2)
{
int d;
assert(s1 != NULL);
assert(s2 != NULL);
while (*s1 != '\0' && *s2 != '\0') {
d = tolower(*s1) - tolower(*s2);
if (d != 0) {
break;
}
s1++;
s2++;
}
d = tolower(*s1) - tolower(*s2);
return d;
}
|