/*
xmd - molecular dynamics for metals and ceramics
By Jonathan Rifkin <jon.rifkin@uconn.edu>
Copyright 1995-2004 Jonathan Rifkin
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/**********************************/
/** STRING PARSING SUBROUTINES **/
/**********************************/
/*
***********************************************************************
History
***********************************************************************
*/
/*
2 Jan 1997
For unknown reasons, strcmpi() has started to fail on AIX
when strsub.c compiled with O3 option. Have rewritten
routine to include explicit indexing of input strings
(rather than incrementing char *). It works now.
*/
/*
***********************************************************************
Include Files
***********************************************************************
*/
#include <stdlib.h>
#include <ctype.h>
#include "strsub.h"
/*
***********************************************************************
Defines
***********************************************************************
*/
#define BOOLEAN int
#define TRUE 1
#define FALSE 0
/*
***********************************************************************
Module Variables
***********************************************************************
*/
/* String holding White Space Characters */
/*
***********************************************************************
Local Function Prototypes
***********************************************************************
*/
void TruncateWhiteSpace (char *);
BOOLEAN IsWhiteSpace(char);
/*
***********************************************************************
Exported Functions
***********************************************************************
*/
/* PULLS AND RETURNS POINTER TO FIRST TOKEN */
char *strhed (char **tadd)
{
char *head;
char *sep;
char *tail;
tail = *tadd;
/* FIND FIRST NONBLANK CHARACTER */
while ( (*tail)
&& ( (*tail)==' ' || (*tail)=='\t' || (*tail)=='\n' || (*tail)=='\r' ) )
tail++;
/* FIND NEXT WHITE SPACE CHARCTER */
head = (sep = tail);
while ((*sep) && (*sep)!=' ' && (*sep)!='\n' && (*sep)!='\r' && (*sep)!='\t') sep++;
/* Mark end of leading token */
tail = sep;
if (*sep)
{
*sep = 0;
tail++;
}
/* Remove leading and trailing whitespace from tail */
SkipWhiteSpace (&tail);
TruncateWhiteSpace ( tail);
/* Save addres of tail */
*tadd = tail;
/* return address of first token */
return head;
}
/* COMPARES TWO STRINGS BUT IGNORES CASE */
/* RETURNS -1 if a< b */
/* 0 if a==b */
/* 1 if a> b */
int strcmpi (const char *FirstString, const char *SecondString)
{
int ichar;
char FirstChar;
char SecondChar;
ichar = 0;
do
{
/* Convert characters to uppercase */
FirstChar = toupper(FirstString [ichar]);
SecondChar = toupper(SecondString[ichar]);
/* Increment ichar */
ichar++;
}
while (FirstChar==SecondChar && FirstChar!='\0' && SecondChar!='\0');
if (FirstChar<SecondChar)
return -1;
else if (FirstChar>SecondChar)
return 1;
else
return 0;
}
int strncmpi
(
const char *FirstString,
const char *SecondString,
const int nchar
)
{
int ichar;
char FirstChar;
char SecondChar;
/* Special case, no characters checked */
if (nchar==0)
return 0;
ichar = 0;
do
{
/* Convert characters to uppercase */
FirstChar = toupper(FirstString [ichar]);
SecondChar = toupper(SecondString[ichar]);
/* Increment ichar */
ichar++;
}
while
(
FirstChar==SecondChar &&
FirstChar!='\0' &&
SecondChar!='\0' &&
ichar<nchar
);
if (FirstChar<SecondChar)
return -1;
else if (FirstChar>SecondChar)
return 1;
else
return 0;
}
#ifdef OLD_STRCMPI
int strcmpi (char *a, char *b) {
int flag;
char inc = 'a' - 'A';
char ta, tb;
/*test*/
printf ("File %s line %i\n", __FILE__, __LINE__);
printf ("a <%s> b <%s>\n", a, b);
/*end test*/
do {
ta = *a;
tb = *b;
flag = (ta==tb);
if (!flag) {
if (ta>='A' && ta<='Z')
ta += inc;
if (tb>='A' && tb<='Z')
tb += inc; ;
flag = (ta==tb);
}
} while (flag && *a++!=0 && *b++!=0);
/*test*/
printf ("flag = %i\n", flag);
/*end test*/
if (flag)
return(0);
if (ta<tb)
return(-1);
return( 1);
}
#endif
/* PULLS LEADING TOKEN: EVALUATES AS CONSTANT - RETURNS DOUBLE */
double dblstr (char **s) {
char *t = strhed (s);
if (*t==0)
return (0.0);
else
/* Return string converted to float value */
return (atof(t));
}
/* PULLS LEADING TOKEN: EVALUATES AS CONSTANT - RETURNS INT */
int intstr (char **s) {
char *t = strhed (s);
if (*t==0)
return (0);
else
return (atoi(t));
}
/* PULLS LEADING TOKEN: EVALUATES AS CONSTANT - RETURNS LONG */
long lngstr (char **s) {
char *t = strhed (s);
if (*t==0)
return (0L);
else
return (atol(t));
}
/* CONVERTS STRING TO UPPER CASE */
void upstr (char *t ) {
while (*t!=0) {
*t = toupper(*t);
t++;
}
}
/* BREAKS STRING INTO LINES ACCORDING TO ; \n \r */
char *strlin (char **tnext) {
char *head, *tail;
/* Start string after any leading ; \n \r */
head = *tnext;
while (*head==';' || *head=='\n' || *head=='\r')
head++;
/* Find end-of-string OR first line separator ; \n \r */
tail = head;
while (*tail && *tail!=';' && *tail!='\n' && *tail!='\r')
tail++;
/* Insert null character to convert line end to end-of-string */
if (*tail) {
*tail=0;
/* Skip forward to until not ; /n /r */
tail++;
while (*tail==';' || *tail=='\n' || *tail=='\r') tail++;
}
/* return pointer to string, and address of next string */
*tnext = tail;
return head;
}
/* Returns True if first non-whitespace characters is in string */
BOOLEAN IsComment (char *InputString, char *CommentChars)
{
char LeadingChar;
int TestIndex;
/* Find first non-white space character */
SkipWhiteSpace (&InputString);
LeadingChar = InputString[0];
/* Scan list of comment characters to see if leading character belongs */
TestIndex = 0;
while
(
CommentChars[TestIndex]!='\0' &&
CommentChars[TestIndex]!=LeadingChar
)
TestIndex++;
/* Return true if matching characters not string terminatore */
return CommentChars[TestIndex] != '\0';
}
BOOLEAN IsBlank (char *InputString)
{
SkipWhiteSpace (&InputString);
if (InputString[0]=='\0')
return TRUE;
else
return FALSE;
}
void SkipWhiteSpace (char **s)
{
while ( IsWhiteSpace(**s) )
(*s)++;
}
/*
Returns true if next token in String is Token
*/
BOOLEAN IsFirstToken (char *Token, char *String)
{
int Len;
Len = strlen (Token);
SkipWhiteSpace(&String);
return
!strncmpi(Token,String,Len) &&
(IsWhiteSpace(String[Len]) || String[Len]=='\0');
}
/* Returns next token or quoted string if present */
char *GetQuotedString (char **Input, char QuoteChar)
{
char *head;
char *tail;
tail = *Input;
/* FIND FIRST NONBLANK CHARACTER */
while ( (*tail)
&& ( (*tail)==' ' || (*tail)=='\t' || (*tail)=='\n' || (*tail)=='\r' ) )
tail++;
/* Return quoted string if next character is a quote */
if (*tail==QuoteChar)
{
tail++;
head = tail;
tail = strchr (tail, QuoteChar);
if (NULL==tail)
{
tail = head + strlen(head);
}
/* Point past quote char */
else
{
tail++;
}
#if 0
head = strsep (&tail, QuoteCharStr);
#endif
}
/* else return next token (delimited by space, \n or \t) */
else
{
head = strhed (&tail);
}
/* Set pointer to remaining string, return pointer to token */
*Input = tail;
return head;
}
/*
Replace double character sequence '\' 'n'
with a single new-line character
*/
void ReplaceSpecialChar (char *src)
{
char *dst = src;
while (*src!='\0')
{
/* Replace new line */
if (*src=='\\' && src[1]=='n')
{
*dst++ = '\n';
src += 2;
}
/* copy character */
else
{
if (dst!=src)
*dst = *src;
dst++;
src++;
}
}
*dst = '\0';
}
/*
***********************************************************************
Local Functions
***********************************************************************
*/
void TruncateWhiteSpace (char *InputStr)
{
int LastPos;
/* Got to string end */
LastPos = strlen(InputStr)-1;
/* Find first non-blank character */
while (LastPos>=0 && IsWhiteSpace(InputStr[LastPos]))
{
LastPos--;
}
/* Mark string end at first character after non-blank */
InputStr[LastPos+1] = '\0';
}
BOOLEAN IsWhiteSpace (char InputChar)
{
return
(
InputChar == ' ' ||
InputChar == '\n' ||
InputChar == '\t' ||
InputChar == '\r' ||
InputChar == '\b' ||
InputChar == '\f' ||
InputChar == '\v'
);
}