/* * Copyright (c) 1995, 1996 Gunther Schadow. All rights reserved. * * 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; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that 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., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "pg_config.h" #include "misc.h" #include #include #include /* * Change string to lower case */ char *strlow(char *s) { if(s != NULL) { char *p; for(p = s; *p != '\0'; p++) *p = tolower(*p); } return s; } /* * Change string to upper case */ char *strup(char *s) { if(s != NULL) { char *p; for(p = s; *p != '\0'; p++) *p = toupper(*p); } return s; } /* * Locate the first occurence of any char from the null terminated string * `chrs' in a memory block. */ void* memchrs(const void *blk, const char *chrs, size_t len) { char *b; const char *c; for ( b=(char*)blk; len > 0; b++, len-- ) { for ( c=chrs; *c; c++ ) if ( *b==*c ) return (void*) b; } return NULL; } /* * This string deconstructors works essentially like BSD's strsep(3). * but takes just a single delimiter character. */ char * strsepc(char **str, char delim) { char *p; char *old=*str; if (old) { for ( p=old; *p; p++ ) if ( *p==delim ) { *p=0; *str= ++p; /* delim found --> *str = what follows delim */ return old; /* delim found --> return old *str */ } } else return NULL; /* *str==NULL --> return NULL */ *str=NULL; /* no delim found --> *str = NULL */ return old; /* return old *str */ } /* * Append a char (e.g. delimiter) to a string. */ char *strcatc(char* str, char append) { char *p=str; for(;*p;p++); *p++=append; *p=0; return str; } size_t txlen(const char *x) { int i = strlen(x); while(i>0) { i--; if (!isspace(x[i])) return i+1; } return 0; } char *txdup(const char *x) { size_t len = txlen(x); char *t = (char *)malloc(len+1); strncpy(t,x,len); t[len]=0; return t; } /* * This algorithm is simple, short and fast, but it moves the pointer * instead of the text --> the beginning of the memory block gets lost, * free() will not find the block in it's memory allocation table. char *clws(char *x) { while (*x && isspace(*x)) x++; return x; } */ /* Move the text instead: */ char *clws(char *x) { char *p = x; char *q = x; while(isspace(*p) && *p!=0) p++; while((*q++=*p++)!=0); /* Yea, this is C! */ return x; } char *trws(char *x) { int i=strlen(x); while(i>0) { i--; if(!isspace(x[i])) { x[++i]=0; return x; } } return x; } /*********************************************************************** * * These are versions of memchrs() and strsep() which implement a * policy for handling escape sequences which are superior but * incompatible with the HL7 rules. */ /* * Locate the first occurence of any unescaped delim-char in a memory * block. */ void* memchrse(const void *blk, const char *chrs, size_t len, char esc) { char *b; const char *c; int is_esc=0; for( b=(char*)blk; len > 0; b++, len--) { if (!is_esc) for( c=chrs; *c; c++) if(*b==*c) return (void*)b; is_esc = (*b==esc) && !is_esc; } return NULL; } /* * This string deconstructors works essentially like BSD's strsep(3). * Any delimiter character can be protected in the string by one * preceding esc character. The esc character can protect itself. If no * esc parameter is given or if esc is 0, then we don't care about escape * sequences (e.g. esc makes no sense in scanning a number). */ char * strsepe(char **str, const char *delim, char esc) { char *p; char *old=*str; int esc_flag=0; int delim_flag=0; if (old) for(p=old;*p;p++) if ((delim_flag=(!esc_flag && strchr(delim, *p)))) { *p=0; *str= ++p; /* delim found --> *str = what follows delim */ return old; /* delim found --> return old *str */ } else { esc_flag=(esc && (*p==esc) && !esc_flag); continue; } else return NULL; /* *str==NULL --> return NULL */ *str=NULL; /* no delim found --> *str = NULL */ return old; /* return old *str */ } /* * The same function which takes but one delimiter character. */ char * strsepce(char **str, char delim, char esc) { char *p; char *old=*str; int esc_flag=0; int delim_flag=0; if (old) for(p=old;*p;p++) if ((delim_flag=(!esc_flag && *p==delim))) { *p=0; *str= ++p; /* delim found --> *str = what follows delim */ return old; /* delim found --> return old *str */ } else { esc_flag=(esc && (*p==esc) && !esc_flag); continue; } else return NULL; /* *str==NULL --> return NULL */ *str=NULL; /* no delim found --> *str = NULL */ return old; /* return old *str */ }