OpenRadioss 2025.1.11
OpenRadioss project
Loading...
Searching...
No Matches
analyse_string.c
Go to the documentation of this file.
1//Copyright> OpenRadioss
2//Copyright> Copyright (C) 1986-2025 Altair Engineering Inc.
3//Copyright>
4//Copyright> This program is free software: you can redistribute it and/or modify
5//Copyright> it under the terms of the GNU Affero General Public License as published by
6//Copyright> the Free Software Foundation, either version 3 of the License, or
7//Copyright> (at your option) any later version.
8//Copyright>
9//Copyright> This program is distributed in the hope that it will be useful,
10//Copyright> but WITHOUT ANY WARRANTY; without even the implied warranty of
11//Copyright> MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12//Copyright> GNU Affero General Public License for more details.
13//Copyright>
14//Copyright> You should have received a copy of the GNU Affero General Public License
15//Copyright> along with this program. If not, see <https://www.gnu.org/licenses/>.
16//Copyright>
17//Copyright>
18//Copyright> Commercial Alternative: Altair Radioss Software
19//Copyright>
20//Copyright> As an alternative to this open-source version, Altair also offers Altair Radioss
21//Copyright> software under a commercial license. Contact Altair to discuss further if the
22//Copyright> commercial version may interest you: https://www.altair.com/radioss/.
23#include <string.h>
24
25#include "analyse_memory.h"
26
27#include "analyse_string.h"
28
29/*****************************************************************************
30 * replace the strset function which is not in ANSI string.h
31 *****************************************************************************/
32char *analyse_string_strset(char *name, int ch)
33{
34 int length,i;
35
36 if ( name != NULL)
37 {
38 length = strlen(name);
39
40 for ( i=0; i<length; i++)
41 {
42 name[i]= ch;
43 }
44
45 }
46 return name;
47}
48
49
50/*****************************************************************************
51 * replace the strrev function which is not in ANSI string.h
52 *****************************************************************************/
53char *analyse_string_strrev(char *name)
54{
55 char *reverse;
56 int length,i;
57
58 if ( name != NULL)
59 {
60 length = strlen(name);
61 reverse = (char *) analyse_malloc( ( length)*sizeof(char));
62
63 for ( i=0; i<length; i++)
64 {
65 reverse[i]=name[length-1-i];
66 }
67
68 for ( i=0; i<length; i++)
69 {
70 name[i]=reverse[i];
71 }
72
73 analyse_free(reverse);
74 }
75 return name;
76}
77
78/*****************************************************************************
79 * remove blanks at the beginning of a string " a a cc " -> "a a cc "
80 *****************************************************************************/
81char *analyse_string_fit_start(char *name)
82{
83 if (name != NULL)
84 {
85 while ( (*name) == ' ')
86 {
87 strcpy(name,name+1);
88 }
89 }
90 return name;
91}
92
93
94/*****************************************************************************
95 * remove blanks at the end of a string " a a cc " -> " a a cc"
96 *****************************************************************************/
97char *analyse_string_fit_end(char *name)
98{
99 int length;
100
101 if ( name != NULL)
102 {
103 length = strlen(name);
104
105 while ( ( length > 0) && ( *(name+length-1) == ' ' ) )
106 {
107 *(name+length-1)= '\0';
108 length = length -1;
109 }
110
111 }
112 return name;
113}
114
115
116
117/*****************************************************************************
118 * remove blanks at the beginning AND the end of a string " a a cc " -> "a a cc"
119 *****************************************************************************/
121{
122 if (name != NULL)
123 {
126 }
127 return name;
128}
129
130
131/*****************************************************************************
132 * remove blanks in a string " a a cc " ->"aacc"
133 *****************************************************************************/
134char *analyse_string_fit_all(char *name)
135{
136 char *blank;
137
138 if (name != NULL)
139 {
140 blank = strchr(name,' ');
141
142 while ( blank != NULL )
143 {
144 strcpy(blank,blank+1);
145 blank = strchr(name,' ');
146 }
147 }
148 return name;
149}
150/*****************************************************************************
151 * returns 0 if strlen(string) is between n1 and n2
152 * returns 1 if > max(n1,n2) ;
153 * returns -1 if < min(n1,n2) ;
154 *****************************************************************************/
155extern int analyse_string_length_brackett(char *string, int n1, int n2)
156{
157 int length = 0 ;
158 int buf = 0 ;
159 char *buf_string = NULL ;
160 char *pos = NULL ;
161
162 if (strlen(string)==0) return -1 ;
163 if (n1-n2 > 0)
164 {
165 buf = n1 ;
166 n1 = n2 ;
167 n2 = buf ;
168 }
169
170 buf_string = (char *)analyse_calloc(strlen(string)+1, sizeof(char)) ;
171
172 strcpy(buf_string, string) ;
173 pos = strrchr(buf_string, '/') ;
174 if (pos != NULL)
175 {
176 pos = pos + 1 ;
177 length = strlen(pos) ;
178 }
179 else
180 {
181 length = strlen(buf_string) ;
182 }
183
184 if (length < n1)
185 {
186 analyse_free(buf_string) ;
187 return -1 ;
188 }
189 if (length > n2)
190 {
191 analyse_free(buf_string) ;
192 return 1 ;
193 }
194
195 analyse_free(buf_string) ;
196 return 0 ;
197}
198
199/*****************************************************************************
200 * converts an int, into a char
201 *****************************************************************************/
202void analyse_convert_int_to_string(int nb_int, int *tab_int, char *message)
203{
204 int i;
205
206 if ( ( message != NULL ) &&
207 ( tab_int != NULL ))
208 {
209 for(i=0; i<nb_int; i++)
210 {
211 message[i]=(char)(tab_int[i]);
212 }
213
214 message[nb_int]='\0';
215 }
216 else
217 {
218 if ( message != NULL ) message[0]='\0';
219 }
220}
221/*****************************************************************************
222 * converts a char into a tab of int
223 *****************************************************************************/
224void analyse_convert_string_to_int(char *message, int *nb_int, int **tab_int)
225{
226 int i;
227
228 if ( ( message != NULL ) &&
229 ( nb_int != NULL ) &&
230 ( tab_int != NULL ))
231 {
232 *nb_int=strlen(message);
233 (*tab_int) = (int *)analyse_malloc((*nb_int)*sizeof(int));
234
235 for(i=0; i<(*nb_int); i++)
236 {
237 (*tab_int)[i] = (int)message[i];
238 }
239 }
240 else
241 {
242 if ( nb_int != NULL) *nb_int = 0;
243 if ( tab_int != NULL) *tab_int = NULL;
244 }
245}
void * analyse_calloc(size_t nitems, size_t size)
void * analyse_malloc(size_t size)
void analyse_free(void *block)
char * analyse_string_strrev(char *name)
char * analyse_string_fit_start(char *name)
char * analyse_string_fit_start_end(char *name)
int analyse_string_length_brackett(char *string, int n1, int n2)
char * analyse_string_strset(char *name, int ch)
void analyse_convert_int_to_string(int nb_int, int *tab_int, char *message)
char * analyse_string_fit_end(char *name)
char * analyse_string_fit_all(char *name)
void analyse_convert_string_to_int(char *message, int *nb_int, int **tab_int)