OpenRadioss 2025.1.11
OpenRadioss project
Loading...
Searching...
No Matches
lf_convert_c.c File Reference
#include "hardware.inc"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <math.h>
#include <fcntl.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/param.h>

Go to the source code of this file.

Macros

#define _FCALL
#define TODOS   0
#define BUFSIZE   1024
#define CR   (int) 0x0D
#define LF   (int) 0x0A
#define MAXINCLVL   15
#define MAXINCNAMELEN   128

Functions

int readbuf (int fid, char *buf, int nbytes)
int writebuf (int fid, char *buf, int nbytes)
void do_lf_convert (int fdi, int fdo)
static char * tmpenv_c ()
static void syserr (char *msg)
static void syserr_fatal (char *msg)
static void fatal (char *msg)
static char * cwd_c ()
void convertfile (FILE *stream, int level, FILE *stream_out, char *filename, int ncharline, int *ierr)
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 _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 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)

Macro Definition Documentation

◆ _FCALL

#define _FCALL

Definition at line 54 of file lf_convert_c.c.

◆ BUFSIZE

#define BUFSIZE   1024

Definition at line 59 of file lf_convert_c.c.

◆ CR

#define CR   (int) 0x0D

Definition at line 62 of file lf_convert_c.c.

◆ LF

#define LF   (int) 0x0A

Definition at line 63 of file lf_convert_c.c.

◆ MAXINCLVL

#define MAXINCLVL   15

Definition at line 64 of file lf_convert_c.c.

◆ MAXINCNAMELEN

#define MAXINCNAMELEN   128

Definition at line 65 of file lf_convert_c.c.

◆ TODOS

#define TODOS   0

Definition at line 55 of file lf_convert_c.c.

Function Documentation

◆ convertfile()

void convertfile ( FILE * stream,
int level,
FILE * stream_out,
char * filename,
int ncharline,
int * ierr )

Definition at line 258 of file lf_convert_c.c.

258 {
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}
return
Definition dmumps.m:15
#define MAXINCLVL
initmumps id

◆ cwd_c()

char * cwd_c ( )
static

Definition at line 204 of file lf_convert_c.c.

204 {
205 char * tmpdir;
206
207 tmpdir = (char *)calloc(2048,sizeof(char));
208 getcwd(tmpdir,2048);
209 return tmpdir;
210}

◆ do_lf_convert()

void do_lf_convert ( int fdi,
int fdo )

Definition at line 217 of file lf_convert_c.c.

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}
#define TODOS
#define LF
#define CR
int readbuf(int fid, char *buf, int nbytes)
int writebuf(int fid, char *buf, int nbytes)
#define BUFSIZE
Definition rad2rad_c.c:94

◆ fatal()

void fatal ( char * msg)
static

Definition at line 92 of file lf_convert_c.c.

93{
94 fprintf(stderr,"%s\n", msg);
95 exit(1);
96}

◆ LF_CONVERT_C_FLAT()

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 )

Definition at line 531 of file lf_convert_c.c.

532{ lf_convert_c_flat(got_input, rootname, rootlen, filename, namelen, outname, ierr, ncharline,len_path,path,len_path2,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)

◆ lf_convert_c_flat()

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 )

Definition at line 381 of file lf_convert_c.c.

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}
void convertfile(FILE *stream, int level, FILE *stream_out, char *filename, int ncharline, int *ierr)
static char * cwd_c()
void syserr()

◆ lf_convert_c_flat_()

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 )

Definition at line 534 of file lf_convert_c.c.

535{ lf_convert_c_flat(got_input, rootname, rootlen, filename, namelen, outname, ierr, ncharline,len_path,path,len_path2,path2);}

◆ lf_convert_c_flat__()

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 )

Definition at line 537 of file lf_convert_c.c.

538{ lf_convert_c_flat(got_input, rootname, rootlen, filename, namelen, outname, ierr, ncharline,len_path,path,len_path2,path2);}

◆ readbuf()

int readbuf ( int fid,
char * buf,
int nbytes )

Definition at line 102 of file lf_convert_c.c.

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}
static void fatal(char *msg)
real(dp), parameter done

◆ syserr()

void syserr ( char * msg)
static

Definition at line 79 of file lf_convert_c.c.

80{
81 fprintf(stderr,"SYSTEM ERROR>> ");
82 perror(msg);
83}

◆ syserr_fatal()

void syserr_fatal ( char * msg)
static

Definition at line 85 of file lf_convert_c.c.

86{
87 fprintf(stderr,"SYSTEM ERROR>> ");
88 perror(msg);
89 exit(1);
90}

◆ tmpenv_c()

char * tmpenv_c ( )
static

Definition at line 192 of file lf_convert_c.c.

192 {
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}

◆ writebuf()

int writebuf ( int fid,
char * buf,
int nbytes )

Definition at line 134 of file lf_convert_c.c.

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}