Debian Codegraph @

packages/seacas/applications/nem_spread/pe_str_util.C

raw
1 /*
2 * Copyright(C) 1999-, 20212021 National Technology & Engineering Solutions
3 * of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
4 * NTESS, the U.S. Government retains certain rights in this software.
5 *
6 * See packages/seacas/LICENSE for details
7 */
8 #include <cctype> // for isupper, tolower
9 #include <cstddef> // for size_t
10 #include <cstring> // for strlen
11
12 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13 * Author(s): Gary L. Hennigan (SNL 9221)
14 *----------------------------------------------------------------------------
15 * Functions contained in this file:
16 * token_compare()
17 * strip_string()
18 * clean_string()
19 * string_to_lower()
20 *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
21
22 /*****************************************************************************/
23 /*****************************************************************************/
24 /*****************************************************************************/
25 int token_compare(char *token, const char *key)
26 {
27
28 size_t kcnt = 0;
29
30 size_t key_len = strlen(key);
31
32 for (size_t i1 = 0; i1 < strlen(token); i1++) {
33 if (isupper(token[i1]) != 0) {
34 token[i1] = (char)tolower(token[i1]);
35 }
36
37 if (token[i1] != ' ') {
38 if (token[i1] == key[kcnt]) {
39 kcnt++;
40 if (kcnt > key_len) {
41 return 0;
42 }
43 }
44 else {
45 return 0;
46 }
47 }
48 if (key[kcnt] == ' ') {
49 kcnt++;
50 }
51 }
52
53 if (kcnt == strlen(key)) {
54 return 1;
55 }
56 return 0;
57 } /*--------------End token_compare()-----------*/
58
59 /*****************************************************************************/
60 /*****************************************************************************/
61 /*****************************************************************************/
62 void strip_string(char inp_str[], const char *tokens)
63 {
64 int j;
65 int itok;
66 int bval;
67
68 int i = 0;
69 int ntokes = (int)strlen(tokens);
70
71 while (inp_str[i] != '\0') {
72 bval = 0;
73 for (itok = 0; itok < ntokes; itok++) {
74 if (inp_str[i] == tokens[itok]) {
75 i++;
76 bval = 1;
77 break; /* out of for loop */
78 }
79 }
80 if (bval == 0) {
81 break; /* out of while loop */
82 }
83 }
84
85 /* Move real part of string to the front */
86 j = 0;
87 while (inp_str[j + i] != '\0') {
88 inp_str[j] = inp_str[j + i];
89 j++;
90 }
91 inp_str[j] = inp_str[j + i];
92 j--;
93
94 /* Remove trailing tokens */
95 while (j != -1) {
96 bval = 0;
97 for (itok = 0; itok < ntokes; itok++) {
98 if (inp_str[j] == tokens[itok]) {
99 bval = 1;
100 j--;
101 break; /* out of for loop */
102 }
103 }
104 if (bval == 0) {
105 break; /* out of while loop */
106 }
107 }
108
109 inp_str[j + 1] = '\0';
110 }
111
112 /*****************************************************************************/
113 /*****************************************************************************/
114 /*****************************************************************************/
115 void clean_string(char inp_str[], const char *tokens)
116 {
117 int j;
118 int itok;
119
120 int ntokes = (int)strlen(tokens);
121 int inplen = (int)strlen(inp_str);
122
123 int i = 0;
124 int bval = 0;
125 while (inp_str[i] != '\0') {
126 for (itok = 0; itok < ntokes; itok++) {
127 if (inp_str[i] == tokens[itok]) {
128 /* Find out if the next character is also a token */
129 for (j = 0; j < ntokes; j++) {
130 if (inp_str[i + 1] == tokens[j]) {
131 bval = 1;
132 break;
133 }
134 }
135
136 if (bval == 1) {
137 for (j = i + 1; j < inplen; j++) {
138 inp_str[j] = inp_str[j + 1];
139 }
140
141 inplen--;
142 bval = 0;
143 i--;
144 if (i < 0) {
145 i = 0;
146 }
147 }
148 }
149 }
150
151 i++;
152
153 } /* End "while(inp_str[i] != '\0')" */
154
155 } /*---------------- End clean_string() -----------------*/
156
157 /*****************************************************************************/
158 /*****************************************************************************/
159 /*****************************************************************************/
160 void string_to_lower(char in_string[], const char cval)
161 {
162 int len = (int)strlen(in_string);
163 for (int cnt = 0; cnt < len; cnt++) {
164 if (in_string[cnt] == cval) {
165 return;
166 }
167
168 if (isupper(in_string[cnt]) != 0) {
169 in_string[cnt] = tolower(in_string[cnt]);
170 }
171 }
172 }