00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include <string.h>
00012
00013 #include "tdef.h"
00014 #include "xalloc.h"
00015 #include "uti.h"
00016 #include "cabdef0.h"
00017 #include "cabctrl.h"
00018
00019
00020
00021 FLOAT4 xlee_sfloat( FILE * f )
00022 {
00023 FLOAT4 sf;
00024
00025 xfread(&sf,sizeof(sf),1,f);
00026 endian_fromlittle32(&sf);
00027 return sf;
00028 }
00029
00030
00031
00032 void xescribe_sfloat( FILE * f,
00033 FLOAT4 sf )
00034 {
00035 endian_tolittle32(&sf);
00036 xfwrite(&sf,sizeof(sf),1,f);
00037 }
00038
00039
00040
00041
00042
00043
00044 void salta_cab( FILE * fp )
00045 {
00046 UINT4 l;
00047
00048 change_rw(fp);
00049 xfread(&l,sizeof(l),1,fp);
00050 endian_fromlittle32(&l);
00051 xfseek(fp,l,SEEK_CUR);
00052 }
00053
00054
00055
00056
00057
00058
00059 UINT4 get_cmask( FILE * fp )
00060 {
00061 UINT4 mcab;
00062
00063 vete_cmask(fp);
00064 xfread(&mcab,sizeof(mcab),1,fp);
00065 endian_fromlittle32(&mcab);
00066 change_rw(fp);
00067
00068 return mcab;
00069 }
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 int vete_cab( FILE * fp, UINT4 cabmask )
00080 {
00081 UINT4 mcab;
00082
00083 if (cabmask==MC_NINGUNA) {
00084 vete_cfich(fp);
00085 return 1;
00086 }
00087 else if (cabmask==MC_TODAS) {
00088 vete_cfich(fp);
00089 salta_cab(fp);
00090 return 1;
00091 }
00092 else {
00093 mcab = get_cmask(fp);
00094
00095 while (mcab) {
00096 if (mcab & cabmask & 0x0001)
00097 return 1;
00098 if (mcab & 0x0001)
00099 salta_cab(fp);
00100
00101 mcab >>= 1;
00102 cabmask >>= 1;
00103 }
00104 }
00105 return 0;
00106 }
00107
00108
00109
00110
00111
00112 void xvete_cab( FILE * fp, UINT4 cabmask )
00113 {
00114 cdie_beep(vete_cab(fp,cabmask)==0,"vete_cab error: requested file header not found" );
00115 }
00116
00117
00118
00119
00120 void lee_cfich( FILE * fp,
00121 struct cfich * cf_ptr)
00122 {
00123 xfread(cf_ptr,sizeof(*cf_ptr),1,fp);
00124 endian_fromlittle32(&(cf_ptr->lcab));
00125 endian_fromlittle16(&(cf_ptr->tfich));
00126 endian_fromlittle32(&(cf_ptr->mcab));
00127 }
00128
00129
00130
00131
00132 void escribe_cfich( FILE * fp,
00133 struct cfich * cf_ptr)
00134 {
00135 endian_tolittle32(&(cf_ptr->lcab));
00136 endian_tolittle16(&(cf_ptr->tfich));
00137 endian_tolittle32(&(cf_ptr->mcab));
00138 xfwrite(cf_ptr,sizeof(*cf_ptr),1,fp);
00139 endian_fromlittle32(&(cf_ptr->lcab));
00140 endian_fromlittle16(&(cf_ptr->tfich));
00141 endian_fromlittle32(&(cf_ptr->mcab));
00142 }
00143
00144
00145
00146
00147
00148
00149 void anade_cmask( FILE * fp, UINT4 cabmask )
00150 {
00151 fpos_t pos;
00152 UINT4 cm;
00153
00154 xfgetpos(fp,&pos);
00155
00156 cm = get_cmask(fp);
00157
00158 cm |= cabmask;
00159 vete_cmask(fp);
00160 endian_tolittle32(&cm);
00161 xfwrite(&cm,sizeof(cm),1,fp);
00162
00163 xfsetpos(fp,&pos);
00164 change_rw(fp);
00165 }
00166
00167
00168
00169
00170 void escribe_long( FILE * fp, UINT4 l )
00171 {
00172 endian_tolittle32(&l);
00173 xfwrite(&l,sizeof(l),1,fp);
00174 }
00175
00176
00177
00178
00179
00180
00181 void escribe_long_actual( FILE * fp, UINT4 cabmask )
00182 {
00183 fpos_t pos;
00184 long pos0, pos1;
00185
00186 xfgetpos(fp,&pos);
00187
00188 pos1 = xftell(fp);
00189 xvete_cab(fp,cabmask);
00190 pos0 = xftell(fp)+sizeof(UINT4);
00191 escribe_long(fp, pos1-pos0);
00192
00193 xfsetpos(fp,&pos);
00194 }
00195
00196
00197
00198
00199
00200 long lee_long_actual( FILE * fp, UINT4 cabmask )
00201 {
00202 fpos_t pos;
00203 long pos0, pos1;
00204
00205 xfgetpos(fp,&pos);
00206
00207 pos1 = xftell(fp);
00208 xvete_cab(fp,cabmask);
00209 pos0 = xftell(fp)+sizeof(UINT4);
00210
00211 xfsetpos(fp,&pos);
00212 return(pos1-pos0);
00213 }
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237 UINT4 prepara_cvariable( pVOID * cvptr,
00238 UINT4 nb,
00239 UINT4 fnb,
00240 BOOL is_txt )
00241 {
00242 UINT4 n;
00243
00244 if (*cvptr==NULL) {
00245 n = ((nb) ? nb : fnb)+( (is_txt) ? 1 : 0);
00246 if (n)
00247 *cvptr = xmalloc(n);
00248 else
00249 *cvptr = NULL;
00250
00251 return( ((fnb<nb)||(!nb)) ? fnb : nb );
00252 }
00253 else
00254 return( (fnb<nb) ? fnb : nb );
00255 }
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271 int lee_cvariable_txt( FILE * fp,
00272 pCHAR1 * cvptr,
00273 UINT2 * nelem )
00274 {
00275 UINT2 w;
00276
00277 xfread(&w,sizeof(w),1,fp);
00278 endian_fromlittle16(&w);
00279 *nelem = (UINT2)prepara_cvariable((pVOID *)cvptr,((UINT4)*nelem)*sizeof(char),
00280 ((UINT4)w)*sizeof(char),TRUE)/sizeof(char);
00281 xfread(*cvptr,sizeof(char),*nelem,fp);
00282 xfseek(fp,((UINT4)(w-(*nelem)))*sizeof(char),SEEK_CUR);
00283
00284 *((*cvptr)+(*nelem)) = 0;
00285
00286 return ( (*nelem<w) ? 1 : 0 );
00287 }
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299 int lee_cvariable_marcas( FILE * fp,
00300 pUINT4 * cvptr,
00301 UINT4 * nelem )
00302 {
00303 UINT4 l,i;
00304 pUINT4 tmpptr;
00305
00306 xfread(&l,sizeof(l),1,fp);
00307 endian_fromlittle32(&l);
00308 *nelem = prepara_cvariable( (pVOID *)cvptr,(*nelem)*sizeof(UINT4),
00309 l*sizeof(UINT4),FALSE)/sizeof(UINT4);
00310 tmpptr=(*cvptr);
00311 for (i=0; i<*nelem;i++) {
00312 xfread((void *)(tmpptr),sizeof(UINT4),1,fp);
00313 endian_fromlittle32(tmpptr);
00314 tmpptr++;
00315 }
00316 xfseek(fp,(l-(*nelem))*sizeof(UINT4),SEEK_CUR);
00317
00318 return ( (*nelem<l) ? 1 : 0 );
00319 }
00320
00321
00322
00323
00324 void escribe_cvariable_txt( FILE * fp,
00325 pCHAR1 cvptr,
00326 UINT2 nelem )
00327 {
00328 UINT2 n=nelem;
00329 endian_tolittle16(&nelem);
00330 xfwrite(&nelem,sizeof(nelem),1,fp);
00331 xfwrite(cvptr,sizeof(*cvptr),n,fp);
00332 }
00333
00334
00335
00336
00337 void escribe_cvariable_marcas( FILE * fp,
00338 pUINT4 cvptr,
00339 UINT4 nelem )
00340 {
00341 UINT4 i;
00342
00343 UINT4 n=nelem;
00344 endian_tolittle32(&nelem);
00345 xfwrite(&nelem,sizeof(nelem),1,fp);
00346
00347 for (i=0;i<n;i++) {
00348 endian_tolittle32(cvptr);
00349 xfwrite((void *)cvptr,sizeof(*cvptr),1,fp);
00350 endian_fromlittle32(cvptr);
00351 cvptr++;
00352 }
00353 }
00354
00355
00356
00357
00358 UINT4 long_cgsenal( struct cgsenal * cgs )
00359 {
00360 #define SIZE(sf) ((UINT4)sizeof(cgs->sf))
00361 #define TSIZE(tf) (UINT4)(sizeof(cgs->tf.ltxt)+ \
00362 ((UINT4)(cgs->tf.ltxt))*sizeof(*(cgs->tf.txt)))
00363
00364 return( SIZE(nm)+SIZE(fm)+SIZE(rango)+TSIZE(texto)+TSIZE(version)+
00365 TSIZE(locu)+TSIZE(obs)+sizeof(cgs->marc.unimarc)+
00366 sizeof(cgs->marc.nmarc)+
00367 (cgs->marc.nmarc)*sizeof(*(cgs->marc.marcas)) );
00368 }
00369
00370
00371
00372
00373
00374 void free_cgsenal( struct cgsenal * cgs )
00375 {
00376 #define FREE(tf) if (cgs->tf.txt!=NULL) \
00377 xfree((void *)cgs->tf.txt); \
00378 cgs->tf.ltxt=0
00379
00380 FREE(locu);
00381 FREE(texto);
00382 FREE(version);
00383 FREE(obs);
00384 if (cgs->marc.marcas!=NULL)
00385 xfree((void *)cgs->marc.marcas);
00386 cgs->marc.nmarc = 0;
00387 }
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401 int lee_cgsenal( FILE * fp, struct cgsenal * cgs )
00402 {
00403 #define READCGS32(sf) { INT32 i32; xfread(&i32,sizeof(i32),1,fp); endian_fromlittle32(&i32); cgs->sf=i32; }
00404 #define FREADCGS(sf) cgs->sf = xlee_sfloat(fp)
00405 #define TREADCGS(tf) { pCHAR1 pch=cgs->tf.txt; UINT2 l=cgs->tf.ltxt; retval|=lee_cvariable_txt(fp,&pch,&l); cgs->tf.txt=pch; cgs->tf.ltxt=l; }
00406
00407 int retval = 0;
00408 pUINT4 p;
00409 UINT4 nelem;
00410
00411 READCGS32(nm);
00412 FREADCGS(fm);
00413 FREADCGS(rango.min);
00414 FREADCGS(rango.max);
00415 TREADCGS(locu);
00416 TREADCGS(texto);
00417 TREADCGS(version);
00418 TREADCGS(obs);
00419 READCGS32(marc.unimarc);
00420 p=cgs->marc.marcas;
00421 nelem=cgs->marc.nmarc;
00422 retval |= lee_cvariable_marcas(fp,&p,&nelem);
00423 cgs->marc.marcas=p;
00424 cgs->marc.nmarc=nelem;
00425 return retval;
00426 }
00427
00428
00429
00430
00431 void escribe_cgsenal( FILE * fp, struct cgsenal * cgs )
00432 {
00433 #define WRITECGS32(sf) { INT32 i32=cgs->sf; endian_tolittle32(&i32); xfwrite(&i32,sizeof(i32),1,fp); }
00434 #define FWRITECGS(sf) xescribe_sfloat(fp,cgs->sf)
00435 #define TWRITECGS(tf) { pCHAR1 pch=cgs->tf.txt; UINT2 l=cgs->tf.ltxt; escribe_cvariable_txt( fp,pch,l); }
00436 pUINT4 p;
00437 UINT4 nelem;
00438
00439 WRITECGS32(nm);
00440 FWRITECGS(fm);
00441 FWRITECGS(rango.min);
00442 FWRITECGS(rango.max);
00443 TWRITECGS(locu);
00444 TWRITECGS(texto);
00445 TWRITECGS(version);
00446 TWRITECGS(obs);
00447 WRITECGS32(marc.unimarc);
00448 p=cgs->marc.marcas;
00449 nelem=cgs->marc.nmarc;
00450 escribe_cvariable_marcas(fp,p,nelem);
00451 }
00452
00453
00454
00455
00456
00457 void escribe_cgsenal_marc( FILE * fp, struct cgsenal * cgs )
00458 {
00459 #define WRITEM32(sf) { INT32 i32=cgs->sf; endian_tolittle32(&i32); xfwrite(&i32,sizeof(i32),1,fp); }
00460 #define FWRITEM(sf) xescribe_sfloat(fp,cgs->sf)
00461 #define TWRITEM(tf) { pCHAR1 pch=cgs->tf.txt; UINT2 l=cgs->tf.ltxt; escribe_cvariable_txt( fp,pch,l); }
00462 WRITEM32(nm);
00463 FWRITEM(fm);
00464 FWRITEM(rango.min);
00465 FWRITEM(rango.max);
00466 TWRITEM(locu);
00467 TWRITEM(texto);
00468 TWRITEM(version);
00469 TWRITEM(obs);
00470 }
00471
00472
00473
00474
00475
00476 void limpia_texto( pCHAR1 p, UINT2 *l )
00477 {
00478 pCHAR1 str;
00479
00480 str = p;
00481
00482 while ((str[0])&&(str[strlen(str)-1]<=' '))
00483 str[strlen(str)-1]=0;
00484
00485 *l = strlen(str);
00486 }
00487
00488
00489
00490
00491 void lee_csenal_vieja( FILE * fp, struct cgsenal * cgs )
00492 {
00493 #define oREAD32(sf) { INT32 i32; xfread(&i32,sizeof(i32),1,fp); endian_fromlittle32(&i32); cgs->sf=i32; }
00494 #define oFREAD(sf) cgs->sf = xlee_sfloat(fp)
00495 #define oTREAD(tf,n) { \
00496 pCHAR1 pch=cgs->tf.txt; \
00497 UINT2 l=cgs->tf.ltxt; \
00498 l= (UINT2)prepara_cvariable( (pVOID *)&pch,l,n,TRUE); \
00499 xfread(pch,1,l,fp); \
00500 xfseek(fp,l,SEEK_CUR); \
00501 limpia_texto(pch,&l); \
00502 cgs->tf.txt=pch; \
00503 cgs->tf.ltxt=l; }
00504
00505 #define M(f) (cgs->marc.f)
00506
00507 INT2 n,u,m, i;
00508 pUINT4 p;
00509 UINT4 nelem;
00510
00511 oREAD32(nm);
00512 oFREAD(fm);
00513 cgs->rango.min=0;
00514 cgs->rango.max=4096;
00515 oTREAD(locu,39);
00516 oTREAD(texto,56*3);
00517 oTREAD(version,3);
00518 oTREAD(obs,56*3);
00519
00520 xfread(&n,sizeof(n),1,fp); endian_fromlittle16(&n);
00521 xfread(&u,sizeof(u),1,fp); endian_fromlittle16(&u);
00522 M(unimarc)=u;
00523 p=M(marcas);
00524 nelem=M(nmarc);
00525 nelem=prepara_cvariable( (pVOID *)&p,nelem*sizeof(UINT4),
00526 n*sizeof(UINT4),FALSE) / sizeof(UINT4);
00527 for (i=0; i<n; i++) {
00528 xfread(&m,sizeof(INT2),1,fp);
00529 endian_fromlittle16(&m);
00530 p[i] = m-1;
00531 }
00532 M(marcas)=p;
00533 M(nmarc)=nelem;
00534
00535 xfseek(fp,512,SEEK_SET);
00536 }
00537
00538
00539
00540
00541 void escribe_csenal_vieja( FILE * fp, struct cgsenal * cgs )
00542 {
00543 #define oWRITE32(sf) { INT32 i=cgs->sf; endian_tolittle32(&i); xfwrite(&i,sizeof(i),1,fp); }
00544 #define oFWRITE(sf) xescribe_sfloat(fp,cgs->sf)
00545 #define oTWRITE(tf,n) { \
00546 UINT2 tmp; \
00547 INT8 c0=0; \
00548 \
00549 tmp = cgs->tf.ltxt; \
00550 if (tmp>n) \
00551 tmp = n; \
00552 xfwrite(cgs->tf.txt,sizeof(INT8),tmp,fp); \
00553 while (tmp++<n) \
00554 xfwrite(&c0,sizeof(INT8),1,fp); \
00555 }
00556
00557 #define M(f) (cgs->marc.f)
00558
00559 UINT2 n,nn, u,m, i;
00560
00561 oWRITE32(nm);
00562 oFWRITE(fm);
00563 oTWRITE(locu,39);
00564 oTWRITE(texto,56*3);
00565 oTWRITE(version,3);
00566 oTWRITE(obs,56*3);
00567
00568 n=(UINT2)M(nmarc);
00569 if (n>40) n = 40;
00570 nn=n;
00571 endian_tolittle16(&n);
00572 xfwrite(&n,sizeof(n),1,fp);
00573 u=(UINT2)M(unimarc);
00574 endian_tolittle16(&u);
00575 xfwrite(&u,sizeof(u),1,fp);
00576 for (i=0; i<nn; i++) {
00577 m=(UINT2)M(marcas)[i]+1;
00578 endian_tolittle16(&m);
00579 xfwrite(&m,sizeof(INT2),1,fp);
00580 }
00581
00582 i = 0;
00583 while (xftell(fp)<512)
00584 xfwrite(&i,1,1,fp);
00585 }
00586
00587
00588
00589
00590
00591 void lee_cgp_parcor( FILE * fp,
00592 struct cgp_parcor * cgp_pc_ptr )
00593 {
00594 xfread(cgp_pc_ptr,sizeof(*(cgp_pc_ptr)),1,fp);
00595 endian_fromlittle16(&(cgp_pc_ptr->cgp.vecd.dimv));
00596 endian_fromlittle32(&(cgp_pc_ptr->cgp.vecd.nvec));
00597 endian_fromlittle16(&(cgp_pc_ptr->cgp.pventa.lvent));
00598 endian_fromlittle16(&(cgp_pc_ptr->cgp.pventa.despv));
00599 endian_fromlittle32(&(cgp_pc_ptr->preenf));
00600 endian_fromlittle16(&(cgp_pc_ptr->orden));
00601 }
00602
00603
00604
00605
00606 void escribe_cgp_parcor( FILE * fp,
00607 struct cgp_parcor * cgp_pc_ptr)
00608 {
00609 endian_tolittle16(&(cgp_pc_ptr->cgp.vecd.dimv));
00610 endian_tolittle32(&(cgp_pc_ptr->cgp.vecd.nvec));
00611 endian_tolittle16(&(cgp_pc_ptr->cgp.pventa.lvent));
00612 endian_tolittle16(&(cgp_pc_ptr->cgp.pventa.despv));
00613 endian_tolittle32(&(cgp_pc_ptr->preenf));
00614 endian_tolittle16(&(cgp_pc_ptr->orden));
00615 xfwrite(cgp_pc_ptr,sizeof(*(cgp_pc_ptr)),1,fp);
00616 endian_fromlittle16(&(cgp_pc_ptr->cgp.vecd.dimv));
00617 endian_fromlittle32(&(cgp_pc_ptr->cgp.vecd.nvec));
00618 endian_fromlittle16(&(cgp_pc_ptr->cgp.pventa.lvent));
00619 endian_fromlittle16(&(cgp_pc_ptr->cgp.pventa.despv));
00620 endian_fromlittle32(&(cgp_pc_ptr->preenf));
00621 endian_fromlittle16(&(cgp_pc_ptr->orden));
00622 }
00623
00624
00625
00626
00627
00628 void lee_cgp_lpc( FILE * fp,
00629 struct cgp_lpc * cgp_lpc_ptr)
00630 {
00631 xfread(cgp_lpc_ptr,sizeof(*(cgp_lpc_ptr)),1,fp);
00632 endian_fromlittle16(&(cgp_lpc_ptr->cgp.vecd.dimv));
00633 endian_fromlittle32(&(cgp_lpc_ptr->cgp.vecd.nvec));
00634 endian_fromlittle16(&(cgp_lpc_ptr->cgp.pventa.lvent));
00635 endian_fromlittle16(&(cgp_lpc_ptr->cgp.pventa.despv));
00636 endian_fromlittle32(&(cgp_lpc_ptr->preenf));
00637 endian_fromlittle16(&(cgp_lpc_ptr->orden));
00638 }
00639
00640
00641
00642
00643 void escribe_cgp_lpc( FILE * fp,
00644 struct cgp_lpc * cgp_lpc_ptr)
00645 {
00646 endian_tolittle16(&(cgp_lpc_ptr->cgp.vecd.dimv));
00647 endian_tolittle32(&(cgp_lpc_ptr->cgp.vecd.nvec));
00648 endian_tolittle16(&(cgp_lpc_ptr->cgp.pventa.lvent));
00649 endian_tolittle16(&(cgp_lpc_ptr->cgp.pventa.despv));
00650 endian_tolittle32(&(cgp_lpc_ptr->preenf));
00651 endian_tolittle16(&(cgp_lpc_ptr->orden));
00652 xfwrite(cgp_lpc_ptr,sizeof(*(cgp_lpc_ptr)),1,fp);
00653 endian_fromlittle16(&(cgp_lpc_ptr->cgp.vecd.dimv));
00654 endian_fromlittle32(&(cgp_lpc_ptr->cgp.vecd.nvec));
00655 endian_fromlittle16(&(cgp_lpc_ptr->cgp.pventa.lvent));
00656 endian_fromlittle16(&(cgp_lpc_ptr->cgp.pventa.despv));
00657 endian_fromlittle32(&(cgp_lpc_ptr->preenf));
00658 endian_fromlittle16(&(cgp_lpc_ptr->orden));
00659 }
00660
00661