-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathL3Input.cpp
More file actions
2639 lines (2471 loc) · 84 KB
/
L3Input.cpp
File metadata and controls
2639 lines (2471 loc) · 84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef TEST_MEMORY_USAGE
long LINETYPES[6];
#endif
/*
* L3Input.cpp, part of the L3 project for handling LDraw *.dat files
* Copyright (C) 1997-1999 Lars C. Hassing (lch@ccieurope.com)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*****************************************************************************
Please do not edit this file. In stead contact me (lch@ccieurope.com)
to get your changes integrated and you will receive an up-to-date version.
******************************************************************************/
/* L3Input.cpp Various input functions for L3 program */
/*
970918 lch First version, both for TurboC 2.0 and Visual C++ 5.0
980926 lch Version released for ldlite
981004 lch File search also in directory of model
981005 lch Support for MPD files
981111 lch Skip collinear lines if color is not 16
981218 lch Better message for moved parts: tell who referenced it
981226 lch Added BBoxES: BBox exclusive studs
990113 lch Skip header before first "0 FILE xx.dat" of MPD files
990114 lch Also look for files with .MPD extension
990212 lch Added support for TRANSLATE, ROTATE, SCALE, TRANSFORM meta commands
990221 lch Treat any header before first valid command as comments
990324 lch Default Part Color added
990408 lch L3P will now look in ldraw.ini if LDRAWDIR is not defined
990416 lch FilteType DAT0, don't complain about comments before "0 FILE"
990419 lch Prepare for L3P32, changing #ifdef __TURBOC__ to #ifdef L3P
990424 lch #if (STUDS) added
990603 lch ModelTitle and ModelAuthor added
990803 lch Use getenv(windir) for ldraw.ini, and remove trailing backslash
991020 lch Crash in ErrorInInput if Filename+InputStr exceeded 200 characters
991130 lch Added LineNo to L3LineS if not TurboC
000317 lch Recursion check added
*/
/*
12345678901234567890123456789012345678901234567890123456789012345678901234567890
problem with bbox for mpd files...
*/
#ifdef USE_OPENGL
#include "math.h"
extern "C" {
#endif
#ifdef USE_OPENGL
// Gotta prepare for case sensitive file systems.
#include "StdAfx.h"
#else
#include "stdafx.h"
#endif
#ifndef L3P
#ifndef USE_OPENGL
#include "resource.h" /* main symbols */
#include "LogDialog.h"
#endif
#endif
#include "stdio.h"
#include "stdlib.h"
#include "L3Def.h"
#include "math.h"
#include "string.h"
#ifdef USE_OPENGL
#include "platform.h"
#include "ldsearchdir.h"
extern void platform_fixcase(char *);
extern char pathname[256];
extern char primitivepath[256];
extern char partspath[256];
extern char modelspath[256];
extern char datfilepath[256];
extern int use_uppercase;
#endif
#ifdef LOGFP
static int nLevel = 0;
#endif
#ifndef L3P
#ifndef USE_OPENGL
CLogDialog LogDia("Warnings and errors during input");
struct L3StatS L3Stat;
#endif
#endif
int DoCheck = 0;
#ifdef L3P
int ReadLineTypeMask = BIT(0) + BIT(1) + BIT(3) + BIT(4);
#else
int ReadLineTypeMask = BIT(0) + BIT(1) + BIT(2) + BIT(3)
+ BIT(4) + BIT(5);
#endif
#ifndef L3P
char ModelTitle[400]; /* Title of model file */
char ModelAuthor[400]; /* Author of model file */
#endif
char LDrawDir[_MAX_PATH]; /* Directory of P, PARTS, MODELS */
char ModelDir[_MAX_PATH]; /* Directory of model */
struct L3ColorS Colors[MAX_COLORS];
int nColors;
struct L3LightS *Lights;
int nLights;
int nLightsAlloc;
struct L3PartS Parts[MAX_PARTS];
int nParts; /* Number of parts in Parts[] */
int WarningLevel;
float DetThreshold = 0.0;
float DistThreshold = 0.0;
int ShowAllDists = 0;
int LightDotDat; /* 1: use light.dat, 0: ignore */
/* TurboC has a limited stack size (default _stklen=4096)
Although I set it to e.g. 16000, it is best to keep automatic variables at a minimum.
Therefore these variables here.
Be careful that they are used in limited code to avoid side-effects.
*/
static struct InputInfo
{
char InputStr[1000];
char *Filename;
int LineNo;
} IInfo;
static char Searchdirpath[_MAX_PATH];
static char Path[_MAX_PATH];
static char SubPartDatName[_MAX_PATH];
static char ErrStr[400];
char *p = (char *)"\\P\\";
char *parts = (char *)"\\Parts\\";
char *models = (char *)"\\Models\\";
char *unoff_p = (char *)"\\Unofficial\\P\\";
char *unoff_parts = (char *)"\\Unofficial\\Parts\\";
char *unoff_lsynth = (char *)"\\Unofficial\\Lsynth\\";
#ifdef USE_OPENGL
char *Dirs[] = {p,
parts,
models,
unoff_p,
unoff_parts,
unoff_lsynth};
char *SDirs[] = {p,
parts,
models};
#else
char *Dirs[] = {p,
parts,
models};
#endif
/***************************************************************/
// version 1.2.7 added by T.Sandy to support reading search directories
/* Naming refers to Windows platform */
#if defined(_WIN32) || defined(__TURBOC__)
// Disable warning message C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead.
#pragma warning( disable : 4996 )
#define BACKSLASH_CHAR '\\'
#define BACKSLASH_STRING "\\"
#define SLASH_CHAR '/'
#else
#define BACKSLASH_CHAR '/'
#define BACKSLASH_STRING "/"
#define SLASH_CHAR '\\'
#endif
static void L3FixSlashes(register char *Path);
static void FreeLDSearchDir(struct LDSearchDirS * LDSearchDir);
static void FreeSearchDirs(struct LDSearchDirS * LDSearchDir);
static void FreeSymbolicDirs(struct LDSearchDirS * LDSearchDir);
static int SplitLDrawSearch(const char *LDrawSearchString,
int *nDirs, char ***Dirs);
struct LDSearchDirS *smLDSearchDirs;
/*
Get the search directories
*/
void GetLDrawSearchDirs(int *ErrorCode)
{
if (smLDSearchDirs)
{
FreeLDSearchDir(smLDSearchDirs);
}
smLDSearchDirs = LDSearchDirGet(ErrorCode);
if (smLDSearchDirs)
{
LDSearchDirComputeRealDirs(smLDSearchDirs, false /*AddTrailingSlash*/);
}
else
{
FreeLDSearchDir(smLDSearchDirs);
printf("FreeLDSearchDir %s\n", ErrorCode);
}
}
/*
Initialize and read data from Ldraw Search Env variable.
If ErrorCode is not NULL it will return a code telling why
LDSearchDirGet returned NULL.
If all is OK then a pointer to struct LDSearchDirS is returned.
You should then call LDSearchDirComputeRealDirs to obtain the search dirs.
Remember to free the struct by calling LDSearchDirFree.
*/
struct LDSearchDirS *LDSearchDirGet(int *ErrorCode)
{
struct LDSearchDirS *LDSearchDir;
const char *e;
int i;
struct LDSearchDirPrivateDataS *pd; //LDrawSearch Symbolic data structure
if (ErrorCode)
*ErrorCode = LDSEARCHDIR_ERROR_OUT_OF_MEMORY; /* Default */
LDSearchDir = (struct LDSearchDirS *) calloc(1, sizeof(struct LDSearchDirS));
if (!LDSearchDir)
return NULL;
LDSearchDir->PrivateData = (struct LDSearchDirPrivateDataS *)
calloc(1, sizeof(struct LDSearchDirPrivateDataS));
if (!LDSearchDir->PrivateData)
{
free(LDSearchDir);
return NULL;
}
/* Now initialize the struct by reading env var*/
/* LDrawSearch, read symbolic dirs */
pd = LDSearchDir->PrivateData;
/* First try environment variable */
e = getenv("LDSEARCHDIRS");
if (e)
{
i = SplitLDrawSearch(e, &pd->nSymbolicSearchDirs, &pd->SymbolicSearchDirs);
if (!i)
return NULL; /* No more memory, just give up */
}
else
{
printf("LDSEARCHDIRS ErrorCode %s\n", ErrorCode);
if (ErrorCode)
*ErrorCode = LDSEARCHDIR_ERROR_NO_SEARCH_DIRS_DETECTED;
free(LDSearchDir);
return NULL;
}
if (ErrorCode)
*ErrorCode = 0;
return LDSearchDir;
}
/*
Compute Real Dirs from items in
the Symbolic Dirs read from the env vars.
If AddTrailingSlash is true then the search dirs will have a slash/backslash appended.
Returns 1 if OK, 0 on error
*/
int LDSearchDirComputeRealDirs(struct LDSearchDirS * LDSearchDir,
int AddTrailingSlash)
{
struct LDSearchDirPrivateDataS *pd;
char *Dir;
const char *s;
int DirLen;
int i;
struct LDrawSearchDirS SearchDir;
if (!LDSearchDir)
return 0;
pd = LDSearchDir->PrivateData;
if (!pd->nSymbolicSearchDirs)
return 0;
FreeSearchDirs(LDSearchDir);
/* We may allocate too much here because some dirs may be skipped... */
LDSearchDir->SearchDirs =
(struct LDrawSearchDirS *) calloc(pd->nSymbolicSearchDirs,
sizeof(struct LDrawSearchDirS));
if (!LDSearchDir->SearchDirs)
return 0;
/* Process symbolic search directories from private data*/
for (i = 0; i < pd->nSymbolicSearchDirs; i++)
{
s = pd->SymbolicSearchDirs[i];
DirLen = strlen(s);
Dir = (char *) malloc(DirLen + 1 + 1); /* See AddTrailingSlash */
if (!Dir)
return 0;
memcpy(Dir, s, DirLen);
Dir[DirLen] = '\0';
L3FixSlashes(Dir);
SearchDir.Dir = Dir;
if (SearchDir.Dir[0] && AddTrailingSlash)
strcat(SearchDir.Dir, BACKSLASH_STRING); /* Dir has room for this */
LDSearchDir->SearchDirs[LDSearchDir->nSearchDirs++] = SearchDir;
}
return 1;
}
/* Returns 1 if OK, 0 on error */
static int SplitLDrawSearch(const char *LDrawSearchString, int *nDirs, char ***Dirs)
{
const char *s;
const char *t;
char *Dir;
int n;
int Len;
/* Count number of dir separators '|' */
for (n = 1, s = strchr(LDrawSearchString, '|'); s; s = strchr(s + 1, '|'))
++n;
*Dirs = (char **) malloc(n * sizeof(char *));
if (!*Dirs)
return 0;
for (n = 0, s = LDrawSearchString; *s;)
{
t = s;
while (*t && *t != '|')
++t;
Len = t - s;
Dir = (char *) malloc(Len + 1);
if (!Dir)
return 0;
memcpy(Dir, s, Len);
Dir[Len] = '\0';
(*Dirs)[n] = Dir;
if (!(*Dirs)[n++])
return 0;
s = *t ? t + 1 : t;
}
*nDirs = n;
return 1;
}
/* Modifies the string: OS-correct slashes or backslashes */
static void L3FixSlashes(register char *Path)
{
for (; *Path; Path++)
if (*Path == SLASH_CHAR)
*Path = BACKSLASH_CHAR;
}
static void FreeSearchDirs(struct LDSearchDirS * LDSearchDir)
{
int i;
if (LDSearchDir->SearchDirs)
{
for (i = 0; i < LDSearchDir->nSearchDirs; i++)
{
free(LDSearchDir->SearchDirs[i].Dir);
}
free(LDSearchDir->SearchDirs);
}
LDSearchDir->nSearchDirs = 0;
LDSearchDir->SearchDirs = NULL;
}
/* Free the SymbolicSearchDirs data */
static void FreeSymbolicDirs(struct LDSearchDirS * LDSearchDir)
{
int i;
struct LDSearchDirPrivateDataS *pd;
pd = LDSearchDir->PrivateData;
if (pd->nSymbolicSearchDirs)
{
for (i = 0; i < pd->nSymbolicSearchDirs; i++)
free(pd->SymbolicSearchDirs[i]);
free(pd->SymbolicSearchDirs);
}
pd->nSymbolicSearchDirs = 0;
pd->SymbolicSearchDirs = NULL;
}
/* Free the LDSearchDir data */
void FreeLDSearchDir(struct LDSearchDirS * LDSearchDir)
{
if (!LDSearchDir)
return;
FreeSearchDirs(LDSearchDir);
/* Free private data */
FreeSymbolicDirs(LDSearchDir);
free(LDSearchDir->PrivateData);
free(LDSearchDir);
}
static int FileIsFromP;
static int FileIsFromPARTS;
static FILE *OpenDatFile2(char *DatName, char *Extension)
{
FILE *fp;
register int i;
strcpy(Path, DatName);
strcat(Path, Extension);
FileIsFromP = 0;
FileIsFromPARTS = 0;
/* First, check if at the root of the current directory */
fp = fopen(Path, "rt");
if (!fp)
{
/* If not in current directory then look in P, PARTS and MODELS */
for (i = 0; i < sizeof(Dirs) / sizeof(char *); i++)
{
#ifdef USE_OPENGL
if (stricmp(Dirs[i], "\\P\\") == 0)
concat_path(primitivepath, DatName, Path);
if (stricmp(Dirs[i], "\\Parts\\") == 0)
concat_path(partspath, DatName, Path);
//printf("000 Parts:\n -partspath(%s)\n -DatName(%2s)\n -Path(%3s)\n",partspath,DatName,Path);
if (stricmp(Dirs[i], "\\Models\\") == 0)
concat_path(modelspath, DatName, Path);
if (stricmp(Dirs[i], "\\Unofficial\\Parts\\") == 0)
{
char PrePath[_MAX_PATH];
concat_path(LDrawDir, Dirs[i], PrePath);
concat_path(PrePath, DatName, Path);
//printf("111 LDrawDir Unofficial Parts:\n -LDrawDir(%s)\n -Dirs[i](%2s)\n -PrePath(%3s)\n -DatName(%4s)\n -Path(%5s)\n -Extension(%5s)\n",LDrawDir,Dirs[i],PrePath,DatName,Path,Extension);
}
if (stricmp(Dirs[i], "\\Unofficial\\P\\") == 0)
{
char PrePath[_MAX_PATH];
concat_path(LDrawDir, Dirs[i], PrePath);
concat_path(PrePath, DatName, Path);
}
if (stricmp(Dirs[i], "\\Unofficial\\LSynth\\") == 0)
{
char PrePath[_MAX_PATH];
concat_path(LDrawDir, Dirs[i], PrePath);
concat_path(PrePath, DatName, Path);
}
#else
strcpy(Path, LDrawDir);
strcat(Path, Dirs[i]);
strcat(Path, DatName);
#endif
strcat(Path, Extension);
FileIsFromP = (i == 0);
FileIsFromPARTS = i & 1;
fp = fopen(Path, "rt");
if (fp)
break;
}
/* If not found, try search directories */
if (!fp && smLDSearchDirs && smLDSearchDirs->nSearchDirs > 0 && !(stricmp(DatName, "ldconfig.ldr") == 0))
{
int i;
for (i = 0; i < smLDSearchDirs->nSearchDirs && !fp; i++) {
LDrawSearchDirS *searchDir = &smLDSearchDirs->SearchDirs[i];
strcpy(Searchdirpath, searchDir->Dir);
/* First, check if at the root of the current directory */
concat_path(Searchdirpath, DatName, Path);
fp = fopen(Path, "rt");
if (!fp && SDirs[0])
{
/* If not in current directory then look in P, PARTS and MODELS */
int i;
for (i = 0; i < sizeof(SDirs) / sizeof(char *); i++)
{
#ifdef USE_OPENGL
if (stricmp(SDirs[i], "\\Parts\\") == 0)
{
char PrePath[_MAX_PATH];
concat_path(Searchdirpath, SDirs[i], PrePath);
concat_path(PrePath, DatName, Path);
//printf("333 Searchdirpath Parts:\n -Searchdirpath(%s)\n -SDirs[i](%2s)\n -PrePath(%3s)\n -DatName(%4s)\n -Path(%5s)\n -Extension(%6s)\n",Searchdirpath,SDirs[i],PrePath,DatName,Path,Extension);
}
if (stricmp(SDirs[i], "\\P\\") == 0)
{
char PrePath[_MAX_PATH];
concat_path(Searchdirpath, SDirs[i], PrePath);
concat_path(PrePath, DatName, Path);
}
if (stricmp(SDirs[i], "\\Models\\") == 0)
{
char PrePath[_MAX_PATH];
concat_path(Searchdirpath, SDirs[i], PrePath);
concat_path(PrePath, DatName, Path);
}
#else
strcpy(Path, Searchdirpath);
strcat(Path, Dirs[i]); /* Use Dirs[] because elements are same as SDirs */
strcat(Path, DatName);
#endif
strcat(Path, Extension);
FileIsFromP = (i == 0);
FileIsFromPARTS = i & 1;
fp = fopen(Path, "rt");
if (fp)
break;
}
} // End !fp
}
}
/* If still not found, try directory of the model itself */
if (!fp && ModelDir[0])
{
#ifdef USE_OPENGL
concat_path(datfilepath, DatName, Path);
#else
strcpy(Path, ModelDir);
strcat(Path, DatName);
#endif
strcat(Path, Extension);
fp = fopen(Path, "rt");
}
/* If still not found, try LDRAWDIR itself for ldconfig.ldr */
if (!fp && (stricmp(DatName, "ldconfig.ldr") == 0))
{
concat_path(LDrawDir, DatName, Path);
strcat(Path, Extension);
fp = fopen(Path, "rt");
}
}
return (fp);
}
FILE *OpenDatFile(char *DatName)
{
FILE *fp;
char *empty = (char *)"";
fp = OpenDatFile2(DatName, empty);
#ifdef USE_OPENGL
if (use_uppercase)
{
char *upperDAT = (char *)".DAT";
char *upperMPD = (char *)".MPD";
if (!fp)
fp = OpenDatFile2(DatName, upperDAT);
if (!fp)
fp = OpenDatFile2(DatName, upperMPD);
}
else
{
char *lowerdat = (char *)".dat";
char *lowermpd = (char *)".mpd";
if (!fp)
fp = OpenDatFile2(DatName, lowerdat);
if (!fp)
fp = OpenDatFile2(DatName, lowermpd);
}
#else
char *lowerdat = (char *)".dat";
char *lowermpd = (char *)".mpd";
if (!fp)
fp = OpenDatFile2(DatName, lowerdat);
if (!fp)
fp = OpenDatFile2(DatName, lowermpd);
#endif
return (fp);
}
char *GetOpenDatFilePath(void)
{
return (Path);
}
/***************************************************************/
struct L3PovS L3Pov = {
{ /* Camera position */
30.0, /* Latitude */
45.0, /* Longitude */
0.0, /* Radius */
0.0
},
{0.0, 0.0, 0.0, 0.0}, /* BackgroundColor */
7, /* DefaultPartColorNumber */
{0.0, 0.0, 0.0, 0.0}, /* DefaultPartColor */
0, /* CameraAngle, 0=default to 67.38 */
0.5, /* SeamWidth */
0, /* FloorY */
0, /* FloorType */
2, /* Quality */
0, /* Debug */
1, /* Globe format for camera
position */
0, /* FloorYspecified */
1, /* UsePovParts */
0, /* UseDefaultLights */
0 /* Bumps */
};
static void PromptUser(char *Str)
{
#ifdef L3P
printf("%s\n", Str);
#else
#ifdef USE_OPENGL
printf("%s\n", Str);
#else
AfxMessageBox(Str, MB_OK | MB_APPLMODAL | MB_ICONERROR, 0);
#endif
#endif
}
/* Severity: 0=error, 1=warning, 2=det, 3=pedantic */
#define II_WARNING 1
#define II_SKIPPING 2
static void ErrorInInput(int Severity, int UseII, char *Str)
{
char TmpStr[2000]; /* Must be at least size of
IInfo.InputStr+Filename */
if (Severity > WarningLevel)
return;
if (UseII)
{
sprintf(TmpStr, "%s \"%s\" Line %d: %s: %s",
(UseII == II_WARNING ? "WARNING" : "SKIPPING"),
IInfo.Filename, IInfo.LineNo, Str, IInfo.InputStr);
Str = TmpStr;
}
#ifdef L3P
printf("%s\n", Str);
#else
#ifdef USE_OPENGL
printf("%s\n", Str);
#else
extern void Cprintf(char *Format,...);
/* Cprintf("%s\n",Str); */
/* AfxMessageBox(Str, MB_OK|MB_APPLMODAL|MB_ICONERROR,0); */
LogDia.m_LogStr += Str;
LogDia.m_LogStr += "\r\n";
#endif
#endif
}
/* Strdup, not strdup, to be able to use farmalloc in TurboC */
char *Strdup(const char *Str)
{
char *Copy;
Copy = (char *) malloc(strlen(Str) + 1); /* Maybe farmalloc */
if (Copy)
strcpy(Copy, Str);
return (Copy);
}
#ifndef USE_OPENGL
static
#endif
void FreePart(struct L3PartS * PartPtr);
static void FreeLines(struct L3PartS * PartPtr)
{
register struct L3LineS *LinePtr;
struct L3LineS *NextLine;
LinePtr = PartPtr->FirstLine;
PartPtr->FirstLine = NULL;
while (LinePtr)
{
if (LinePtr->Comment && LinePtr->Comment != (char *) LinePtr->v)
free(LinePtr->Comment);
if (LinePtr->LineType == 1 && LinePtr->PartPtr->Internal)
FreePart(LinePtr->PartPtr);
NextLine = LinePtr->NextLine;
free(LinePtr);
LinePtr = NextLine;
}
}
#ifndef USE_OPENGL
static
#endif
void FreePart(struct L3PartS * PartPtr)
{
free(PartPtr->DatName);
FreeLines(PartPtr);
if (PartPtr->Internal)
free(PartPtr);
else
memset(PartPtr, 0, sizeof(struct L3PartS)); /* Clear all flags */
}
#ifndef L3P
void FreeParts(void)
{
register int i;
for (i = 0; i < nParts; i++)
FreePart(&Parts[i]);
nParts = 0;
}
#endif
struct L3LightS *AddLight(void)
{
if (nLights == nLightsAlloc)
{
nLightsAlloc += 10;
Lights = (struct L3LightS *)
realloc(Lights, nLightsAlloc * sizeof(struct L3LightS));
if (!Lights)
{
char *outOfMemory = (char *)"Out of memory for more lights";
PromptUser(outOfMemory);
/* If this small malloc fails, we might as well exit! */
exit(1);
}
}
return (&Lights[nLights++]);
}
#ifndef L3P
void FreeLights(void)
{
if (nLights)
{
free(Lights);
nLights = nLightsAlloc = 0;
}
}
#endif
const static char *StudPrimitives[] = {
"stud5.dat",
"stu2.dat",
"stu22.dat",
"stu22a.dat",
"stu23.dat",
"stu23a.dat",
"stu24.dat",
"stu24a.dat",
"stud2.dat",
"stud2a.dat",
"stud3a.dat",
"stud4a.dat",
"studline.dat",
"stu2p01.dat",
"studp01.dat",
"studel.dat",
"stud.dat",
"stud3.dat",
"stud4.dat",
};
/* If this code works, it was written by Lars C. Hassing. */
/* If not, I don't know who wrote it. */
/* Like fgets, except that 1) any line ending is accepted (\n (unix),
\r\n (DOS/Windows), \r (Mac (OS9)) and 2) Str is ALWAYS zero terminated
(even if no line ending was found) */
char *L3fgets(char *Str, int n, FILE *fp)
{
register int c;
int nextc;
register char *s = Str;
while (--n > 0)
{
if ((c = getc(fp)) == EOF)
break;
if (c == '\032')
continue; /* Skip CTRL+Z */
if (c == '\r' || c == '\n')
{
*s++ = '\n';
/* We got CR or LF, eat next character if LF or CR respectively */
if ((nextc = getc(fp)) == EOF)
break;
if (nextc == c || (nextc != '\r' && nextc != '\n'))
ungetc(nextc, fp); /* CR-CR or LF-LF or ordinary character */
break;
}
*s++ = c;
}
*s = 0;
/* if (ferror(fp)) return NULL; if (s == Str) return NULL; */
if (s == Str)
return NULL;
return Str;
}
#ifndef USE_OPENGL
static
#else
void StripQuotes(char *s, char *SubPartDatName)
{
char *sub;
if (SubPartDatName[0] == '\"')
{
if (s = strchr(s, '\"'))
{
strcpy(SubPartDatName, s+1); /* Strip leading quotes */
if (s = strchr(SubPartDatName, '\"'))
*s = 0; /* Strip trailing quotes */
}
}
// NOTE: This is kinda lame.
else if ((sub = strstr(s, SubPartDatName)) == strstr(s, SubPartDatName))
{
strcpy(SubPartDatName, sub);
}
}
#endif
/* Modifies the string: lower case and OS-correct slashes */
void FixDatName(register char *DatName)
{
register int i;
#ifdef USE_OPENGL
platform_fixcase(SubPartDatName); // Should NOT do this for model files.
localize_path(SubPartDatName);
#else
_strlwr(DatName);
for (i = strlen(DatName); --i >= 0;)
if (DatName[i] == '/')
DatName[i] = '\\';
#endif
}
static void TrimRight(char *Str)
{
register char *s;
/* 971114/lch. Win95: If a file ends with a line not terminated by CR/LF
fgets will not append a newline. So Line[Len-1] may not be a newline but
a valid character. */
/* TurboC huge pointers for pointer comparison? */
s = Str + strlen(Str) - 1;
while (s >= Str && (*s == '\n' || *s == '\r' || *s == '\t' || *s == ' '))
*s-- = '\0'; /* Clear newline and trailing tabs
and spaces */
}
void DeleteTrailingBackslash(char *Str)
{
register char *s;
s = Str + strlen(Str) - 1;
while (s >= Str && *s == '\\')
*s-- = '\0';
}
static char *FirstNonBlank(char *s)
{
while (*s == '\t' || *s == ' ')
s++;
return (s);
}
#ifndef USE_OPENGL
static
#endif
struct L3PartS *FindPart(int Internal, char *DatName)
{
register int i;
struct L3PartS *PartPtr;
if (strlen(DatName) >= _MAX_PATH)
{
sprintf(ErrStr, "Name too long '%s'.", DatName);
PromptUser(ErrStr);
return (NULL);
}
if (Internal)
{
PartPtr = (struct L3PartS *) malloc(sizeof(struct L3PartInternalS));
if (!PartPtr)
{
char *outOfMemory = (char *)"Out of memory for new internal part";
PromptUser(outOfMemory);
return (NULL);
}
}
else
{
/* First check if we have already registered it. */
for (i = 0; i < nParts; i++)
{
if (strcmp(Parts[i].DatName, DatName) == 0)
return (&Parts[i]);
}
/* No, allocate new part. */
if (nParts >= MAX_PARTS)
{
sprintf(ErrStr, "Sorry, max %d parts.", MAX_PARTS);
PromptUser(ErrStr);
return (NULL);
}
PartPtr = &Parts[nParts++];
}
memset(PartPtr, 0, sizeof(struct L3PartS)); /* Clear all flags */
PartPtr->Internal = Internal;
PartPtr->DatName = Strdup(DatName);
if (!PartPtr->DatName)
{
char *outOfMemory = (char *)"Out of memory for new part name";
PromptUser(outOfMemory);
free(PartPtr);
return (NULL);
}
for (i = sizeof(StudPrimitives) / sizeof(StudPrimitives[0]); --i >= 0;)
{
#ifdef USE_OPENGL
if (stricmp(DatName, StudPrimitives[i]) == 0)
#else
if (strcmp(DatName, StudPrimitives[i]) == 0)
#endif
{
PartPtr->IsStud = 1;
break;
}
}
return (PartPtr);
} /* FindPart */
/* Return 0 if OK */
int SaveLine(struct L3LineS *** LinePtrPtrPtr,
struct L3LineS * Data, char *Comment)
{
register struct L3LineS *LinePtr;
int Len;
LinePtr = (struct L3LineS *) malloc(sizeof(struct L3LineS));
if (!LinePtr)
return (1);
*LinePtr = *Data; /* Copy data */
if (LinePtr->LineType == 0)
{
/* Save comment */
Len = strlen(Comment) + 1;
if (Len > sizeof(LinePtr->v))
{
LinePtr->Comment = Strdup(Comment);
if (!LinePtr->Comment)
{
free(LinePtr);
return (1);
}
}
else
{
/* Reuse the 64 bytes of float v[4][4] */
LinePtr->Comment = (char *) LinePtr->v;
strcpy(LinePtr->Comment, Comment);
}
}
**LinePtrPtrPtr = LinePtr;
*LinePtrPtrPtr = &(LinePtr->NextLine);
return (0);
}
/*
// Bounding boxes of linetype 1,2,3,4,5 D:\Lego\Ldraw\LDRAW\P 19980818/LCH
// - 1-4ccyli.dat: 2 <0,-1,0> <3.5,1,3.5>
// - 1-4cyli.dat : 1 <0,0,0> <1,1,1>
// - 1-4cyls.dat : 1 <0,0,0> <1,1,1>
// + 1-4disc.dat : 0 <0,0,0> <1,0,1>
// + 1-4edge.dat : 0 <0,0,0> <1,0,1>
// + 1-4ndis.dat : 0 <0,0,0> <1,0,1>
// - 1-8cyli.dat : 1 <0.7071,0,0> <1,1,0.7071>
// + 1-8disc.dat : 0 <0,0,0> <1,0,0.7>
// + 1-8edge.dat : 0 <0.7071,0,0> <1,0,0.7071>
// + 1-8ndis.dat : 0 <0.7071,0,0> <1,0,1>
// - 1-8sphe.dat : 1 <0,0,0> <1,1,1>
// - 2-4cyli.dat : 1 <-1,0,0> <1,1,1>
// - 2-4cyls.dat : 2 <-1,0,0> <1,2,1>
// + 2-4disc.dat : 0 <-1,0,0> <1,0,1>