OpenRadioss 2025.1.11
OpenRadioss project
Loading...
Searching...
No Matches
lf_convert_c.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 "hardware.inc"
24#include <stdio.h>
25#include <string.h>
26#include <stdlib.h>
27#include <errno.h>
28#include <math.h>
29#include <fcntl.h>
30
31#ifdef _WIN64
32#include <sys\types.h>
33#include <errno.h>
34#include <signal.h>
35#include <windows.h>
36#include <winbase.h>
37#include <process.h>
38#include <string.h>
39#include <ctype.h>
40#include <io.h>
41#include <sys/stat.h>
42#include <direct.h>
43#define _FCALL
44#define TODOS 1
45
46#else
47
48#include <sys/resource.h>
49#include <sys/types.h>
50#include <time.h>
51#include <sys/stat.h>
52#include <unistd.h>
53#include <sys/param.h>
54#define _FCALL
55#define TODOS 0
56#endif
57
58#ifndef BUFSIZE
59#define BUFSIZE 1024
60#endif
61
62#define CR (int) 0x0D
63#define LF (int) 0x0A
64#define MAXINCLVL 15
65#define MAXINCNAMELEN 128
66
67#ifdef _WIN64
68int readbuf (HANDLE fid, char *buf, int nbytes);
69int writebuf(HANDLE fid, char *buf, int nbytes);
70void do_lf_convert (HANDLE fdi, HANDLE fdo);
71#else
72int readbuf (int fid, char *buf, int nbytes);
73int writebuf(int fid, char *buf, int nbytes);
74void do_lf_convert (int fdi, int fdo);
75#endif
76static char* tmpenv_c();
77/***************************************************************************/
78
79static void syserr(char *msg)
80{
81 fprintf(stderr,"SYSTEM ERROR>> ");
82 perror(msg);
83}
84
85static void syserr_fatal(char *msg)
86{
87 fprintf(stderr,"SYSTEM ERROR>> ");
88 perror(msg);
89 exit(1);
90}
91
92static void fatal(char *msg)
93{
94 fprintf(stderr,"%s\n", msg);
95 exit(1);
96}
97
98/***************************************************************************/
99#ifdef _WIN64
100int readbuf(HANDLE fid, char *buf, int nbytes)
101#else
102int readbuf(int fid, char *buf, int nbytes)
103#endif
104{
105 int ncount, nread;
106#ifdef _WIN64
107 unsigned long done;
108 BOOL fSuccess;
109#else
110 int done;
111#endif
112 ncount = nbytes;
113 nread = 0;
114 while (ncount > 0) {
115#ifdef _WIN64
116 fSuccess = ReadFile(fid, buf, ncount*sizeof(TCHAR), &done, NULL);
117#else
118 done = read(fid, (void *) buf, (size_t) ncount);
119#endif
120 nread += done;
121 if (done < 0)
122 fatal("Failed reading buffer");
123 else if (done == 0)
124 break;
125 ncount -= done;
126 buf += done;
127 }
128 return nread;
129}
130
131#ifdef _WIN64
132 int writebuf(HANDLE fid, char *buf, int nbytes)
133#else
134 int writebuf(int fid, char *buf, int nbytes)
135#endif
136{
137 int ncount, nwrite;
138#ifdef _WIN64
139 BOOL fSuccess;
140 unsigned long done;
141#else
142 int done;
143#endif
144
145 ncount = nbytes;
146 nwrite = 0;
147 while (ncount > 0){
148#ifdef _WIN64
149 fSuccess = WriteFile(fid, buf,ncount*sizeof(TCHAR),&done,NULL);
150#elif 1
151 done = write(fid, (void *) buf, (size_t) ncount);
152#endif
153 nwrite += done;
154 if (done < 0)
155 fatal("Failed writing buffer");
156 ncount -= done;
157 buf += done;
158 }
159 return nwrite;
160}
161
162
163#ifdef _WIN64
164
165
166static char* tmpenv_c(){
167
168 char * tmpdir;
169 int sz_tmpdir;
170
171 tmpdir = malloc(sizeof(char)*2048);
172
173 sz_tmpdir = GetEnvironmentVariable("TMPDIR",tmpdir,2048);
174
175 /* second trial get current working directory */
176 if ( sz_tmpdir ==0 ){
177 free(tmpdir);
178 tmpdir = _getcwd( NULL, 0 );
179 }
180 return tmpdir;
181}
182
183static char* cwd_c(){
184 char * tmpdir;
185
186 tmpdir = (char *)calloc(2048,sizeof(char));
187 tmpdir = _getcwd( NULL, 0 );
188 return tmpdir;
189}
190#elif 1
191
192static char* tmpenv_c(){
193 char * tmpdir;
194
195 tmpdir = getenv("TMPDIR");
196 /* second trial get current working directory */
197 if (tmpdir==NULL){
198 tmpdir = (char *)calloc(2048,sizeof(char));
199 getcwd(tmpdir,2048);
200 }
201 return tmpdir;
202}
203
204static char* cwd_c(){
205 char * tmpdir;
206
207 tmpdir = (char *)calloc(2048,sizeof(char));
208 getcwd(tmpdir,2048);
209 return tmpdir;
210}
211#endif
212/***************************************************************************/
213
214#ifdef _WIN64
215void do_lf_convert (HANDLE fdi, HANDLE fdo)
216#else
217void do_lf_convert (int fdi, int fdo)
218#endif
219{
220 int nread, nwrite, i, ch, last, len;
221 char inbuf[BUFSIZE], outbuf[BUFSIZE*2];
222
223 if (TODOS) /* convert to DOS */
224 do {
225 nwrite = 0;
226 nread = readbuf(fdi, inbuf, (int )sizeof(inbuf));
227 for (i=0, len=0; i < nread; i++) {
228 ch = inbuf[i];
229 if (ch == LF) {
230 outbuf[len++] = CR;
231 outbuf[len++] = LF;
232 }
233 else
234 outbuf[len++] = ch;
235 last = ch;
236 }
237 nwrite = writebuf(fdo, outbuf, len);
238 } while (nread != 0);
239 else /* convert to UNIX */
240 do {
241 nwrite = 0;
242 nread = readbuf(fdi, inbuf, (int )sizeof(inbuf));
243
244 for (i=0, len=0; i < nread; i++) {
245 ch = inbuf[i];
246 if (ch == CR)
247 outbuf[len++] = LF;
248 else if (ch != LF || last != CR)
249 outbuf[len++] = ch;
250 last = ch;
251 }
252 nwrite = writebuf(fdo, outbuf, len);
253 } while (nread != 0);
254}
255
256
257
258void convertfile(FILE *stream, int level,FILE *stream_out,char *filename,int ncharline,int *ierr){
259
260 char *line,*line1,*tag;
261 char *idchr;int id;
262 char *newinc;
263 int k,val,nlevel,snewinc,linelen;
264 FILE *newstream;
265 int firstline=1,i,j;
266 int iend2=0;
267 char *pch,*pch1,*pch2;
268 const char *cs = " ";
269 line=(char *)malloc(sizeof(char)* ncharline);
270 line1=(char *)malloc(sizeof(char)* ncharline);
271 tag=(char *)malloc(sizeof(char)* ncharline);
272 idchr=(char *)malloc(sizeof(char)* ncharline);
273 newinc=(char *)malloc(sizeof(char)* ncharline);
274
275 if (level > MAXINCLVL) {
276 printf("Max level=%d reached\n",level);
277 printf("Include file %s skipped.\n",filename);
278 return;
279 }
280 nlevel = level +1;
281 /*
282 123456789012345678901234567890123456789012345678901234567890 */
283 /* sprintf(line1,"### include header for parameters # include level=%5d ### %s\n",nlevel,filename);
284 */
285
286 while (fgets(line,ncharline,stream)!=NULL){
287 linelen=strlen(line);
288 /* delete CR, assume \n at eof, skip blank lines */
289 if ( linelen > 1 ) {
290 if ( line[linelen-2] == '\r' ) {
291 linelen --;
292 line[linelen-1] = line[linelen];
293 line[linelen] = '\0';
294 }
295 }
296 if ( line[linelen-1] != '\n' ) {
297 line[linelen] = '\n';
298 linelen++;
299 line[linelen] = '\0';
300 }
301 if (firstline == 1 && strncmp(line,"#",1)==0 && level == 0) {
302 fputs(line,stream_out);
303 }
304 /* remove comment lines */
305 else if ( ( strncmp(line,"#",1)==0 && strncmp(line,"#include ",9)==0 && strncmp(line,"#INCLUDE ",9)==0 ) ||
306 strncmp(line,"$",1)==0 ) {
307 continue ;
308 }
309 /* add submodel tag 1234567890*/
310 else if ( strncmp(line,"//SUBMODEL",10)==0 ){
311 nlevel++;
312 // sprintf(line1,"### include header for parameters # include level=%5d ### %s\n",nlevel,filename);
313 // fputs(line1,stream_out);
314 fputs(line,stream_out);
315 }
316 else if ( strncmp(line,"//ENDSUB",8)==0 ){
317 fputs(line,stream_out);
318 // sprintf(line1,"### include footer for parameters # include level=%5d ### %s\n",nlevel,filename);
319 // fputs(line1,stream_out);
320 nlevel--;
321 }
322 else if ( ( (strncmp(line,"/END",4)==0 && strncmp(line,"/END/ENGINE",11)!=0) ||
323 strncmp(line,"#enddata",8)==0 ||
324 strncmp(line,"#ENDDATA",8)==0 ) &&
325 nlevel > 1 ){
326 if (strncmp(line,"/END",4)==0) {
327 printf("Warning in include file %s : /END detected !\n",filename);
328 }
329 return;
330 }
331 else if ( strncmp(line,"/END",4)==0 &&
332 strncmp(line,"/END/ENGINE",11)!=0 ){
333 fputs(line,stream_out);
334 // if (nlevel > 1) {
335 // sprintf(line1,"### include footer for parameters # include level=%5d ### %s\n",nlevel,filename);
336 // fputs(line1,stream_out);
337 // }
338 // nlevel--;
339 if (nlevel == 1) {
340 // ALTDOCTAG
341 while (fgets(line,ncharline,stream)!=NULL && iend2!=1){
342 if ( strncmp(line,"/ALTDOCTAG",10)==0 ){
343 fputs(line,stream_out);
344 fgets(line,ncharline,stream);
345 fputs(line,stream_out);
346 iend2=1;
347 }
348 }
349 return;
350 }
351 }
352 else if( strncmp(line,"/END",4)==0 &&
353 strncmp(line,"/END/ENGINE",11)==0 ){
354 fputs(line,stream_out);
355 free(line);
356 free(line1);
357 free(tag);
358 free(idchr);
359 free(newinc);
360 return ;
361 }
362 else {
363 fputs(line,stream_out);
364 }
365 firstline=0;
366 }
367 if (strncmp(line,"/END",4)==0 ) {
368 printf("myline %s \n",line);
369 }
370
371 // sprintf(line1,"### include footer for parameters # include level=%5d ### %s\n",nlevel,filename);
372 // fputs(line1,stream_out);
373 free(line);
374 free(line1);
375 free(tag);
376 free(idchr);
377 free(newinc);
378}
379
380
381void lf_convert_c_flat(int *got_input, char *rootname, int *rootlen, char *filename, int *namelen, char *outname, int *ierr, int *ncharline,int *len_path,char *path,int *len_path2,char *path2)
382{
383 char *inname, *outname_local ;
384 char tmpstr[20];
385 int fdi, fdo, pid, ifclose_in, ifclose_out;
386 FILE * stream;
387 FILE * stream_out;
388 int sz_lenpath;
389 int hostname_err;
390
391#ifdef _WIN64
392 char tmpstr_host[MAX_COMPUTERNAME_LENGTH+1];
393 int size_tmpstr_host;
394 size_tmpstr_host=MAX_COMPUTERNAME_LENGTH+1;
395 WSADATA wsadata;
396 WORD version= MAKEWORD(1,1);
397 int nRet,le;
398#else
399 char tmpstr_host[MAXHOSTNAMELEN];
400 int size_tmpstr_host;
401 size_tmpstr_host=MAXHOSTNAMELEN;
402#endif
403 inname = NULL;
404 outname_local = NULL;
405 ifclose_in=0;
406 *ierr = 0;
407 if (*got_input == 1) {
408 if(*len_path==0)
409 {
410 inname = (char *) calloc(*namelen+1, sizeof (char));
411#ifdef _WIN64
412 strncpy_s(inname,*namelen+1,filename,*namelen);
413#else
414 strncpy(inname,filename,*namelen);
415#endif
416 }
417 else
418 {
419 inname = (char *) calloc(*namelen+1+ *len_path, sizeof (char));
420#ifdef _WIN64
421 strncpy_s(inname,*namelen+1+ *len_path,path,*len_path);
422 strncat_s(inname,*namelen+1+ *len_path,filename,*namelen);
423#else
424 strncpy(inname,path,*len_path);
425 strncat(inname, filename, *namelen);
426#endif
427 }
428 // if ((fdi = open (inname, O_RDONLY,"r")) == -1) {
429#ifdef _WIN64
430 fopen_s(&stream,inname,"r");
431#else
432 stream = fopen(inname,"r");
433#endif
434 // }
435 if (stream == NULL) {
436 fprintf (stderr, "*** ERROR IN OPENING INPUT FILE : %s !\n", inname);
437 *ierr = 1;
438 }
439 }
440 if( *ierr==0 ) {
441#ifdef _WIN64
442 pid = _getpid();
443 nRet = WSAStartup(version,&wsadata);
444 hostname_err=gethostname(tmpstr_host,size_tmpstr_host);
445 le=WSAGetLastError();
446 if (hostname_err != 0) {
447 strcpy_s(tmpstr_host,size_tmpstr_host,"Unknown");
448 }
449
450#else
451 pid = getpid();
452 gethostname(tmpstr_host,size_tmpstr_host);
453#endif
454 outname[0] = '\0' ;
455 sprintf(tmpstr,"%d",pid);
456 if(*len_path2==0)
457 {
458 // current working directory
459 sz_lenpath=2148;
460 outname_local = (char *) calloc(sz_lenpath, sizeof (char));
461 char * cwd = cwd_c();
462#ifdef _WIN64
463 strcpy_s(outname_local,sz_lenpath,cwd);
464 strcat_s(outname_local,sz_lenpath,"\\");
465#else
466 strcpy(outname_local,cwd);
467 strcat(outname_local, "/");
468#endif
469 free(cwd);
470 }
471 else
472 {
473 sz_lenpath=*len_path2 +1 + 2148;
474 outname_local = (char *) calloc(sz_lenpath, sizeof (char));
475 outname_local[0]='\0';
476#ifdef _WIN64
477 strncpy_s(outname_local,sz_lenpath,path2, *len_path2);
478#else
479 strncpy(outname_local, path2, *len_path2);
480#endif
481 // user working directory
482 }
483
484#ifdef _WIN64
485 strncat_s(outname_local,sz_lenpath, rootname, *rootlen);
486 strcat_s(outname_local,sz_lenpath, "_");
487 strcat_s(outname_local,sz_lenpath, tmpstr);
488 strcat_s(outname_local,sz_lenpath, "_");
489 strcat_s(outname_local,sz_lenpath, tmpstr_host);
490 strcat_s(outname,sz_lenpath, outname_local);
491 *namelen = (int) strlen(outname);
492 fopen_s(&stream_out,outname,"w");
493
494#else
495 strncat(outname_local, rootname, *rootlen);
496 strcat(outname_local, "_");
497 strcat(outname_local, tmpstr);
498 strcat(outname_local, "_");
499 strcat(outname_local, tmpstr_host);
500 strcat(outname, outname_local);
501 *namelen = (int) strlen(outname);
502 stream_out = fopen(outname,"w");
503
504#endif
505
506 if (stream_out == NULL) {
507 fprintf (stderr, " *** ERROR INPUT FILE: CANNOT CREATE TEMP FILE : %s !\n",outname);
508 *ierr = 1;
509 }
510
511 if (stream != NULL && stream_out != NULL) {
512 convertfile(stream,0,stream_out,inname,*ncharline,ierr);
513
514 ifclose_in=fclose(stream);
515 ifclose_out=fclose(stream_out);
516
517 if (ifclose_in != 0) {
518 syserr("Error: close input (flat deck)");
519 }
520 if (ifclose_out != 0) {
521 syserr("Error: close output (flat deck)");
522 }
523 }
524 }
525 free(outname_local);
526 free(inname);
527}
528
529/*-------------------------------------------------------------------------------*/
530
531void _FCALL LF_CONVERT_C_FLAT(int *got_input, char *rootname, int *rootlen, char *filename, int *namelen, char *outname, int *ierr, int *ncharline,int *len_path,char *path,int *len_path2,char *path2)
532{ lf_convert_c_flat(got_input, rootname, rootlen, filename, namelen, outname, ierr, ncharline,len_path,path,len_path2,path2);}
533
534void lf_convert_c_flat_(int *got_input, char *rootname, int *rootlen, char *filename, int *namelen, char *outname, int *ierr, int *ncharline,int *len_path,char *path,int *len_path2,char *path2)
535{ lf_convert_c_flat(got_input, rootname, rootlen, filename, namelen, outname, ierr, ncharline,len_path,path,len_path2,path2);}
536
537void lf_convert_c_flat__(int *got_input, char *rootname, int *rootlen, char *filename, int *namelen, char *outname, int *ierr, int *ncharline,int *len_path,char *path,int *len_path2,char *path2)
538{ lf_convert_c_flat(got_input, rootname, rootlen, filename, namelen, outname, ierr, ncharline,len_path,path,len_path2,path2);}
539
540
541/*-------------------------------------------------------------------------------*/
return
Definition dmumps.m:15
void lf_convert_c_flat(int *got_input, char *rootname, int *rootlen, char *filename, int *namelen, char *outname, int *ierr, int *ncharline, int *len_path, char *path, int *len_path2, char *path2)
#define TODOS
static void syserr_fatal(char *msg)
#define LF
void lf_convert_c_flat_(int *got_input, char *rootname, int *rootlen, char *filename, int *namelen, char *outname, int *ierr, int *ncharline, int *len_path, char *path, int *len_path2, char *path2)
void lf_convert_c_flat__(int *got_input, char *rootname, int *rootlen, char *filename, int *namelen, char *outname, int *ierr, int *ncharline, int *len_path, char *path, int *len_path2, char *path2)
#define CR
static void fatal(char *msg)
int readbuf(int fid, char *buf, int nbytes)
void _FCALL LF_CONVERT_C_FLAT(int *got_input, char *rootname, int *rootlen, char *filename, int *namelen, char *outname, int *ierr, int *ncharline, int *len_path, char *path, int *len_path2, char *path2)
void convertfile(FILE *stream, int level, FILE *stream_out, char *filename, int ncharline, int *ierr)
#define MAXINCLVL
int writebuf(int fid, char *buf, int nbytes)
static char * cwd_c()
void do_lf_convert(int fdi, int fdo)
static char * tmpenv_c()
#define _FCALL
initmumps id
#define BUFSIZE
Definition rad2rad_c.c:94
void syserr()