| [8] | 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| [151] | 7 | |
|---|
| 8 | |
|---|
| [8] | 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| [151] | 13 | |
|---|
| [8] | 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | #define LIB3DS_EXPORT |
|---|
| [1563] | 23 | #include "readwrite.h" |
|---|
| [5453] | 24 | #include <osg/Endian> |
|---|
| [8] | 25 | |
|---|
| [10076] | 26 | |
|---|
| [8] | 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| [5453] | 33 | static bool s_requiresByteSwap = false; |
|---|
| 34 | |
|---|
| 35 | extern LIB3DSAPI void setByteOrder() |
|---|
| 36 | { |
|---|
| 37 | s_requiresByteSwap = osg::getCpuByteOrder()==osg::BigEndian; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| [8] | 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| [151] | 45 | |
|---|
| [8] | 46 | |
|---|
| [151] | 47 | |
|---|
| [8] | 48 | |
|---|
| [151] | 49 | |
|---|
| [8] | 50 | |
|---|
| 51 | Lib3dsByte |
|---|
| [10076] | 52 | lib3ds_byte_read(iostream *strm) |
|---|
| [8] | 53 | { |
|---|
| [151] | 54 | Lib3dsByte b; |
|---|
| [8] | 55 | |
|---|
| [10076] | 56 | ASSERT(strm); |
|---|
| 57 | strm->read((char*)&b,1); |
|---|
| [151] | 58 | return(b); |
|---|
| [8] | 59 | } |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| [151] | 63 | |
|---|
| [8] | 64 | |
|---|
| [151] | 65 | |
|---|
| [8] | 66 | |
|---|
| [151] | 67 | |
|---|
| [8] | 68 | |
|---|
| 69 | Lib3dsWord |
|---|
| [10076] | 70 | lib3ds_word_read(iostream *strm) |
|---|
| [8] | 71 | { |
|---|
| [151] | 72 | Lib3dsByte b[2]; |
|---|
| 73 | Lib3dsWord w; |
|---|
| [8] | 74 | |
|---|
| [10076] | 75 | ASSERT(strm); |
|---|
| 76 | strm->read((char*)&b,2); |
|---|
| [151] | 77 | w=((Lib3dsWord)b[1] << 8) | |
|---|
| 78 | ((Lib3dsWord)b[0]); |
|---|
| 79 | return(w); |
|---|
| [8] | 80 | } |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| [151] | 86 | |
|---|
| [8] | 87 | |
|---|
| [151] | 88 | |
|---|
| [8] | 89 | |
|---|
| [151] | 90 | |
|---|
| [8] | 91 | |
|---|
| 92 | Lib3dsDword |
|---|
| [10076] | 93 | lib3ds_dword_read(iostream *strm) |
|---|
| [8] | 94 | { |
|---|
| [151] | 95 | Lib3dsByte b[4]; |
|---|
| 96 | Lib3dsDword d; |
|---|
| 97 | |
|---|
| [10076] | 98 | ASSERT(strm); |
|---|
| 99 | strm->read((char*)&b,4); |
|---|
| [151] | 100 | d=((Lib3dsDword)b[3] << 24) | |
|---|
| 101 | ((Lib3dsDword)b[2] << 16) | |
|---|
| 102 | ((Lib3dsDword)b[1] << 8) | |
|---|
| 103 | ((Lib3dsDword)b[0]); |
|---|
| 104 | return(d); |
|---|
| [8] | 105 | } |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| [151] | 111 | |
|---|
| [8] | 112 | |
|---|
| [151] | 113 | |
|---|
| [8] | 114 | |
|---|
| [151] | 115 | |
|---|
| [8] | 116 | |
|---|
| 117 | Lib3dsIntb |
|---|
| [10076] | 118 | lib3ds_intb_read(iostream *strm) |
|---|
| [8] | 119 | { |
|---|
| [151] | 120 | Lib3dsIntb b; |
|---|
| [8] | 121 | |
|---|
| [10076] | 122 | ASSERT(strm); |
|---|
| 123 | strm->read((char*)&b,1); |
|---|
| [151] | 124 | return(b); |
|---|
| [8] | 125 | } |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | |
|---|
| 130 | |
|---|
| [151] | 131 | |
|---|
| [8] | 132 | |
|---|
| [151] | 133 | |
|---|
| [8] | 134 | |
|---|
| [151] | 135 | |
|---|
| [8] | 136 | |
|---|
| 137 | Lib3dsIntw |
|---|
| [10076] | 138 | lib3ds_intw_read(iostream *strm) |
|---|
| [8] | 139 | { |
|---|
| [151] | 140 | Lib3dsByte b[2]; |
|---|
| [8] | 141 | |
|---|
| [10076] | 142 | ASSERT(strm); |
|---|
| 143 | strm->read((char*)&b,2); |
|---|
| [5453] | 144 | |
|---|
| 145 | if (s_requiresByteSwap) |
|---|
| 146 | { |
|---|
| 147 | osg::swapBytes2((char*)b); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | return (*((Lib3dsIntw*)b)); |
|---|
| [8] | 151 | } |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| [151] | 157 | |
|---|
| [8] | 158 | |
|---|
| [151] | 159 | |
|---|
| [8] | 160 | |
|---|
| [151] | 161 | |
|---|
| [8] | 162 | |
|---|
| 163 | Lib3dsIntd |
|---|
| [10076] | 164 | lib3ds_intd_read(iostream *strm) |
|---|
| [8] | 165 | { |
|---|
| [10076] | 166 | Lib3dsByte b[4]; |
|---|
| [151] | 167 | |
|---|
| [10076] | 168 | ASSERT(strm); |
|---|
| 169 | strm->read((char*)&b,4); |
|---|
| [5453] | 170 | |
|---|
| 171 | if (s_requiresByteSwap) |
|---|
| 172 | { |
|---|
| 173 | osg::swapBytes4((char*)b); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | return (*((Lib3dsIntd*)b)); |
|---|
| [10076] | 177 | |
|---|
| [8] | 178 | } |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| [151] | 184 | |
|---|
| [8] | 185 | |
|---|
| [151] | 186 | |
|---|
| [8] | 187 | |
|---|
| [151] | 188 | |
|---|
| [8] | 189 | |
|---|
| 190 | Lib3dsFloat |
|---|
| [10076] | 191 | lib3ds_float_read(iostream *strm) |
|---|
| [8] | 192 | { |
|---|
| [151] | 193 | Lib3dsByte b[4]; |
|---|
| [8] | 194 | |
|---|
| [10076] | 195 | ASSERT(strm); |
|---|
| 196 | b[0]=b[1]=b[2]=b[3]=0; |
|---|
| 197 | strm->read((char*)&b,4); |
|---|
| [5453] | 198 | |
|---|
| 199 | if (s_requiresByteSwap) |
|---|
| 200 | { |
|---|
| 201 | osg::swapBytes4((char*)b); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | return (*((Lib3dsFloat*)b)); |
|---|
| [8] | 205 | } |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | |
|---|
| [151] | 212 | |
|---|
| [8] | 213 | |
|---|
| [151] | 214 | |
|---|
| 215 | |
|---|
| [8] | 216 | |
|---|
| [151] | 217 | |
|---|
| [8] | 218 | |
|---|
| 219 | Lib3dsBool |
|---|
| [10076] | 220 | lib3ds_vector_read(Lib3dsVector v, iostream *strm) |
|---|
| [8] | 221 | { |
|---|
| [10076] | 222 | v[0]=lib3ds_float_read(strm); |
|---|
| 223 | v[1]=lib3ds_float_read(strm); |
|---|
| 224 | v[2]=lib3ds_float_read(strm); |
|---|
| [8] | 225 | |
|---|
| [10076] | 226 | if (strm->fail()) { |
|---|
| [151] | 227 | return(LIB3DS_FALSE); |
|---|
| 228 | } |
|---|
| [10076] | 229 | |
|---|
| [5453] | 230 | |
|---|
| [10076] | 231 | |
|---|
| [151] | 232 | return(LIB3DS_TRUE); |
|---|
| [8] | 233 | } |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | Lib3dsBool |
|---|
| [10076] | 240 | lib3ds_rgb_read(Lib3dsRgb rgb, iostream *strm) |
|---|
| [8] | 241 | { |
|---|
| [10076] | 242 | rgb[0]=lib3ds_float_read(strm); |
|---|
| 243 | rgb[1]=lib3ds_float_read(strm); |
|---|
| 244 | rgb[2]=lib3ds_float_read(strm); |
|---|
| [8] | 245 | |
|---|
| [10076] | 246 | if (strm->fail()) { |
|---|
| [151] | 247 | return(LIB3DS_FALSE); |
|---|
| 248 | } |
|---|
| [5453] | 249 | |
|---|
| 250 | |
|---|
| [151] | 251 | return(LIB3DS_TRUE); |
|---|
| [8] | 252 | } |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | Lib3dsBool |
|---|
| [10076] | 267 | lib3ds_string_read(char *s, int buflen, iostream *strm) |
|---|
| [8] | 268 | { |
|---|
| [151] | 269 | int k=0; |
|---|
| [10076] | 270 | ASSERT(s); |
|---|
| 271 | s--; |
|---|
| 272 | do |
|---|
| 273 | { |
|---|
| 274 | s++; |
|---|
| 275 | k++; |
|---|
| 276 | strm->read(s,1); |
|---|
| 277 | } while ((*s!=0) && (k<buflen)); |
|---|
| 278 | |
|---|
| 279 | if (strm->fail()) { |
|---|
| [151] | 280 | return(LIB3DS_FALSE); |
|---|
| 281 | } |
|---|
| 282 | return(LIB3DS_TRUE); |
|---|
| [8] | 283 | } |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | Lib3dsBool |
|---|
| [10076] | 296 | lib3ds_byte_write(Lib3dsByte b, iostream *strm) |
|---|
| [8] | 297 | { |
|---|
| [10076] | 298 | ASSERT(strm); |
|---|
| 299 | strm->write((char*)&b,1); |
|---|
| 300 | if (strm->fail()) { |
|---|
| [151] | 301 | return(LIB3DS_FALSE); |
|---|
| 302 | } |
|---|
| 303 | return(LIB3DS_TRUE); |
|---|
| [8] | 304 | } |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | Lib3dsBool |
|---|
| [10076] | 318 | lib3ds_word_write(Lib3dsWord w, iostream *strm) |
|---|
| [8] | 319 | { |
|---|
| [151] | 320 | Lib3dsByte b[2]; |
|---|
| [8] | 321 | |
|---|
| [10076] | 322 | ASSERT(strm); |
|---|
| [9571] | 323 | b[1]=(Lib3dsByte)(((Lib3dsWord)w & 0xFF00) >> 8); |
|---|
| 324 | b[0]=(Lib3dsByte)((Lib3dsWord)w & 0x00FF); |
|---|
| [10076] | 325 | strm->write((char*)b,2); |
|---|
| 326 | if (strm->fail()) { |
|---|
| [151] | 327 | return(LIB3DS_FALSE); |
|---|
| 328 | } |
|---|
| 329 | return(LIB3DS_TRUE); |
|---|
| [8] | 330 | } |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | |
|---|
| 343 | Lib3dsBool |
|---|
| [10076] | 344 | lib3ds_dword_write(Lib3dsDword d, iostream *strm) |
|---|
| [8] | 345 | { |
|---|
| [151] | 346 | Lib3dsByte b[4]; |
|---|
| [8] | 347 | |
|---|
| [10076] | 348 | ASSERT(strm); |
|---|
| [151] | 349 | b[3]=(Lib3dsByte)(((Lib3dsDword)d & 0xFF000000) >> 24); |
|---|
| 350 | b[2]=(Lib3dsByte)(((Lib3dsDword)d & 0x00FF0000) >> 16); |
|---|
| 351 | b[1]=(Lib3dsByte)(((Lib3dsDword)d & 0x0000FF00) >> 8); |
|---|
| 352 | b[0]=(Lib3dsByte)(((Lib3dsDword)d & 0x000000FF)); |
|---|
| [10076] | 353 | |
|---|
| 354 | strm->write((char*)b,4); |
|---|
| 355 | if (strm->fail()) { |
|---|
| [151] | 356 | return(LIB3DS_FALSE); |
|---|
| 357 | } |
|---|
| 358 | return(LIB3DS_TRUE); |
|---|
| [8] | 359 | } |
|---|
| 360 | |
|---|
| 361 | |
|---|
| 362 | |
|---|
| 363 | |
|---|
| 364 | |
|---|
| 365 | |
|---|
| 366 | |
|---|
| 367 | |
|---|
| 368 | |
|---|
| 369 | |
|---|
| 370 | |
|---|
| 371 | |
|---|
| 372 | Lib3dsBool |
|---|
| [10076] | 373 | lib3ds_intb_write(Lib3dsIntb b, iostream *strm) |
|---|
| [8] | 374 | { |
|---|
| [10076] | 375 | ASSERT(strm); |
|---|
| [10128] | 376 | strm->write((const char*)&b,1); |
|---|
| [10076] | 377 | if (strm->fail()) { |
|---|
| [151] | 378 | return(LIB3DS_FALSE); |
|---|
| 379 | } |
|---|
| 380 | return(LIB3DS_TRUE); |
|---|
| [8] | 381 | } |
|---|
| 382 | |
|---|
| 383 | |
|---|
| 384 | |
|---|
| 385 | |
|---|
| 386 | |
|---|
| 387 | |
|---|
| 388 | |
|---|
| 389 | |
|---|
| 390 | |
|---|
| 391 | |
|---|
| 392 | |
|---|
| 393 | |
|---|
| 394 | Lib3dsBool |
|---|
| [10076] | 395 | lib3ds_intw_write(Lib3dsIntw w, iostream *strm) |
|---|
| [8] | 396 | { |
|---|
| [151] | 397 | Lib3dsByte b[2]; |
|---|
| [8] | 398 | |
|---|
| [10076] | 399 | ASSERT(strm); |
|---|
| [9571] | 400 | b[1]=(Lib3dsByte)(((Lib3dsWord)w & 0xFF00) >> 8); |
|---|
| 401 | b[0]=(Lib3dsByte)((Lib3dsWord)w & 0x00FF); |
|---|
| [10076] | 402 | |
|---|
| [10088] | 403 | strm->write((const char*)b,2); |
|---|
| [10076] | 404 | if (strm->fail()) { |
|---|
| [151] | 405 | return(LIB3DS_FALSE); |
|---|
| 406 | } |
|---|
| 407 | return(LIB3DS_TRUE); |
|---|
| [8] | 408 | } |
|---|
| 409 | |
|---|
| 410 | |
|---|
| 411 | |
|---|
| 412 | |
|---|
| 413 | |
|---|
| 414 | |
|---|
| 415 | |
|---|
| 416 | |
|---|
| 417 | |
|---|
| 418 | |
|---|
| 419 | |
|---|
| 420 | |
|---|
| 421 | Lib3dsBool |
|---|
| [10076] | 422 | lib3ds_intd_write(Lib3dsIntd d, iostream *strm) |
|---|
| [8] | 423 | { |
|---|
| [151] | 424 | Lib3dsByte b[4]; |
|---|
| [8] | 425 | |
|---|
| [10076] | 426 | ASSERT(strm); |
|---|
| [151] | 427 | b[3]=(Lib3dsByte)(((Lib3dsDword)d & 0xFF000000) >> 24); |
|---|
| 428 | b[2]=(Lib3dsByte)(((Lib3dsDword)d & 0x00FF0000) >> 16); |
|---|
| 429 | b[1]=(Lib3dsByte)(((Lib3dsDword)d & 0x0000FF00) >> 8); |
|---|
| 430 | b[0]=(Lib3dsByte)(((Lib3dsDword)d & 0x000000FF)); |
|---|
| [10076] | 431 | |
|---|
| [10088] | 432 | strm->write((const char*)b,4); |
|---|
| [10076] | 433 | if (strm->fail()) { |
|---|
| [151] | 434 | return(LIB3DS_FALSE); |
|---|
| 435 | } |
|---|
| 436 | return(LIB3DS_TRUE); |
|---|
| [8] | 437 | } |
|---|
| 438 | |
|---|
| 439 | |
|---|
| 440 | |
|---|
| 441 | |
|---|
| 442 | |
|---|
| 443 | |
|---|
| 444 | |
|---|
| 445 | |
|---|
| 446 | |
|---|
| 447 | |
|---|
| 448 | |
|---|
| 449 | |
|---|
| 450 | Lib3dsBool |
|---|
| [10076] | 451 | lib3ds_float_write(Lib3dsFloat l, iostream *strm) |
|---|
| [8] | 452 | { |
|---|
| [10076] | 453 | ASSERT(strm); |
|---|
| [9397] | 454 | |
|---|
| [151] | 455 | Lib3dsByte b[4]; |
|---|
| [9397] | 456 | Lib3dsByte* ptr = (Lib3dsByte*) (&l); |
|---|
| [8] | 457 | |
|---|
| [9397] | 458 | if (s_requiresByteSwap) |
|---|
| 459 | { |
|---|
| 460 | b[3] = *ptr++; |
|---|
| 461 | b[2] = *ptr++; |
|---|
| 462 | b[1] = *ptr++; |
|---|
| 463 | b[0] = *ptr++; |
|---|
| 464 | } |
|---|
| 465 | else |
|---|
| 466 | { |
|---|
| 467 | b[0] = *ptr++; |
|---|
| 468 | b[1] = *ptr++; |
|---|
| 469 | b[2] = *ptr++; |
|---|
| 470 | b[3] = *ptr++; |
|---|
| 471 | } |
|---|
| [10076] | 472 | |
|---|
| 473 | strm->write((char*)b,4); |
|---|
| 474 | if (strm->fail()) { |
|---|
| [151] | 475 | return(LIB3DS_FALSE); |
|---|
| 476 | } |
|---|
| 477 | return(LIB3DS_TRUE); |
|---|
| [8] | 478 | } |
|---|
| 479 | |
|---|
| 480 | |
|---|
| 481 | |
|---|
| 482 | |
|---|
| 483 | |
|---|
| 484 | |
|---|
| [151] | 485 | |
|---|
| [8] | 486 | |
|---|
| [151] | 487 | |
|---|
| 488 | |
|---|
| [8] | 489 | |
|---|
| 490 | Lib3dsBool |
|---|
| [10076] | 491 | lib3ds_vector_write(Lib3dsVector v, iostream *strm) |
|---|
| [8] | 492 | { |
|---|
| [10076] | 493 | if (!lib3ds_float_write(v[0], strm)) { |
|---|
| [151] | 494 | return(LIB3DS_FALSE); |
|---|
| 495 | } |
|---|
| [10076] | 496 | if (!lib3ds_float_write(v[1], strm)) { |
|---|
| [151] | 497 | return(LIB3DS_FALSE); |
|---|
| 498 | } |
|---|
| [10076] | 499 | if (!lib3ds_float_write(v[2], strm)) { |
|---|
| [151] | 500 | return(LIB3DS_FALSE); |
|---|
| 501 | } |
|---|
| 502 | return(LIB3DS_TRUE); |
|---|
| [8] | 503 | } |
|---|
| 504 | |
|---|
| 505 | |
|---|
| 506 | |
|---|
| 507 | |
|---|
| 508 | |
|---|
| 509 | Lib3dsBool |
|---|
| [10076] | 510 | lib3ds_rgb_write(Lib3dsRgb rgb, iostream *strm) |
|---|
| [8] | 511 | { |
|---|
| [10076] | 512 | if (!lib3ds_float_write(rgb[0], strm)) { |
|---|
| [151] | 513 | return(LIB3DS_FALSE); |
|---|
| 514 | } |
|---|
| [10076] | 515 | if (!lib3ds_float_write(rgb[1], strm)) { |
|---|
| [151] | 516 | return(LIB3DS_FALSE); |
|---|
| 517 | } |
|---|
| [10076] | 518 | if (!lib3ds_float_write(rgb[2], strm)) { |
|---|
| [151] | 519 | return(LIB3DS_FALSE); |
|---|
| 520 | } |
|---|
| 521 | return(LIB3DS_TRUE); |
|---|
| [8] | 522 | } |
|---|
| 523 | |
|---|
| 524 | |
|---|
| 525 | |
|---|
| 526 | |
|---|
| 527 | |
|---|
| 528 | |
|---|
| 529 | |
|---|
| 530 | |
|---|
| 531 | |
|---|
| 532 | |
|---|
| 533 | |
|---|
| 534 | |
|---|
| 535 | Lib3dsBool |
|---|
| [10076] | 536 | lib3ds_string_write(const char *s, iostream *strm) |
|---|
| [8] | 537 | { |
|---|
| [151] | 538 | ASSERT(s); |
|---|
| [10076] | 539 | ASSERT(strm); |
|---|
| 540 | do strm->write(s,1); while (*s++); |
|---|
| 541 | if (strm->fail()) { |
|---|
| [151] | 542 | return(LIB3DS_FALSE); |
|---|
| 543 | } |
|---|
| 544 | return(LIB3DS_TRUE); |
|---|
| [8] | 545 | } |
|---|