00001 #include "intools.h"
00002 #include "iotools.h"
00003 #include "xfile.h"
00004 #include <string.h>
00005 #include <ctype.h>
00006
00007
00008
00009
00010 static long Ngkw = 2;
00011 static const char *Genkw[2] = {"begin", "end"};
00012 static long Npkw = 21;
00013 static const char *Propkw[21] = {"ndofn", "bocon", "nload", "crsec", "lcsys", "eltype",
00014 "mater", "eload", "dloadn", "dloadel", "inicon", "comcn",
00015 "loadedge", "sscomp", "ntemp", "tfunct", "loadsurf", "elsurfload",
00016 "ntimefunc", "etimefunc", "bocsurf"};
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 long isgkw(char *s, char *&ptr)
00034 {
00035 ptr = NULL;
00036 for (long i = 0; i < Ngkw; i++)
00037 {
00038 if ((ptr = strstr(s, Genkw[i])) != NULL)
00039 return(i);
00040 }
00041 return(-1);
00042 }
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 long ispkw(char *s, char *&ptr)
00059 {
00060 for (long i = 0; i < Npkw; i++)
00061 {
00062 if ((ptr = strstr(s, Propkw[i])) != NULL)
00063 return(i);
00064 }
00065 return(-1);
00066 }
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 long iscomment(char *s, char *&ptr)
00082 {
00083 if ((ptr = strstr(s, "#")) != NULL)
00084 return(1);
00085 return(0);
00086 }
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 void delcomment(char *s)
00099 {
00100 long l = strlen(s), nonsp = -1;
00101
00102 for (long i = 0; i < l; i++)
00103 {
00104 if (! isspace(s[i]))
00105 nonsp++;
00106 if (s[i] == '#')
00107 {
00108 if (nonsp)
00109 s[i] = '\0';
00110 else
00111 s[0] = '\0';
00112 break;
00113 }
00114 }
00115 }
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 FILE *cleancommblock(FILE *in)
00129 {
00130 FILE *ret;
00131 long posb, curpos, pose, comfl;
00132 int c;
00133
00134 ret = tmpfile();
00135
00136 if (ret == NULL)
00137 return(ret);
00138 getgkwid(in, 0);
00139 getnextln(in);
00140 posb = ftell(in);
00141 getgkwid(in, 1);
00142 pose = ftell(in);
00143 pose -= strlen(Genkw[1]);
00144 fseek(in, posb, SEEK_SET);
00145 curpos = posb;
00146 comfl = 0;
00147 while (curpos < pose)
00148 {
00149 c = fgetc(in);
00150 if (c == '#')
00151 {
00152 getnextln(in);
00153 comfl = 1;
00154 }
00155 else
00156 {
00157 if (comfl)
00158 fputc('\n', ret);
00159 fputc(c, ret);
00160 comfl = 0;
00161 }
00162 curpos = ftell(in);
00163 }
00164 rewind(ret);
00165 return(ret);
00166 }
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 void geteoln(FILE *in)
00178 {
00179 int c = 0;
00180 while ((! feof(in)) && ((c = fgetc(in)) != '\n'));
00181 if (c == '\n')
00182 fseek(in, -1, SEEK_CUR);
00183 }
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 void getnextln(FILE *in)
00195 {
00196 int c = 0;
00197 while ((! feof(in)) && ((c = fgetc(in)) != '\n'));
00198 }
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209 void getnexttxt(FILE *in)
00210 {
00211 int c = 0;
00212 while (! feof(in))
00213 {
00214 c = fgetc(in);
00215 if (! isspace(c))
00216 {
00217
00218 ungetc(c, in);
00219 break;
00220 }
00221 };
00222 }
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 long getgkwid(FILE *in, long id)
00239 {
00240 char buf[1025]; char *ptr, *ptrc;
00241 long pos = ftell(in);
00242 long idkw;
00243 unsigned int cb;
00244 do
00245 {
00246 memset(buf, 0, sizeof(*buf)*1025);
00247 if (fscanf(in, "%1024s%n", buf, &cb) != 1)
00248 {
00249 fprintf(stderr, "\nError searching general keyword ""%s"" in function getgkwid()", Genkw[id]);
00250 fprintf(stderr, "\n in file input.cpp\n");
00251 return(1);
00252 }
00253 pos += cb;
00254 idkw = isgkw(buf, ptr);
00255
00256 if (iscomment(buf, ptrc))
00257 {
00258 if ((ptrc < ptr) || (idkw < 0))
00259 {
00260 pos -= cb;
00261 pos += (ptrc - buf);
00262 pos += strlen("#");
00263 pos++;
00264 fseek(in, pos, SEEK_SET);
00265 geteoln(in);
00266 pos = ftell(in);
00267 idkw = -1;
00268 }
00269 }
00270 }
00271 while (idkw != id);
00272 pos -= cb;
00273 pos += (ptr - buf);
00274 pos += strlen(Genkw[id]);
00275 pos++;
00276 fseek(in, pos, SEEK_SET);
00277 return(0);
00278 }
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296 long getgkwid(XFILE *in, long id, long &numl)
00297 {
00298 char *buf; char *ptr;
00299 long pos;
00300 long idkw;
00301 unsigned int cb;
00302
00303 buf = new char[in->give_maxlnsize()];
00304 memset(buf, 0, sizeof(*buf)*in->give_maxlnsize());
00305 numl = -1;
00306 do
00307 {
00308 in->lnfpos = pos = ftell(in->file);
00309 if ((cb = inputln(in->file, buf, in->give_maxlnsize())) == 0)
00310 {
00311 fprintf(stderr, "\nError searching property keyword in function getpkw()");
00312 fprintf(stderr, "\n in file input.cpp\n");
00313 numl = 0;
00314 return(1);
00315 }
00316 numl++;
00317 clearcomments(buf);
00318 idkw = isgkw(buf, ptr);
00319 }
00320 while (idkw != id);
00321 pos += (ptr - buf);
00322 pos += strlen(Propkw[idkw]);
00323 fseek(in->file, pos, SEEK_SET);
00324 in->col = pos - in->lnfpos+1;
00325 delete [] buf;
00326 return(0);
00327 }
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343 long getpkw(FILE *in)
00344 {
00345 char buf[1025], *ptr, *ptrc;
00346 long pos = ftell(in);
00347 unsigned int cb;
00348 long idkw;
00349 do
00350 {
00351 if (fscanf(in, "%1024s%n", buf, &cb) != 1)
00352 {
00353 fprintf(stderr, "\nError searching property keyword in function getpkw()");
00354 fprintf(stderr, "\n in file input.cpp\n");
00355 return(1);
00356 }
00357 pos += cb;
00358 idkw = ispkw(buf, ptr);
00359
00360 if (iscomment(buf, ptrc))
00361 {
00362 if ((ptrc < ptr) || (idkw < 0))
00363 {
00364 pos -= cb;
00365 pos += (ptrc - buf);
00366 pos += strlen("#");
00367 pos++;
00368 fseek(in, pos, SEEK_SET);
00369 geteoln(in);
00370 pos = ftell(in);
00371 idkw = -1;
00372 }
00373 }
00374 }
00375 while (idkw < 0);
00376 pos -= cb;
00377 pos += (ptr - buf);
00378 pos += strlen(Propkw[idkw]);
00379 pos++;
00380 fseek(in, pos, SEEK_SET);
00381 return(idkw);
00382 }
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400 long getnprop(XFILE *in, long &numl)
00401 {
00402 long ret = 0;
00403 char *buf, *ptr, *ptrg;
00404 long origpos = ftell(in->file);
00405 long idkw, idgkw;
00406 long cb;
00407
00408 buf = new char[in->give_maxlnsize()];
00409 memset(buf, 0, sizeof(*buf)*in->give_maxlnsize());
00410 numl = -1;
00411 do
00412 {
00413 if((cb = inputln(in->file, buf, in->give_maxlnsize())) == 0)
00414 {
00415 fprintf(stderr, "\nError counting property input lines in function getnprop()");
00416 fprintf(stderr, "\n in file input.cpp\n");
00417 return(-1);
00418 }
00419 numl++;
00420 clearcomments(buf);
00421 if ((idkw = ispkw(buf, ptr)) > -1)
00422 ret++;
00423 idgkw = isgkw(buf, ptrg);
00424 }
00425 while (idgkw < 0);
00426 fseek(in->file, origpos, SEEK_SET);
00427 delete [] buf;
00428 return(ret);
00429 }
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446 long getnprop(FILE *in)
00447 {
00448 long pos, ret = 0;
00449 char buf[1025], *ptr, *ptrc, *ptrg;
00450 long origpos = pos = ftell(in);
00451 long idkw, idgkw;
00452 unsigned int cb;
00453
00454 do
00455 {
00456 if (fscanf(in, "%1024s%n", buf, &cb) != 1)
00457 {
00458 fprintf(stderr, "\nError counting property input lines in function getnprop()");
00459 fprintf(stderr, "\n in file input.cpp\n");
00460 return(-1);
00461 }
00462 pos += cb;
00463 if ((idkw = ispkw(buf, ptr)) > -1)
00464 ret++;
00465 idgkw = isgkw(buf, ptrg);
00466
00467 if (iscomment(buf, ptrc))
00468 {
00469 if ((ptrc < ptr) || (idkw < 0))
00470 {
00471 pos -= cb;
00472 pos += (ptrc - buf);
00473 pos += strlen("#");
00474 pos++;
00475 fseek(in, pos, SEEK_SET);
00476 geteoln(in);
00477 pos = ftell(in);
00478 if (idkw > -1)
00479 ret--;
00480 }
00481 if ((idgkw > -1) && (ptrc < ptrg))
00482 idgkw = -1;
00483 }
00484 }
00485 while (idgkw < 0);
00486 fseek(in, origpos, SEEK_SET);
00487 return(ret);
00488 }
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506 long getlong(FILE *in, long &num)
00507 {
00508 char buf[1025], *ptrc;
00509 long pos = ftell(in);
00510 unsigned int cb, tcb;
00511 long idl;
00512 do
00513 {
00514 if (fscanf(in, "%1024s%n", buf, &cb) != 1)
00515 {
00516 fprintf(stderr, "\nError reading long number in function getlong()");
00517 fprintf(stderr, "\n in file input.cpp\n");
00518 return(1);
00519 }
00520 pos += cb;
00521 idl = sscanf(buf, "%ld%n", &num, &tcb);
00522
00523 if (iscomment(buf, ptrc))
00524 {
00525 if ((ptrc < (buf+tcb)) || (idl == 0))
00526 {
00527
00528 pos -= strlen(buf);
00529 pos += (ptrc - buf);
00530 fseek(in, pos, SEEK_SET);
00531 geteoln(in);
00532 pos = ftell(in);
00533 idl = 0;
00534 }
00535 }
00536 }
00537 while (idl == 0);
00538
00539
00540
00541 pos -= strlen(buf);
00542 pos += tcb;
00543 fseek(in, pos, SEEK_SET);
00544 return(0);
00545 }
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561 long getint(FILE *in, int &num)
00562 {
00563 char buf[1025], *ptrc;
00564 long pos = ftell(in);
00565 unsigned int cb, tcb;
00566 long idl;
00567 do
00568 {
00569 if (fscanf(in, "%1024s%n", buf, &cb) != 1)
00570 {
00571 fprintf(stderr, "\nError reading int number in function getlong()");
00572 fprintf(stderr, "\n in file input.cpp\n");
00573 return(1);
00574 }
00575 pos += cb;
00576 idl = sscanf(buf, "%d%n", &num, &tcb);
00577
00578 if (iscomment(buf, ptrc))
00579 {
00580 if ((ptrc < (buf+tcb)) || (idl == 0))
00581 {
00582 pos -= strlen(buf);
00583 pos += (ptrc - buf);
00584 fseek(in, pos, SEEK_SET);
00585 geteoln(in);
00586 pos = ftell(in);
00587 idl = 0;
00588 }
00589 }
00590 }
00591 while (idl == 0);
00592 pos -= strlen(buf);
00593 pos += tcb;
00594 fseek(in, pos, SEEK_SET);
00595 return(0);
00596 }
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612 long getdouble(FILE *in, double &num)
00613 {
00614 char buf[1025], *ptrc;
00615 long pos;
00616 unsigned int cb, tcb;
00617 long idl;
00618
00619 do
00620 {
00621 pos = ftell(in);
00622 if (fscanf(in, "%1024s%n", buf, &cb) != 1)
00623 {
00624 fprintf(stderr, "\nError reading double number in function getlong()");
00625 fprintf(stderr, "\n in file input.cpp\n");
00626 return(1);
00627 }
00628 pos += cb;
00629 idl = sscanf(buf, "%lf%n", &num, &tcb);
00630
00631 if (iscomment(buf, ptrc))
00632 {
00633 if ((ptrc < (buf+tcb)) || (idl == 0))
00634 {
00635 pos -= strlen(buf);
00636 pos += (ptrc - buf);
00637 fseek(in, pos, SEEK_SET);
00638 geteoln(in);
00639 pos = ftell(in);
00640 idl = 0;
00641 }
00642 }
00643 }
00644 while (idl == 0);
00645 pos -= strlen(buf);
00646 pos += tcb;
00647 fseek(in, pos, SEEK_SET);
00648 return(0);
00649 }
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666 long getstring(FILE *in, char *str, long maxlen)
00667 {
00668 long i = 0;
00669 int c;
00670
00671
00672 while (1)
00673 {
00674 c = fgetc(in);
00675 if (feof(in))
00676 return(1);
00677 if (! isspace(c))
00678 {
00679 ungetc(c, in);
00680 break;
00681 }
00682 }
00683
00684
00685 c = 0;
00686 while (i < maxlen)
00687 {
00688 if(feof(in))
00689 break;
00690 c = fgetc(in);
00691 str[i] = char(c);
00692 if (c == '\n')
00693 {
00694 ungetc(c, in);
00695 break;
00696 }
00697 if (c == '#')
00698 {
00699 if (i != 0)
00700
00701 {
00702 geteoln(in);
00703 break;
00704 }
00705 else
00706 {
00707 getnextln(in);
00708 continue;
00709 }
00710 }
00711 i++;
00712 }
00713 str[i] = '\0';
00714
00715 if ((i == maxlen) && (c != '\n'))
00716 while (! feof(in) && (fgetc(in) != '\n'));
00717 return(0);
00718 }
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734 long getstring2(FILE *in, char *str, long maxlen)
00735 {
00736 long i = 0;
00737 int c;
00738
00739
00740 c = 0;
00741 while (i < maxlen)
00742 {
00743 if(feof(in))
00744 break;
00745 c = fgetc(in);
00746 str[i] = char(c);
00747 if (c == '\n')
00748 {
00749
00750 break;
00751 }
00752 if (c == '#')
00753 {
00754 if (i != 0)
00755
00756 {
00757 geteoln(in);
00758 break;
00759 }
00760 else
00761 {
00762 getnextln(in);
00763 continue;
00764 }
00765 }
00766 i++;
00767 }
00768 str[i] = '\0';
00769
00770 if ((i == maxlen) && (c != '\n'))
00771 while (! feof(in) && (fgetc(in) != '\n'));
00772 return(0);
00773 }
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786 long inputln(FILE* in, char* anystr, long maxlen)
00787 {
00788 long i = 0;
00789 char c;
00790
00791 c = '\0';
00792 anystr[0] = '\0';
00793 do
00794 {
00795 if (i >= maxlen)
00796 break;
00797 if (feof(in))
00798 break;
00799 c = char(fgetc(in));
00800 if ((c == '\n') && strlen(anystr) == 0)
00801 continue;
00802 if ((c == '\n') && i != 0)
00803 break;
00804 anystr[i] = c;
00805 i++;
00806 } while (1);
00807 anystr[i] = '\0';
00808 if ((i == maxlen) && (c != '\n'))
00809 while (! feof(in) && (fgetc(in) != '\n')) ;
00810
00811 return (i);
00812 }