b311480e |
1 | /* |
973c2be1 |
2 | Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e |
3 | |
973c2be1 |
4 | This file is part of Open CASCADE Technology software library. |
b311480e |
5 | |
d5f74e42 |
6 | This library is free software; you can redistribute it and/or modify it under |
7 | the terms of the GNU Lesser General Public License version 2.1 as published |
973c2be1 |
8 | by the Free Software Foundation, with special exception defined in the file |
9 | OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
10 | distribution for complete text of the license and disclaimer of any warranty. |
b311480e |
11 | |
973c2be1 |
12 | Alternatively, this file may be used under the terms of Open CASCADE |
13 | commercial license or contractual agreement. |
b311480e |
14 | */ |
15 | |
7fd59977 |
16 | #include "igesread.h" |
17 | #include <string.h> |
18 | /* Routine de base de lecture d'un fichier IGES |
19 | |
20 | Cette routine lit une ligne, sauf si le statut "relire sur place" est mis |
21 | (utilise pour changement de section) : il est reannule ensuite |
22 | |
23 | Cette routine retourne : |
24 | - statut (retour fonction) : no de section : S,G,D,P,T (car 73) ou |
25 | 0 (EOF) ou -1 (tacher de sauter) ou -2 (car. 73 faux) |
26 | - un numero de ligne dans la section (car. 74 a 80) |
27 | - la ligne tronquee a 72 caracteres (0 binaire dans le 73ieme) |
28 | Il faut lui fournir (buffer) une ligne reservee a 81 caracteres |
29 | |
30 | Cas d erreur : ligne fausse des le debut -> abandon. Sinon tacher d enjamber |
31 | */ |
32 | |
33 | static int iges_fautrelire = 0; |
34 | int iges_lire (FILE* lefic, int *numsec, char ligne[100], int modefnes) |
35 | /*int iges_lire (lefic,numsec,ligne,modefnes)*/ |
36 | /*FILE* lefic; int *numsec; char ligne[100]; int modefnes;*/ |
37 | { |
38 | int i,result; char typesec; |
39 | /* int length;*/ |
22835705 |
40 | if (iges_fautrelire == 0) |
41 | { |
42 | if (*numsec == 0) |
43 | ligne[72] = ligne[79] = ' '; |
44 | |
7fd59977 |
45 | ligne[0] = '\0'; |
22835705 |
46 | if(modefnes) |
47 | fgets(ligne,99,lefic); /*for kept compatibility with fnes*/ |
48 | else |
49 | { |
7fd59977 |
50 | /* PTV: 21.03.2002 it is neccessary for files that have only `\r` but no `\n` |
51 | examle file is 919-001-T02-04-CP-VL.iges */ |
22835705 |
52 | while ( fgets ( ligne, 2, lefic ) && ( ligne[0] == '\r' || ligne[0] == '\n' ) ) |
53 | { |
54 | } |
55 | |
7fd59977 |
56 | fgets(&ligne[1],80,lefic); |
7fd59977 |
57 | } |
22835705 |
58 | |
59 | if (*numsec == 0 && ligne[72] != 'S' && ligne[79] == ' ') |
60 | {/* ON A DU FNES : Sauter la 1re ligne */ |
61 | ligne[0] = '\0'; |
62 | |
63 | if(modefnes) |
64 | fgets(ligne,99,lefic);/*for kept compatibility with fnes*/ |
65 | else |
66 | { |
67 | while ( fgets ( ligne, 2, lefic ) && ( ligne[0] == '\r' || ligne[0] == '\n' ) ) |
68 | { |
69 | } |
a849d42d |
70 | fgets(&ligne[1],80,lefic); |
7fd59977 |
71 | } |
72 | } |
22835705 |
73 | |
74 | if ((ligne[0] & 128) && modefnes) |
75 | { |
008aef40 |
76 | for (i = 0; i < 80; i ++) |
77 | ligne[i] = (char)(ligne[i] ^ (150 + (i & 3))); |
7fd59977 |
78 | } |
79 | } |
22835705 |
80 | |
81 | if (feof(lefic)) |
82 | return 0; |
83 | |
84 | {//0x1A is END_OF_FILE for OS DOS and WINDOWS. For other OS we set this rule forcefully. |
85 | char *fc = strchr(ligne, 0x1A); |
86 | if(fc != 0) |
87 | { |
88 | fc[0] = '\0'; |
89 | return 0; |
90 | } |
91 | } |
92 | |
7fd59977 |
93 | iges_fautrelire = 0; |
94 | if (ligne[0] == '\0' || ligne[0] == '\n' || ligne[0] == '\r') |
95 | return iges_lire(lefic,numsec,ligne,modefnes); /* 0 */ |
a849d42d |
96 | |
eeec0986 |
97 | if (sscanf(&ligne[73],"%d",&result) != 0) { |
a849d42d |
98 | *numsec = result; |
7fd59977 |
99 | typesec = ligne[72]; |
100 | switch (typesec) { |
a849d42d |
101 | case 'S' : ligne[72] = '\0'; return (1); |
102 | case 'G' : ligne[72] = '\0'; return (2); |
103 | case 'D' : ligne[72] = '\0'; return (3); |
104 | case 'P' : ligne[72] = '\0'; return (4); |
105 | case 'T' : ligne[72] = '\0'; return (5); |
106 | default :; |
107 | } |
108 | /* the column 72 is empty, try to check the neighbour*/ |
109 | if(strlen(ligne)==80 |
110 | && (ligne[79]=='\n' || ligne[79]=='\r') && (ligne[0]<='9' && ligne[0]>='0')) { |
111 | /*check if the case of losted .*/ |
112 | int index; |
113 | for(index = 1; ligne[index]<='9' && ligne[index]>='0'; index++); |
114 | if (ligne[index]=='D' || ligne[index]=='d') { |
115 | for(index = 79; index > 0; index--) |
116 | ligne[index] = ligne[index-1]; |
117 | ligne[0]='.'; |
118 | } |
119 | typesec = ligne[72]; |
120 | switch (typesec) { |
121 | case 'S' : ligne[72] = '\0'; return (1); |
122 | case 'G' : ligne[72] = '\0'; return (2); |
123 | case 'D' : ligne[72] = '\0'; return (3); |
124 | case 'P' : ligne[72] = '\0'; return (4); |
125 | case 'T' : ligne[72] = '\0'; return (5); |
126 | default :; |
127 | } |
7fd59977 |
128 | } |
129 | } |
130 | |
a849d42d |
131 | // the line is not conform to standard, try to read it (if there are some missing spaces) |
132 | // find the number end |
133 | i = (int)strlen(ligne); |
134 | while ((ligne[i] == '\0' || ligne[i] == '\n' || ligne[i] == '\r' || ligne[i] == ' ') && i > 0) |
135 | i--; |
136 | if (i != (int)strlen(ligne)) |
137 | ligne[i + 1] = '\0'; |
138 | // find the number start |
139 | while (ligne[i] >= '0' && ligne[i] <= '9' && i > 0) |
140 | i--; |
141 | if (sscanf(&ligne[i + 1],"%d",&result) == 0) |
142 | return -1; |
143 | *numsec = result; |
144 | // find type of line |
145 | while (ligne[i] == ' ' && i > 0) |
146 | i--; |
147 | typesec = ligne[i]; |
148 | switch (typesec) { |
149 | case 'S' : ligne[i] = '\0'; return (1); |
150 | case 'G' : ligne[i] = '\0'; return (2); |
151 | case 'D' : ligne[i] = '\0'; return (3); |
152 | case 'P' : ligne[i] = '\0'; return (4); |
153 | case 'T' : ligne[i] = '\0'; return (5); |
154 | default :; /* printf("Ligne incorrecte, ignoree n0.%d :\n%s\n",*numl,ligne); */ |
155 | } |
7fd59977 |
156 | return -1; |
157 | } |
158 | |
159 | /* Pour commander la relecture sur place */ |
160 | |
161 | void iges_arelire() |
162 | { iges_fautrelire = 1; } |