ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-1.0/libmdtools/StringUtils.c
Revision: 1334
Committed: Fri Jul 16 18:58:03 2004 UTC (20 years ago) by gezelter
Content type: text/plain
File size: 3117 byte(s)
Log Message:
Initial import of OOPSE-1.0 source tree

File Contents

# User Rev Content
1 gezelter 1334 #include <stdio.h>
2     #include <math.h>
3     #include <stdlib.h>
4     #include "simError.h"
5     #include "StringUtils.h"
6    
7     int fastForwardToBeginBlock(FILE* theFile, char* blockName ){
8     int foundText = 0;
9     int lineNum;
10     char* the_token;
11     char* junk_token;
12     char* eof_test;
13     char readLine[500];
14    
15     rewind( theFile );
16     lineNum = 0;
17    
18     eof_test = fgets( readLine, sizeof(readLine), theFile );
19     lineNum++;
20     if( eof_test == NULL ){
21     sprintf( painCave.errMsg,
22     "fastForwardToBeginBlock: File Is Empty!\n");
23     painCave.isFatal = 1;
24     simError();
25     return -1;
26     }
27    
28     while( !foundText ){
29    
30     if( eof_test == NULL ){
31     sprintf( painCave.errMsg,
32     "fastForwardToBeginBlock: File Ended Unexpectedly!\n"
33     "\tAt Line: %d\n", lineNum);
34     painCave.isFatal = 1;
35     simError();
36     return -1;
37     }
38    
39     if (isBeginLine(readLine)) {
40     junk_token = strtok( readLine, " ,;\t" );
41     the_token = TrimSpaces(strtok( NULL, " ,;\t" ));
42     foundText = !strcasecmp( blockName, the_token );
43     }
44    
45     if( !foundText ){
46     eof_test = fgets( readLine, sizeof(readLine), theFile );
47     lineNum++;
48    
49    
50     if (eof_test == NULL) {
51     sprintf( painCave.errMsg,
52     "fastForwardToBeginBlock: File Ended Unexpectedly!\n"
53     "\tAt Line: %d\n", lineNum);
54     painCave.isFatal = 1;
55     simError();
56     return -1;
57     }
58     }
59     }
60    
61     return lineNum;
62     }
63    
64     int isBeginLine(char *line) {
65     /* checks to see if the first token on the line is "begin" */
66     char *working_line;
67     char *foo;
68    
69     working_line = strdup(line);
70    
71     foo = strtok(working_line, " ,;\t");
72    
73     if (!strcasecmp(foo, "begin")) return 1;
74    
75     return 0;
76     }
77    
78     int isEndLine(char *line) {
79     /* checks to see if the first token on the line is "end" */
80     char *working_line;
81     char *foo;
82    
83     working_line = strdup(line);
84    
85     foo = strtok(working_line, " ,;\t");
86    
87     if (!strcasecmp(foo, "end")) return 1;
88    
89     return 0;
90     }
91    
92    
93     int count_tokens(char *line, char *delimiters) {
94     /* PURPOSE: RETURN A COUNT OF THE NUMBER OF TOKENS ON THE LINE. */
95    
96     char *working_line; /* WORKING COPY OF LINE. */
97     int ntokens; /* NUMBER OF TOKENS FOUND IN LINE. */
98     char *strtok_ptr; /* POINTER FOR STRTOK. */
99    
100     strtok_ptr= working_line= strdup(line);
101    
102     ntokens=0;
103     while (strtok(strtok_ptr,delimiters)!=NULL)
104     {
105     ntokens++;
106     strtok_ptr=NULL;
107     }
108    
109     free(working_line);
110     return(ntokens);
111     }
112    
113     /**
114     * Removes left and right spaces from a string
115     *
116     * @param str String to trim
117     *
118     * @return char* to the trimed string
119     */
120     char * TrimSpaces(char *str) {
121     size_t len;
122     char *right, *left;
123    
124     if (strlen(str) == 0) return(str);
125    
126     /* Trim whitespace from left side */
127     for (left = str; isspace(*left); left++);
128    
129     /* Trim whitespace from right side */
130     if ((len = strlen(left)))
131     {
132     right = left + (len - 1);
133    
134     while (isspace(*right))
135     {
136     *right = '\0';
137     right--;
138     }
139     }
140    
141     /* Only do the str copy if their was spaces to the left */
142     if (left != str)
143     strcpy(str, left);
144    
145     return (str);
146     }