fork of PCE focusing on macplus, supporting DaynaPort SCSI network emulation
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

pri/text: Use the new generic text parser

+254 -553
+6
Makefile.dep
··· 3302 3302 src/config.h \ 3303 3303 src/drivers/pri/pri.h \ 3304 3304 src/drivers/psi/psi.h \ 3305 + src/lib/text.h \ 3305 3306 src/utils/pri/main.h \ 3306 3307 src/utils/pri/text.h 3307 3308 ··· 3309 3310 src/config.h \ 3310 3311 src/drivers/pri/pri.h \ 3311 3312 src/drivers/psi/psi.h \ 3313 + src/lib/text.h \ 3312 3314 src/utils/pri/main.h \ 3313 3315 src/utils/pri/text.h 3314 3316 ··· 3317 3319 src/drivers/pri/pri-enc-gcr.h \ 3318 3320 src/drivers/pri/pri.h \ 3319 3321 src/drivers/psi/psi.h \ 3322 + src/lib/text.h \ 3320 3323 src/utils/pri/main.h \ 3324 + src/utils/pri/pri-mac-gcr.h \ 3321 3325 src/utils/pri/text.h 3322 3326 3323 3327 src/utils/pri/text-raw.o: src/utils/pri/text-raw.c \ 3324 3328 src/config.h \ 3325 3329 src/drivers/pri/pri.h \ 3326 3330 src/drivers/psi/psi.h \ 3331 + src/lib/text.h \ 3327 3332 src/utils/pri/main.h \ 3328 3333 src/utils/pri/text.h 3329 3334 ··· 3331 3336 src/config.h \ 3332 3337 src/drivers/pri/pri.h \ 3333 3338 src/drivers/psi/psi.h \ 3339 + src/lib/text.h \ 3334 3340 src/utils/pri/main.h \ 3335 3341 src/utils/pri/text.h 3336 3342
+1
src/utils/pri/Makefile.inc
··· 35 35 PRI_OBJ_EXT := \ 36 36 src/lib/getopt.o \ 37 37 src/lib/sysdep.o \ 38 + src/lib/text.o \ 38 39 $(DRV_PSI_OBJ) \ 39 40 $(DRV_PRI_OBJ) 40 41
+48 -47
src/utils/pri/text-ibm-fm.c
··· 5 5 /***************************************************************************** 6 6 * File name: src/utils/pri/text-ibm-fm.c * 7 7 * Created: 2017-10-29 by Hampa Hug <hampa@hampa.ch> * 8 - * Copyright: (C) 2017-2021 Hampa Hug <hampa@hampa.ch> * 8 + * Copyright: (C) 2017-2023 Hampa Hug <hampa@hampa.ch> * 9 9 *****************************************************************************/ 10 10 11 11 /***************************************************************************** ··· 15 15 * * 16 16 * This program is distributed in the hope that it will be useful, but * 17 17 * WITHOUT ANY WARRANTY, without even the implied warranty of * 18 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * 18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * 19 19 * Public License for more details. * 20 20 *****************************************************************************/ 21 21 ··· 27 27 #include <string.h> 28 28 29 29 #include <drivers/pri/pri.h> 30 + #include <lib/text.h> 30 31 31 32 #include "main.h" 32 33 #include "text.h" ··· 83 84 84 85 if (clk != 0xff) { 85 86 if (ctx->column > 0) { 86 - fputc ('\n', ctx->fp); 87 + fputc ('\n', ctx->txt.fp); 87 88 } 88 89 89 - fprintf (ctx->fp, "%02X/%02X\n", val, clk); 90 + fprintf (ctx->txt.fp, "%02X/%02X\n", val, clk); 90 91 91 92 ctx->column = 0; 92 93 } 93 94 else { 94 95 if (ctx->column > 0) { 95 96 if (ctx->column >= 16) { 96 - fputc ('\n', ctx->fp); 97 + fputc ('\n', ctx->txt.fp); 97 98 ctx->column = 0; 98 99 } 99 100 else { 100 - fputc (' ', ctx->fp); 101 + fputc (' ', ctx->txt.fp); 101 102 } 102 103 } 103 104 104 - fprintf (ctx->fp, "%02X", val); 105 + fprintf (ctx->txt.fp, "%02X", val); 105 106 106 107 ctx->column += 1; 107 108 108 109 if (ctx->need_nl) { 109 - fputc ('\n', ctx->fp); 110 + fputc ('\n', ctx->txt.fp); 110 111 ctx->column = 0; 111 112 } 112 113 } ··· 129 130 unsigned long bit; 130 131 unsigned long type, val; 131 132 132 - fprintf (ctx->fp, "\nTRACK %lu %lu\n\n", ctx->c, ctx->h); 133 - fprintf (ctx->fp, "MODE IBM-FM\n"); 134 - fprintf (ctx->fp, "RATE %lu\n\n", pri_trk_get_clock (ctx->trk)); 133 + fprintf (ctx->txt.fp, "\nTRACK %lu %lu\n\n", ctx->c, ctx->h); 134 + fprintf (ctx->txt.fp, "MODE IBM-FM\n"); 135 + fprintf (ctx->txt.fp, "RATE %lu\n\n", pri_trk_get_clock (ctx->trk)); 135 136 136 137 ctx->shift_cnt = 0; 137 138 ctx->last_val = 0; ··· 171 172 fm_dec_flush (ctx); 172 173 173 174 if (ctx->column > 0) { 174 - fputc ('\n', ctx->fp); 175 + fputc ('\n', ctx->txt.fp); 175 176 } 176 177 177 178 return (0); ··· 240 241 { 241 242 unsigned long val; 242 243 243 - if (txt_match_uint (ctx, 16, &val) == 0) { 244 + if (txt_match_uint (&ctx->txt, 16, &val) == 0) { 244 245 return (1); 245 246 } 246 247 ··· 258 259 { 259 260 unsigned long val; 260 261 261 - while (txt_match_eol (ctx) == 0) { 262 - if (txt_match_uint (ctx, 16, &val) == 0) { 262 + while (txt_match_eol (&ctx->txt) == 0) { 263 + if (txt_match_uint (&ctx->txt, 16, &val) == 0) { 263 264 return (1); 264 265 } 265 266 ··· 306 307 static 307 308 int fm_enc_check (pri_text_t *ctx) 308 309 { 309 - if (txt_match (ctx, "START", 1)) { 310 + if (txt_match (&ctx->txt, "START", 1)) { 310 311 return (fm_enc_check_start (ctx)); 311 312 } 312 313 313 - if (txt_match (ctx, "END", 1)) { 314 + if (txt_match (&ctx->txt, "END", 1)) { 314 315 return (fm_enc_check_stop (ctx, 0)); 315 316 } 316 317 317 - if (txt_match (ctx, "SUM", 1)) { 318 + if (txt_match (&ctx->txt, "SUM", 1)) { 318 319 return (fm_enc_check_stop (ctx, 1)); 319 320 } 320 321 ··· 362 363 unsigned long max; 363 364 unsigned long val[2]; 364 365 365 - if (txt_match (ctx, "TRACK", 1)) { 366 + if (txt_match (&ctx->txt, "TRACK", 1)) { 366 367 max = 50000; 367 368 } 368 - else if (txt_match_uint (ctx, 10, &max)) { 369 + else if (txt_match_uint (&ctx->txt, 10, &max)) { 369 370 if (max <= 5000) { 370 371 max *= 16; 371 372 } ··· 374 375 return (1); 375 376 } 376 377 377 - if (txt_match_uint (ctx, 16, val) == 0) { 378 + if (txt_match_uint (&ctx->txt, 16, val) == 0) { 378 379 return (1); 379 380 } 380 381 381 - if (txt_match (ctx, "/", 1)) { 382 - if (txt_match_uint (ctx, 16, val + 1) == 0) { 382 + if (txt_match (&ctx->txt, "/", 1)) { 383 + if (txt_match_uint (&ctx->txt, 16, val + 1) == 0) { 383 384 return (1); 384 385 } 385 386 } ··· 406 407 { 407 408 unsigned long cnt; 408 409 409 - if (txt_match_uint (ctx, 10, &cnt) == 0) { 410 + if (txt_match_uint (&ctx->txt, 10, &cnt) == 0) { 410 411 return (1); 411 412 } 412 413 ··· 425 426 426 427 clk = 0xff; 427 428 428 - if (txt_match (ctx, "/", 1)) { 429 - if (txt_match_uint (ctx, 16, &tmp) == 0) { 429 + if (txt_match (&ctx->txt, "/", 1)) { 430 + if (txt_match_uint (&ctx->txt, 16, &tmp) == 0) { 430 431 return (1); 431 432 } 432 433 ··· 445 446 { 446 447 unsigned long gap4a, gap1; 447 448 448 - if (txt_match_uint (ctx, 10, &gap4a) == 0) { 449 + if (txt_match_uint (&ctx->txt, 10, &gap4a) == 0) { 449 450 return (1); 450 451 } 451 452 452 - if (txt_match_uint (ctx, 10, &gap1) == 0) { 453 + if (txt_match_uint (&ctx->txt, 10, &gap1) == 0) { 453 454 return (1); 454 455 } 455 456 ··· 492 493 unsigned long cnt; 493 494 unsigned long val[2]; 494 495 495 - if (txt_match_uint (ctx, 10, &cnt) == 0) { 496 + if (txt_match_uint (&ctx->txt, 10, &cnt) == 0) { 496 497 return (1); 497 498 } 498 499 499 - if (txt_match_uint (ctx, 16, val) == 0) { 500 + if (txt_match_uint (&ctx->txt, 16, val) == 0) { 500 501 return (1); 501 502 } 502 503 503 - if (txt_match (ctx, "/", 1)) { 504 - if (txt_match_uint (ctx, 16, val + 1) == 0) { 504 + if (txt_match (&ctx->txt, "/", 1)) { 505 + if (txt_match_uint (&ctx->txt, 16, val + 1) == 0) { 505 506 return (1); 506 507 } 507 508 } ··· 529 530 unsigned char id[4]; 530 531 531 532 for (i = 0; i < 4; i++) { 532 - if (txt_match_uint (ctx, 16, &val) == 0) { 533 + if (txt_match_uint (&ctx->txt, 16, &val) == 0) { 533 534 return (1); 534 535 } 535 536 ··· 588 589 { 589 590 unsigned long val; 590 591 591 - if (txt_match (ctx, "AM", 1)) { 592 + if (txt_match (&ctx->txt, "AM", 1)) { 592 593 return (fm_enc_am (ctx)); 593 594 } 594 - else if (txt_match (ctx, "BIT", 1)) { 595 + else if (txt_match (&ctx->txt, "BIT", 1)) { 595 596 return (fm_enc_bit (ctx)); 596 597 } 597 - else if (txt_match (ctx, "CHECK", 1)) { 598 + else if (txt_match (&ctx->txt, "CHECK", 1)) { 598 599 return (fm_enc_check (ctx)); 599 600 } 600 - else if (txt_match (ctx, "CRC", 1)) { 601 + else if (txt_match (&ctx->txt, "CRC", 1)) { 601 602 return (fm_enc_check_stop (ctx, 1)); 602 603 } 603 - else if (txt_match (ctx, "DAM", 1)) { 604 + else if (txt_match (&ctx->txt, "DAM", 1)) { 604 605 return (fm_enc_dam (ctx)); 605 606 } 606 - else if (txt_match (ctx, "EOT", 1)) { 607 + else if (txt_match (&ctx->txt, "EOT", 1)) { 607 608 return (fm_enc_eot (ctx)); 608 609 } 609 - else if (txt_match (ctx, "FILL", 1)) { 610 + else if (txt_match (&ctx->txt, "FILL", 1)) { 610 611 return (fm_enc_fill (ctx)); 611 612 } 612 - else if (txt_match (ctx, "GAP", 1)) { 613 + else if (txt_match (&ctx->txt, "GAP", 1)) { 613 614 return (fm_enc_gap (ctx)); 614 615 } 615 - else if (txt_match (ctx, "IAM", 1)) { 616 + else if (txt_match (&ctx->txt, "IAM", 1)) { 616 617 return (fm_enc_iam (ctx)); 617 618 } 618 - else if (txt_match (ctx, "IDAM", 1)) { 619 + else if (txt_match (&ctx->txt, "IDAM", 1)) { 619 620 return (fm_enc_idam (ctx)); 620 621 } 621 - else if (txt_match (ctx, "REP", 1)) { 622 + else if (txt_match (&ctx->txt, "REP", 1)) { 622 623 return (fm_enc_rep (ctx)); 623 624 } 624 - else if (txt_match (ctx, "SECT", 1)) { 625 + else if (txt_match (&ctx->txt, "SECT", 1)) { 625 626 return (fm_enc_sect (ctx)); 626 627 } 627 - else if (txt_match (ctx, "SYNC", 1)) { 628 + else if (txt_match (&ctx->txt, "SYNC", 1)) { 628 629 return (txt_fm_enc_sync (ctx)); 629 630 } 630 - else if (txt_match_uint (ctx, 16, &val)) { 631 + else if (txt_match_uint (&ctx->txt, 16, &val)) { 631 632 return (fm_enc_hex (ctx, val)); 632 633 } 633 634
+49 -48
src/utils/pri/text-ibm-mfm.c
··· 5 5 /***************************************************************************** 6 6 * File name: src/utils/pri/text-ibm-mfm.c * 7 7 * Created: 2017-10-29 by Hampa Hug <hampa@hampa.ch> * 8 - * Copyright: (C) 2017-2021 Hampa Hug <hampa@hampa.ch> * 8 + * Copyright: (C) 2017-2023 Hampa Hug <hampa@hampa.ch> * 9 9 *****************************************************************************/ 10 10 11 11 /***************************************************************************** ··· 15 15 * * 16 16 * This program is distributed in the hope that it will be useful, but * 17 17 * WITHOUT ANY WARRANTY, without even the implied warranty of * 18 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * 18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * 19 19 * Public License for more details. * 20 20 *****************************************************************************/ 21 21 ··· 27 27 #include <string.h> 28 28 29 29 #include <drivers/pri/pri.h> 30 + #include <lib/text.h> 30 31 31 32 #include "main.h" 32 33 #include "text.h" ··· 89 90 90 91 if (clk1 ^ clk2) { 91 92 if (ctx->column > 0) { 92 - fputc ('\n', ctx->fp); 93 + fputc ('\n', ctx->txt.fp); 93 94 } 94 95 95 - fprintf (ctx->fp, "%02X/%02X\n", data, clk1 ^ clk2); 96 + fprintf (ctx->txt.fp, "%02X/%02X\n", data, clk1 ^ clk2); 96 97 97 98 ctx->column = 0; 98 99 } 99 100 else { 100 101 if (ctx->column > 0) { 101 102 if (ctx->column >= 16) { 102 - fputc ('\n', ctx->fp); 103 + fputc ('\n', ctx->txt.fp); 103 104 ctx->column = 0; 104 105 } 105 106 else { 106 - fputc (' ', ctx->fp); 107 + fputc (' ', ctx->txt.fp); 107 108 } 108 109 } 109 110 110 - fprintf (ctx->fp, "%02X", data); 111 + fprintf (ctx->txt.fp, "%02X", data); 111 112 112 113 ctx->column += 1; 113 114 114 115 if (ctx->need_nl) { 115 - fputc ('\n', ctx->fp); 116 + fputc ('\n', ctx->txt.fp); 116 117 ctx->column = 0; 117 118 } 118 119 } ··· 168 169 unsigned long bit, align; 169 170 unsigned long type, val; 170 171 171 - fprintf (ctx->fp, "\nTRACK %lu %lu\n\n", ctx->c, ctx->h); 172 - fprintf (ctx->fp, "MODE IBM-MFM\n"); 173 - fprintf (ctx->fp, "RATE %lu\n\n", pri_trk_get_clock (ctx->trk)); 172 + fprintf (ctx->txt.fp, "\nTRACK %lu %lu\n\n", ctx->c, ctx->h); 173 + fprintf (ctx->txt.fp, "MODE IBM-MFM\n"); 174 + fprintf (ctx->txt.fp, "RATE %lu\n\n", pri_trk_get_clock (ctx->trk)); 174 175 175 176 if (par_text_align) { 176 177 align = mfm_dec_align (ctx); ··· 220 221 mfm_dec_flush (ctx); 221 222 222 223 if (ctx->column > 0) { 223 - fputc ('\n', ctx->fp); 224 + fputc ('\n', ctx->txt.fp); 224 225 } 225 226 226 227 return (0); ··· 322 323 { 323 324 unsigned long val; 324 325 325 - if (txt_match_uint (ctx, 16, &val) == 0) { 326 + if (txt_match_uint (&ctx->txt, 16, &val) == 0) { 326 327 return (1); 327 328 } 328 329 ··· 338 339 { 339 340 unsigned long val; 340 341 341 - while (txt_match_eol (ctx) == 0) { 342 - if (txt_match_uint (ctx, 16, &val) == 0) { 342 + while (txt_match_eol (&ctx->txt) == 0) { 343 + if (txt_match_uint (&ctx->txt, 16, &val) == 0) { 343 344 return (1); 344 345 } 345 346 ··· 386 387 static 387 388 int mfm_enc_check (pri_text_t *ctx) 388 389 { 389 - if (txt_match (ctx, "START", 1)) { 390 + if (txt_match (&ctx->txt, "START", 1)) { 390 391 return (mfm_enc_check_start (ctx)); 391 392 } 392 393 393 - if (txt_match (ctx, "END", 1)) { 394 + if (txt_match (&ctx->txt, "END", 1)) { 394 395 return (mfm_enc_check_stop (ctx, 0)); 395 396 } 396 397 397 - if (txt_match (ctx, "SUM", 1)) { 398 + if (txt_match (&ctx->txt, "SUM", 1)) { 398 399 return (mfm_enc_check_stop (ctx, 1)); 399 400 } 400 401 ··· 447 448 unsigned long max; 448 449 unsigned long val[2]; 449 450 450 - if (txt_match (ctx, "TRACK", 1)) { 451 + if (txt_match (&ctx->txt, "TRACK", 1)) { 451 452 if (pri_trk_get_clock (ctx->trk) < 750000) { 452 453 max = 100000; 453 454 } ··· 455 456 max = 200000; 456 457 } 457 458 } 458 - else if (txt_match_uint (ctx, 10, &max)) { 459 + else if (txt_match_uint (&ctx->txt, 10, &max)) { 459 460 if (max <= 15000) { 460 461 max *= 16; 461 462 } ··· 464 465 return (1); 465 466 } 466 467 467 - if (txt_match_uint (ctx, 16, val) == 0) { 468 + if (txt_match_uint (&ctx->txt, 16, val) == 0) { 468 469 return (1); 469 470 } 470 471 471 - if (txt_match (ctx, "/", 1)) { 472 - if (txt_match_uint (ctx, 16, val + 1) == 0) { 472 + if (txt_match (&ctx->txt, "/", 1)) { 473 + if (txt_match_uint (&ctx->txt, 16, val + 1) == 0) { 473 474 return (1); 474 475 } 475 476 } ··· 496 497 { 497 498 unsigned long cnt; 498 499 499 - if (txt_match_uint (ctx, 10, &cnt) == 0) { 500 + if (txt_match_uint (&ctx->txt, 10, &cnt) == 0) { 500 501 return (1); 501 502 } 502 503 ··· 515 516 516 517 clk = 0; 517 518 518 - if (txt_match (ctx, "/", 1)) { 519 - if (txt_match_uint (ctx, 16, &tmp) == 0) { 519 + if (txt_match (&ctx->txt, "/", 1)) { 520 + if (txt_match_uint (&ctx->txt, 16, &tmp) == 0) { 520 521 return (1); 521 522 } 522 523 ··· 535 536 { 536 537 unsigned long gap4a, gap1; 537 538 538 - if (txt_match_uint (ctx, 10, &gap4a) == 0) { 539 + if (txt_match_uint (&ctx->txt, 10, &gap4a) == 0) { 539 540 return (1); 540 541 } 541 542 542 - if (txt_match_uint (ctx, 10, &gap1) == 0) { 543 + if (txt_match_uint (&ctx->txt, 10, &gap1) == 0) { 543 544 return (1); 544 545 } 545 546 ··· 586 587 unsigned long cnt; 587 588 unsigned long val[2]; 588 589 589 - if (txt_match_uint (ctx, 10, &cnt) == 0) { 590 + if (txt_match_uint (&ctx->txt, 10, &cnt) == 0) { 590 591 return (1); 591 592 } 592 593 593 - if (txt_match_uint (ctx, 16, val) == 0) { 594 + if (txt_match_uint (&ctx->txt, 16, val) == 0) { 594 595 return (1); 595 596 } 596 597 597 - if (txt_match (ctx, "/", 1)) { 598 - if (txt_match_uint (ctx, 16, val + 1) == 0) { 598 + if (txt_match (&ctx->txt, "/", 1)) { 599 + if (txt_match_uint (&ctx->txt, 16, val + 1) == 0) { 599 600 return (1); 600 601 } 601 602 } ··· 632 633 unsigned long val; 633 634 unsigned char id[4]; 634 635 635 - if (txt_match (ctx, "END", 1)) { 636 + if (txt_match (&ctx->txt, "END", 1)) { 636 637 return (mfm_enc_sect_end (ctx)); 637 638 } 638 639 639 640 for (i = 0; i < 4; i++) { 640 - if (txt_match_uint (ctx, 16, &val) == 0) { 641 + if (txt_match_uint (&ctx->txt, 16, &val) == 0) { 641 642 return (1); 642 643 } 643 644 ··· 702 703 { 703 704 unsigned long val; 704 705 705 - if (txt_match (ctx, "AM", 1)) { 706 + if (txt_match (&ctx->txt, "AM", 1)) { 706 707 return (mfm_enc_am (ctx)); 707 708 } 708 - else if (txt_match (ctx, "BIT", 1)) { 709 + else if (txt_match (&ctx->txt, "BIT", 1)) { 709 710 return (mfm_enc_bit (ctx)); 710 711 } 711 - else if (txt_match (ctx, "CHECK", 1)) { 712 + else if (txt_match (&ctx->txt, "CHECK", 1)) { 712 713 return (mfm_enc_check (ctx)); 713 714 } 714 - else if (txt_match (ctx, "CRC", 1)) { 715 + else if (txt_match (&ctx->txt, "CRC", 1)) { 715 716 return (mfm_enc_check_stop (ctx, 1)); 716 717 } 717 - else if (txt_match (ctx, "DAM", 1)) { 718 + else if (txt_match (&ctx->txt, "DAM", 1)) { 718 719 return (mfm_enc_dam (ctx)); 719 720 } 720 - else if (txt_match (ctx, "EOT", 1)) { 721 + else if (txt_match (&ctx->txt, "EOT", 1)) { 721 722 return (mfm_enc_eot (ctx)); 722 723 } 723 - else if (txt_match (ctx, "FILL", 1)) { 724 + else if (txt_match (&ctx->txt, "FILL", 1)) { 724 725 return (mfm_enc_fill (ctx)); 725 726 } 726 - else if (txt_match (ctx, "GAP", 1)) { 727 + else if (txt_match (&ctx->txt, "GAP", 1)) { 727 728 return (mfm_enc_gap (ctx)); 728 729 } 729 - else if (txt_match (ctx, "IAM", 1)) { 730 + else if (txt_match (&ctx->txt, "IAM", 1)) { 730 731 return (mfm_enc_iam (ctx)); 731 732 } 732 - else if (txt_match (ctx, "IDAM", 1)) { 733 + else if (txt_match (&ctx->txt, "IDAM", 1)) { 733 734 return (mfm_enc_idam (ctx)); 734 735 } 735 - else if (txt_match (ctx, "REP", 1)) { 736 + else if (txt_match (&ctx->txt, "REP", 1)) { 736 737 return (mfm_enc_rep (ctx)); 737 738 } 738 - else if (txt_match (ctx, "SECT", 1)) { 739 + else if (txt_match (&ctx->txt, "SECT", 1)) { 739 740 return (mfm_enc_sect (ctx)); 740 741 } 741 - else if (txt_match (ctx, "SYNC", 1)) { 742 + else if (txt_match (&ctx->txt, "SYNC", 1)) { 742 743 return (mfm_enc_sync (ctx)); 743 744 } 744 - else if (txt_match_uint (ctx, 16, &val)) { 745 + else if (txt_match_uint (&ctx->txt, 16, &val)) { 745 746 return (mfm_enc_hex (ctx, val)); 746 747 } 747 748
+66 -60
src/utils/pri/text-mac-gcr.c
··· 29 29 #include <drivers/pri/pri.h> 30 30 #include <drivers/pri/pri-enc-gcr.h> 31 31 32 + #include <lib/text.h> 33 + 32 34 #include "main.h" 33 35 #include "pri-mac-gcr.h" 34 36 #include "text.h" ··· 93 95 void mac_put_nl (pri_text_t *ctx, int force) 94 96 { 95 97 if ((ctx->column > 0) || force) { 96 - fputc ('\n', ctx->fp); 98 + fputc ('\n', ctx->txt.fp); 97 99 } 98 100 99 101 ctx->column = 0; ··· 104 106 { 105 107 if (ctx->column > 0) { 106 108 if (ctx->column >= 16) { 107 - fputc ('\n', ctx->fp); 109 + fputc ('\n', ctx->txt.fp); 108 110 ctx->column = 0; 109 111 } 110 112 else { 111 - fputc (' ', ctx->fp); 113 + fputc (' ', ctx->txt.fp); 112 114 } 113 115 } 114 116 115 - fprintf (ctx->fp, "%02X", val & 0xff); 117 + fprintf (ctx->txt.fp, "%02X", val & 0xff); 116 118 117 119 ctx->column += 1; 118 120 } ··· 180 182 if (group_cnt > 0) { 181 183 mac_flush (ctx); 182 184 mac_put_nl (ctx, 0); 183 - fprintf (ctx->fp, "SYNC GROUP %u\n", group_cnt); 185 + fprintf (ctx->txt.fp, "SYNC GROUP %u\n", group_cnt); 184 186 } 185 187 186 188 mac_dec_events (ctx); ··· 192 194 if (sync_cnt > 0) { 193 195 mac_flush (ctx); 194 196 mac_put_nl (ctx, 0); 195 - fprintf (ctx->fp, "SYNC %u\n", sync_cnt); 197 + fprintf (ctx->txt.fp, "SYNC %u\n", sync_cnt); 196 198 } 197 199 } while ((group_cnt > 0) || (sync_cnt > 0)); 198 200 ··· 227 229 c = (dec[0] & 0x3f) | ((dec[2] & 0x1f) << 6); 228 230 h = (dec[2] >> 5) & 1; 229 231 230 - fprintf (ctx->fp, 232 + fprintf (ctx->txt.fp, 231 233 " %02X %02X %02X %02X %02X %02X %02X %02X" 232 234 "\t# SECT %02X %02X %02X %02X\n", 233 235 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], ··· 311 313 return (1); 312 314 } 313 315 314 - fprintf (ctx->fp, " %02X\n", s); 315 - fprintf (ctx->fp, "#CHECK START\n"); 316 + fprintf (ctx->txt.fp, " %02X\n", s); 317 + fprintf (ctx->txt.fp, "#CHECK START\n"); 316 318 317 319 for (i = 0; i < 699; i++) { 318 320 j = i & 15; 319 321 320 322 if (j > 0) { 321 - fputc (' ', ctx->fp); 323 + fputc (' ', ctx->txt.fp); 322 324 } 323 325 324 - fprintf (ctx->fp, "%02X", buf[i]); 326 + fprintf (ctx->txt.fp, "%02X", buf[i]); 325 327 326 328 if (j == 15) { 327 - fputc ('\n', ctx->fp); 329 + fputc ('\n', ctx->txt.fp); 328 330 } 329 331 } 330 332 331 - fprintf (ctx->fp, "\n#CHECK END\n"); 332 - fprintf (ctx->fp, "%02X %02X %02X %02X\n", chk[0], chk[1], chk[2], chk[3]); 333 + fprintf (ctx->txt.fp, "\n#CHECK END\n"); 334 + fprintf (ctx->txt.fp, "%02X %02X %02X %02X\n", 335 + chk[0], chk[1], chk[2], chk[3] 336 + ); 333 337 334 338 ctx->column = 0; 335 339 ··· 401 405 402 406 pri_mac_gcr_checksum (buf, buf, 0); 403 407 404 - fprintf (ctx->fp, " %02X\n", s); 405 - fprintf (ctx->fp, "CHECK START\n"); 406 - fprintf (ctx->fp, "%02X", buf[0]); 408 + fprintf (ctx->txt.fp, " %02X\n", s); 409 + fprintf (ctx->txt.fp, "CHECK START\n"); 410 + fprintf (ctx->txt.fp, "%02X", buf[0]); 407 411 408 412 for (i = 1; i < 12; i++) { 409 - fprintf (ctx->fp, " %02X", buf[i]); 413 + fprintf (ctx->txt.fp, " %02X", buf[i]); 410 414 } 411 415 412 - fputc ('\n', ctx->fp); 416 + fputc ('\n', ctx->txt.fp); 413 417 414 418 if (mac_check_data (buf + 12, 512)) { 415 - fprintf (ctx->fp, "REP 512 %02X\n", buf[12]); 419 + fprintf (ctx->txt.fp, "REP 512 %02X\n", buf[12]); 416 420 } 417 421 else { 418 422 for (i = 0; i < 512; i++) { 419 423 j = i & 15; 420 424 421 425 if (j > 0) { 422 - fputc (' ', ctx->fp); 426 + fputc (' ', ctx->txt.fp); 423 427 } 424 428 425 - fprintf (ctx->fp, "%02X", buf[i + 12]); 429 + fprintf (ctx->txt.fp, "%02X", buf[i + 12]); 426 430 427 431 if (j == 15) { 428 - fputc ('\n', ctx->fp); 432 + fputc ('\n', ctx->txt.fp); 429 433 } 430 434 } 431 435 } 432 436 433 - fprintf (ctx->fp, "CHECK END\n"); 434 - fprintf (ctx->fp, "%02X %02X %02X %02X\n", chk[0], chk[1], chk[2], chk[3]); 437 + fprintf (ctx->txt.fp, "CHECK END\n"); 438 + fprintf (ctx->txt.fp, "%02X %02X %02X %02X\n", 439 + chk[0], chk[1], chk[2], chk[3] 440 + ); 435 441 436 442 ctx->column = 0; 437 443 ··· 478 484 rot -= (long) trk->size; 479 485 } 480 486 481 - fprintf (ctx->fp, "ROTATE %ld\n\n", rot); 487 + fprintf (ctx->txt.fp, "ROTATE %ld\n\n", rot); 482 488 483 489 pri_trk_rotate (trk, pos); 484 490 ··· 494 500 unsigned long bit; 495 501 unsigned char buf[3]; 496 502 497 - fprintf (ctx->fp, "\nTRACK %lu %lu\n\n", ctx->c, ctx->h); 498 - fprintf (ctx->fp, "MODE MAC-GCR\n"); 499 - fprintf (ctx->fp, "RATE %lu\n\n", pri_trk_get_clock (ctx->trk)); 503 + fprintf (ctx->txt.fp, "\nTRACK %lu %lu\n\n", ctx->c, ctx->h); 504 + fprintf (ctx->txt.fp, "MODE MAC-GCR\n"); 505 + fprintf (ctx->txt.fp, "RATE %lu\n\n", pri_trk_get_clock (ctx->trk)); 500 506 501 507 ctx->shift = 0; 502 508 ctx->shift_cnt = 0; ··· 630 636 { 631 637 unsigned long val; 632 638 633 - while (txt_match_eol (ctx) == 0) { 634 - if (txt_match_uint (ctx, 16, &val) == 0) { 639 + while (txt_match_eol (&ctx->txt) == 0) { 640 + if (txt_match_uint (&ctx->txt, 16, &val) == 0) { 635 641 return (1); 636 642 } 637 643 ··· 740 746 static 741 747 int mac_enc_check (pri_text_t *ctx) 742 748 { 743 - if (txt_match (ctx, "START", 1)) { 749 + if (txt_match (&ctx->txt, "START", 1)) { 744 750 return (mac_enc_check_start (ctx)); 745 751 } 746 752 747 - if (txt_match (ctx, "END", 1)) { 753 + if (txt_match (&ctx->txt, "END", 1)) { 748 754 return (mac_enc_check_stop (ctx, 0)); 749 755 } 750 756 751 - if (txt_match (ctx, "SUM", 1)) { 757 + if (txt_match (&ctx->txt, "SUM", 1)) { 752 758 return (mac_enc_check_stop (ctx, 3)); 753 759 } 754 760 ··· 833 839 { 834 840 unsigned long max, val; 835 841 836 - if (txt_match (ctx, "TRACK", 1)) { 842 + if (txt_match (&ctx->txt, "TRACK", 1)) { 837 843 max = pri_get_mac_gcr_track_length (ctx->c); 838 844 } 839 845 else { 840 - if (txt_match_uint (ctx, 10, &max) == 0) { 846 + if (txt_match_uint (&ctx->txt, 10, &max) == 0) { 841 847 return (1); 842 848 } 843 849 } 844 850 845 - if (txt_match (ctx, "SYNC", 1)) { 846 - if (txt_match (ctx, "GROUP", 1)) { 851 + if (txt_match (&ctx->txt, "SYNC", 1)) { 852 + if (txt_match (&ctx->txt, "GROUP", 1)) { 847 853 return (mac_enc_fill_sync_group (ctx, max)); 848 854 } 849 855 ··· 854 860 return (0); 855 861 } 856 862 857 - if (txt_match_uint (ctx, 16, &val) == 0) { 863 + if (txt_match_uint (&ctx->txt, 16, &val) == 0) { 858 864 return (1); 859 865 } 860 866 ··· 898 904 { 899 905 unsigned long bits; 900 906 901 - if (txt_match (ctx, "/", 1)) { 902 - if (txt_match_uint (ctx, 10, &bits) == 0) { 907 + if (txt_match (&ctx->txt, "/", 1)) { 908 + if (txt_match_uint (&ctx->txt, 10, &bits) == 0) { 903 909 return (1); 904 910 } 905 911 ··· 958 964 unsigned long cnt; 959 965 unsigned long val; 960 966 961 - if (txt_match_uint (ctx, 10, &cnt) == 0) { 967 + if (txt_match_uint (&ctx->txt, 10, &cnt) == 0) { 962 968 return (1); 963 969 } 964 970 965 - if (txt_match_uint (ctx, 16, &val)) { 971 + if (txt_match_uint (&ctx->txt, 16, &val)) { 966 972 return (mac_enc_hex (ctx, val, cnt)); 967 973 } 968 974 ··· 991 997 unsigned char buf[4]; 992 998 unsigned char enc[5]; 993 999 994 - if (txt_match (ctx, "END", 1)) { 1000 + if (txt_match (&ctx->txt, "END", 1)) { 995 1001 return (mac_enc_sect_end (ctx)); 996 1002 } 997 1003 998 1004 for (i = 0; i < 4; i++) { 999 - if (txt_match_uint (ctx, 16, &val) == 0) { 1005 + if (txt_match_uint (&ctx->txt, 16, &val) == 0) { 1000 1006 return (1); 1001 1007 } 1002 1008 ··· 1053 1059 { 1054 1060 unsigned long cnt; 1055 1061 1056 - if (txt_match_uint (ctx, 10, &cnt) == 0) { 1062 + if (txt_match_uint (&ctx->txt, 10, &cnt) == 0) { 1057 1063 return (1); 1058 1064 } 1059 1065 ··· 1077 1083 { 1078 1084 unsigned long cnt; 1079 1085 1080 - if (txt_match (ctx, "GROUP", 1)) { 1086 + if (txt_match (&ctx->txt, "GROUP", 1)) { 1081 1087 return (mac_enc_sync_group (ctx)); 1082 1088 } 1083 1089 1084 - if (txt_match_uint (ctx, 10, &cnt) == 0) { 1090 + if (txt_match_uint (&ctx->txt, 10, &cnt) == 0) { 1085 1091 return (1); 1086 1092 } 1087 1093 ··· 1100 1106 { 1101 1107 unsigned long val; 1102 1108 1103 - if (txt_match (ctx, "BIT", 1)) { 1109 + if (txt_match (&ctx->txt, "BIT", 1)) { 1104 1110 return (mac_enc_bit (ctx)); 1105 1111 } 1106 - else if (txt_match (ctx, "CHECK", 1)) { 1112 + else if (txt_match (&ctx->txt, "CHECK", 1)) { 1107 1113 return (mac_enc_check (ctx)); 1108 1114 } 1109 - else if (txt_match (ctx, "EOTG", 1)) { 1115 + else if (txt_match (&ctx->txt, "EOTG", 1)) { 1110 1116 return (mac_enc_eotg (ctx)); 1111 1117 } 1112 - else if (txt_match (ctx, "EOTS", 1)) { 1118 + else if (txt_match (&ctx->txt, "EOTS", 1)) { 1113 1119 return (mac_enc_eots (ctx)); 1114 1120 } 1115 - else if (txt_match (ctx, "EOT", 1)) { 1121 + else if (txt_match (&ctx->txt, "EOT", 1)) { 1116 1122 return (mac_enc_eotg (ctx)); 1117 1123 } 1118 - else if (txt_match (ctx, "FILL", 1)) { 1124 + else if (txt_match (&ctx->txt, "FILL", 1)) { 1119 1125 return (mac_enc_fill (ctx)); 1120 1126 } 1121 - else if (txt_match (ctx, "REP", 1)) { 1127 + else if (txt_match (&ctx->txt, "REP", 1)) { 1122 1128 return (mac_enc_rep (ctx)); 1123 1129 } 1124 - else if (txt_match (ctx, "SECT", 1)) { 1130 + else if (txt_match (&ctx->txt, "SECT", 1)) { 1125 1131 return (mac_enc_sect (ctx)); 1126 1132 } 1127 - else if (txt_match (ctx, "SYNC", 1)) { 1133 + else if (txt_match (&ctx->txt, "SYNC", 1)) { 1128 1134 return (mac_enc_sync (ctx)); 1129 1135 } 1130 - else if (txt_match (ctx, "<", 1)) { 1136 + else if (txt_match (&ctx->txt, "<", 1)) { 1131 1137 return (mac_enc_nibble_start (ctx)); 1132 1138 } 1133 - else if (txt_match (ctx, ">", 1)) { 1139 + else if (txt_match (&ctx->txt, ">", 1)) { 1134 1140 return (mac_enc_nibble_stop (ctx)); 1135 1141 } 1136 - else if (txt_match_uint (ctx, 16, &val)) { 1142 + else if (txt_match_uint (&ctx->txt, 16, &val)) { 1137 1143 return (mac_enc_hex (ctx, val, 1)); 1138 1144 } 1139 1145
+21 -19
src/utils/pri/text-raw.c
··· 5 5 /***************************************************************************** 6 6 * File name: src/utils/pri/text-raw.c * 7 7 * Created: 2017-10-29 by Hampa Hug <hampa@hampa.ch> * 8 - * Copyright: (C) 2017-2018 Hampa Hug <hampa@hampa.ch> * 8 + * Copyright: (C) 2017-2023 Hampa Hug <hampa@hampa.ch> * 9 9 *****************************************************************************/ 10 10 11 11 /***************************************************************************** ··· 15 15 * * 16 16 * This program is distributed in the hope that it will be useful, but * 17 17 * WITHOUT ANY WARRANTY, without even the implied warranty of * 18 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * 18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * 19 19 * Public License for more details. * 20 20 *****************************************************************************/ 21 21 ··· 28 28 29 29 #include <drivers/pri/pri.h> 30 30 31 + #include <lib/text.h> 32 + 31 33 #include "main.h" 32 34 #include "text.h" 33 35 ··· 44 46 45 47 if (ctx->column > 0) { 46 48 if (ctx->column >= 16) { 47 - fputc ('\n', ctx->fp); 49 + fputc ('\n', ctx->txt.fp); 48 50 ctx->column = 0; 49 51 } 50 52 else { 51 - fputc (' ', ctx->fp); 53 + fputc (' ', ctx->txt.fp); 52 54 } 53 55 } 54 56 55 - fprintf (ctx->fp, "%02X", val); 57 + fprintf (ctx->txt.fp, "%02X", val); 56 58 57 59 ctx->column += 1; 58 60 } ··· 73 75 unsigned long bit; 74 76 unsigned long type, val; 75 77 76 - fprintf (ctx->fp, "TRACK %lu %lu\n\n", ctx->c, ctx->h); 77 - fprintf (ctx->fp, "MODE RAW\n"); 78 - fprintf (ctx->fp, "RATE %lu\n\n", pri_trk_get_clock (ctx->trk)); 78 + fprintf (ctx->txt.fp, "TRACK %lu %lu\n\n", ctx->c, ctx->h); 79 + fprintf (ctx->txt.fp, "MODE RAW\n"); 80 + fprintf (ctx->txt.fp, "RATE %lu\n\n", pri_trk_get_clock (ctx->trk)); 79 81 80 82 ctx->shift_cnt = 0; 81 83 ctx->last_val = 0; ··· 104 106 raw_dec_flush (ctx); 105 107 106 108 if (ctx->column > 0) { 107 - fputc ('\n', ctx->fp); 109 + fputc ('\n', ctx->txt.fp); 108 110 } 109 111 110 112 return (0); ··· 115 117 { 116 118 unsigned long val; 117 119 118 - while (txt_match_eol (ctx) == 0) { 119 - if (txt_match_uint (ctx, 16, &val) == 0) { 120 + while (txt_match_eol (&ctx->txt) == 0) { 121 + if (txt_match_uint (&ctx->txt, 16, &val) == 0) { 120 122 return (1); 121 123 } 122 124 ··· 138 140 unsigned long max; 139 141 unsigned long val; 140 142 141 - if (txt_match_uint (ctx, 10, &max) == 0) { 143 + if (txt_match_uint (&ctx->txt, 10, &max) == 0) { 142 144 return (1); 143 145 } 144 146 145 - if (txt_match_uint (ctx, 16, &val) == 0) { 147 + if (txt_match_uint (&ctx->txt, 16, &val) == 0) { 146 148 return (1); 147 149 } 148 150 ··· 173 175 unsigned long cnt; 174 176 unsigned long val; 175 177 176 - if (txt_match_uint (ctx, 10, &cnt) == 0) { 178 + if (txt_match_uint (&ctx->txt, 10, &cnt) == 0) { 177 179 return (1); 178 180 } 179 181 180 - if (txt_match_uint (ctx, 16, &val) == 0) { 182 + if (txt_match_uint (&ctx->txt, 16, &val) == 0) { 181 183 return (1); 182 184 } 183 185 ··· 196 198 { 197 199 unsigned long val; 198 200 199 - if (txt_match (ctx, "BIT", 1)) { 201 + if (txt_match (&ctx->txt, "BIT", 1)) { 200 202 return (raw_enc_bit (ctx)); 201 203 } 202 - else if (txt_match (ctx, "FILL", 1)) { 204 + else if (txt_match (&ctx->txt, "FILL", 1)) { 203 205 return (raw_enc_fill (ctx)); 204 206 } 205 - else if (txt_match (ctx, "REP", 1)) { 207 + else if (txt_match (&ctx->txt, "REP", 1)) { 206 208 return (raw_enc_rep (ctx)); 207 209 } 208 - else if (txt_match_uint (ctx, 16, &val)) { 210 + else if (txt_match_uint (&ctx->txt, 16, &val)) { 209 211 return (raw_enc_hex (ctx, val)); 210 212 } 211 213
+61 -367
src/utils/pri/text.c
··· 27 27 #include <string.h> 28 28 29 29 #include <drivers/pri/pri.h> 30 + #include <lib/text.h> 30 31 31 32 #include "main.h" 32 33 #include "text.h" 33 34 34 - 35 - static 36 - void txt_init (pri_text_t *ctx, FILE *fp) 37 - { 38 - memset (ctx, 0, sizeof (pri_text_t)); 39 - 40 - ctx->fp = fp; 41 - } 42 35 43 36 void txt_save_pos (const pri_text_t *ctx, pri_text_pos_t *pos) 44 37 { ··· 134 127 ctx->shift_cnt -= cnt; 135 128 136 129 if (ctx->column > 0) { 137 - fputc ('\n', ctx->fp); 130 + fputc ('\n', ctx->txt.fp); 138 131 } 139 132 140 - fprintf (ctx->fp, "RAW"); 133 + fprintf (ctx->txt.fp, "RAW"); 141 134 142 135 for (i = 0; i < cnt; i++) { 143 - fprintf (ctx->fp, " %lu", (val >> (cnt - i - 1)) & 1); 136 + fprintf (ctx->txt.fp, " %lu", (val >> (cnt - i - 1)) & 1); 144 137 } 145 138 146 - fprintf (ctx->fp, "\n"); 139 + fprintf (ctx->txt.fp, "\n"); 147 140 148 141 ctx->column = 0; 149 142 ··· 153 146 void txt_dec_event (pri_text_t *ctx, unsigned long type, unsigned long val) 154 147 { 155 148 if (ctx->column > 0) { 156 - fputc ('\n', ctx->fp); 149 + fputc ('\n', ctx->txt.fp); 157 150 ctx->column = 0; 158 151 ctx->need_nl = 0; 159 152 } 160 153 161 154 if (type == PRI_EVENT_WEAK) { 162 - fprintf (ctx->fp, "WEAK %08lX\n", val); 155 + fprintf (ctx->txt.fp, "WEAK %08lX\n", val); 163 156 } 164 157 else if (type == PRI_EVENT_CLOCK) { 165 158 unsigned long long tmp; ··· 167 160 tmp = pri_trk_get_clock (ctx->trk); 168 161 tmp = (tmp * val + 32768) / 65536; 169 162 170 - fprintf (ctx->fp, "CLOCK %lu\n", (unsigned long) tmp); 163 + fprintf (ctx->txt.fp, "CLOCK %lu\n", (unsigned long) tmp); 171 164 } 172 165 else { 173 - fprintf (ctx->fp, "EVENT %08lX %08lX\n", type, val); 166 + fprintf (ctx->txt.fp, "EVENT %08lX %08lX\n", type, val); 174 167 } 175 168 } 176 169 177 170 static 178 171 void txt_dec_init (pri_text_t *ctx, FILE *fp, pri_img_t *img, pri_trk_t *trk, unsigned long c, unsigned long h) 179 172 { 180 - txt_init (ctx, fp); 173 + txt_init (&ctx->txt, fp); 181 174 182 175 ctx->img = img; 183 176 ctx->trk = trk; ··· 196 189 pri_trk_del (ctx->trk); 197 190 ctx->trk = NULL; 198 191 } 192 + 193 + txt_free (&ctx->txt); 199 194 } 200 195 201 196 static ··· 307 302 } 308 303 309 304 310 - static 311 - int txt_getc (pri_text_t *ctx, unsigned idx) 312 - { 313 - int c; 314 - unsigned i; 315 - 316 - for (i = ctx->cnt; i <= idx; i++) { 317 - if ((c = fgetc (ctx->fp)) == EOF) { 318 - return (EOF); 319 - } 320 - 321 - ctx->buf[ctx->cnt++] = c; 322 - } 323 - 324 - return (ctx->buf[idx]); 325 - } 326 - 327 - int txt_error (pri_text_t *ctx, const char *str) 328 - { 329 - int c; 330 - 331 - if (str == NULL) { 332 - str = "unknown"; 333 - } 334 - 335 - c = txt_getc (ctx, 0); 336 - 337 - fprintf (stderr, "pri-text:%u: error (%s), next = %02X\n", ctx->line + 1, str, c); 338 - 339 - return (1); 340 - } 341 - 342 305 unsigned long txt_get_position (pri_text_t *ctx) 343 306 { 344 307 unsigned long val; ··· 350 313 return (val); 351 314 } 352 315 353 - static 354 - void txt_skip (pri_text_t *ctx, unsigned cnt) 355 - { 356 - unsigned i; 357 - 358 - if (cnt >= ctx->cnt) { 359 - ctx->cnt = 0; 360 - } 361 - else { 362 - for (i = cnt; i < ctx->cnt; i++) { 363 - ctx->buf[i - cnt] = ctx->buf[i]; 364 - } 365 - 366 - ctx->cnt -= cnt; 367 - } 368 - } 369 - 370 - static 371 - void txt_match_space (pri_text_t *ctx) 372 - { 373 - int c, line; 374 - 375 - line = 0; 376 - 377 - while (1) { 378 - c = txt_getc (ctx, 0); 379 - 380 - if (c == EOF) { 381 - return; 382 - } 383 - 384 - if ((c == 0x0d) || (c == 0x0a)) { 385 - line = 0; 386 - ctx->line += 1; 387 - } 388 - else if (line) { 389 - ; 390 - } 391 - else if ((c == '\t') || (c == ' ')) { 392 - ; 393 - } 394 - else if (c == '#') { 395 - line = 1; 396 - } 397 - else { 398 - return; 399 - } 400 - 401 - txt_skip (ctx, 1); 402 - } 403 - } 404 - 405 - int txt_match_eol (pri_text_t *ctx) 406 - { 407 - int c, line; 408 - 409 - line = 0; 410 - 411 - while (1) { 412 - c = txt_getc (ctx, 0); 413 - 414 - if (c == EOF) { 415 - return (1); 416 - } 417 - 418 - if ((c == 0x0d) || (c == 0x0a)) { 419 - ctx->line += 1; 420 - txt_skip (ctx, 1); 421 - 422 - return (1); 423 - } 424 - else if (line) { 425 - ; 426 - } 427 - else if ((c == '\t') || (c == ' ')) { 428 - ; 429 - } 430 - else if (c == '#') { 431 - line = 1; 432 - } 433 - else { 434 - return (0); 435 - } 436 - 437 - txt_skip (ctx, 1); 438 - } 439 - } 440 - 441 - int txt_match (pri_text_t *ctx, const char *str, int skip) 442 - { 443 - int c; 444 - unsigned i, n; 445 - 446 - txt_match_space (ctx); 447 - 448 - n = 0; 449 - 450 - while (str[n] != 0) { 451 - if (n >= ctx->cnt) { 452 - if ((c = fgetc (ctx->fp)) == EOF) { 453 - return (0); 454 - } 455 - 456 - ctx->buf[ctx->cnt++] = c; 457 - } 458 - 459 - if (ctx->buf[n] != str[n]) { 460 - return (0); 461 - } 462 - 463 - n += 1; 464 - } 465 - 466 - if (skip) { 467 - for (i = n; i < ctx->cnt; i++) { 468 - ctx->buf[i - n] = ctx->buf[i]; 469 - } 470 - 471 - ctx->cnt -= n; 472 - } 473 - 474 - return (1); 475 - } 476 - 477 - int txt_match_string (pri_text_t *ctx, char *str, unsigned max) 478 - { 479 - int c, esc; 480 - unsigned idx; 481 - 482 - txt_match_space (ctx); 483 - 484 - if ((c = txt_getc (ctx, 0)) != '"') { 485 - return (0); 486 - } 487 - 488 - txt_skip (ctx, 1); 489 - 490 - idx = 0; 491 - esc = 0; 492 - 493 - while (1) { 494 - if (idx >= max) { 495 - return (0); 496 - } 497 - 498 - if ((c = txt_getc (ctx, 0)) == EOF) { 499 - return (0); 500 - } 501 - 502 - txt_skip (ctx, 1); 503 - 504 - esc = 0; 505 - 506 - if (c == '\\') { 507 - esc = 1; 508 - 509 - if ((c = txt_getc (ctx, 0)) == EOF) { 510 - return (0); 511 - } 512 - 513 - txt_skip (ctx, 1); 514 - } 515 - 516 - if (esc) { 517 - if (c == 'n') { 518 - c = 0x0a; 519 - } 520 - else if (c == 't') { 521 - c = 0x09; 522 - } 523 - } 524 - else { 525 - if (c == '"') { 526 - break; 527 - } 528 - } 529 - 530 - str[idx++] = c; 531 - } 532 - 533 - if (idx >= max) { 534 - return (0); 535 - } 536 - 537 - str[idx] = 0; 538 - 539 - return (1); 540 - } 541 - 542 - int txt_match_hex_digit (pri_text_t *ctx, unsigned *val) 543 - { 544 - int c; 545 - 546 - c = txt_getc (ctx, 0); 547 - 548 - if ((c >= '0') && (c <= '9')) { 549 - *val = c - '0'; 550 - } 551 - else if ((c >= 'A') && (c <= 'F')) { 552 - *val = c - 'A' + 10; 553 - } 554 - else if ((c >= 'a') && (c <= 'f')) { 555 - *val = c - 'a' + 10; 556 - } 557 - else { 558 - return (0); 559 - } 560 - 561 - txt_skip (ctx, 1); 562 - 563 - return (1); 564 - } 565 - 566 - int txt_match_uint (pri_text_t *ctx, unsigned base, unsigned long *val) 567 - { 568 - unsigned i, dig; 569 - int c, s, ok; 570 - 571 - txt_match_space (ctx); 572 - 573 - i = 0; 574 - s = 0; 575 - ok = 0; 576 - 577 - c = txt_getc (ctx, i); 578 - 579 - if ((c == '-') || (c == '+')) { 580 - s = (c == '-'); 581 - 582 - c = txt_getc (ctx, ++i); 583 - } 584 - 585 - *val = 0; 586 - 587 - while (1) { 588 - if ((c >= '0') && (c <= '9')) { 589 - dig = c - '0'; 590 - } 591 - else if ((c >= 'a') && (c <= 'z')) { 592 - dig = c - 'a' + 10; 593 - } 594 - else if ((c >= 'A') && (c <= 'Z')) { 595 - dig = c - 'A' + 10; 596 - } 597 - else { 598 - break; 599 - } 600 - 601 - if (dig >= base) { 602 - return (0); 603 - } 604 - 605 - *val = base * *val + dig; 606 - ok = 1; 607 - 608 - c = txt_getc (ctx, ++i); 609 - } 610 - 611 - if (ok == 0) { 612 - return (0); 613 - } 614 - 615 - if (s) { 616 - *val = ~*val + 1; 617 - } 618 - 619 - *val &= 0xffffffff; 620 - 621 - txt_skip (ctx, i); 622 - 623 - return (1); 624 - } 625 - 626 316 int txt_enc_bits_raw (pri_text_t *ctx, unsigned long val, unsigned cnt) 627 317 { 628 318 pri_trk_set_bits (ctx->trk, val, cnt); ··· 648 338 { 649 339 unsigned long pos, val, old; 650 340 651 - if (txt_match_uint (ctx, 10, &val) == 0) { 341 + if (txt_match_uint (&ctx->txt, 10, &val) == 0) { 652 342 return (1); 653 343 } 654 344 ··· 669 359 static 670 360 int txt_enc_comm (pri_text_t *ctx) 671 361 { 672 - unsigned cnt; 673 - char str[256]; 362 + unsigned cnt; 363 + char str[256]; 674 364 675 - if (txt_match (ctx, "RESET", 1)) { 365 + if (txt_match (&ctx->txt, "RESET", 1)) { 676 366 pri_img_set_comment (ctx->img, NULL, 0); 677 367 return (0); 678 368 } 679 369 680 - if (txt_match_string (ctx, str, 256) == 0) { 370 + if (txt_match_string (&ctx->txt, str, 256) == 0) { 681 371 return (1); 682 372 } 683 373 ··· 705 395 static 706 396 int txt_enc_mode (pri_text_t *ctx) 707 397 { 708 - if (txt_match (ctx, "RAW", 1)) { 398 + if (txt_match (&ctx->txt, "RAW", 1)) { 709 399 ctx->encoding = PRI_TEXT_RAW; 710 400 } 711 - else if (txt_match (ctx, "IBM-MFM", 1)) { 401 + else if (txt_match (&ctx->txt, "IBM-MFM", 1)) { 712 402 ctx->encoding = PRI_TEXT_MFM; 713 403 } 714 - else if (txt_match (ctx, "IBM-FM", 1)) { 404 + else if (txt_match (&ctx->txt, "IBM-FM", 1)) { 715 405 ctx->encoding = PRI_TEXT_FM; 716 406 } 717 - else if (txt_match (ctx, "MAC-GCR", 1)) { 407 + else if (txt_match (&ctx->txt, "MAC-GCR", 1)) { 718 408 ctx->encoding = PRI_TEXT_MAC; 719 409 } 720 - else if (txt_match (ctx, "MFM", 1)) { 410 + else if (txt_match (&ctx->txt, "MFM", 1)) { 721 411 ctx->encoding = PRI_TEXT_MFM; 722 412 } 723 - else if (txt_match (ctx, "FM", 1)) { 413 + else if (txt_match (&ctx->txt, "FM", 1)) { 724 414 ctx->encoding = PRI_TEXT_FM; 725 415 } 726 416 else { ··· 736 426 { 737 427 unsigned long val; 738 428 739 - if (txt_match_uint (ctx, 10, &val) == 0) { 429 + if (txt_match_uint (&ctx->txt, 10, &val) == 0) { 740 430 return (1); 741 431 } 742 432 ··· 754 444 return (1); 755 445 } 756 446 757 - if (txt_match_uint (ctx, 10, &clock) == 0) { 447 + if (txt_match_uint (&ctx->txt, 10, &clock) == 0) { 758 448 return (1); 759 449 } 760 450 ··· 768 458 { 769 459 unsigned long val; 770 460 771 - while (txt_match_eol (ctx) == 0) { 772 - if (txt_match_uint (ctx, 16, &val) == 0) { 461 + while (txt_match_eol (&ctx->txt) == 0) { 462 + if (txt_match_uint (&ctx->txt, 16, &val) == 0) { 773 463 return (1); 774 464 } 775 465 ··· 790 480 { 791 481 unsigned long val; 792 482 793 - if (txt_match_uint (ctx, 10, &val) == 0) { 483 + if (txt_match_uint (&ctx->txt, 10, &val) == 0) { 794 484 return (1); 795 485 } 796 486 ··· 836 526 { 837 527 unsigned long c, h; 838 528 839 - if (txt_match_uint (ctx, 10, &c) == 0) { 529 + if (txt_match_uint (&ctx->txt, 10, &c) == 0) { 840 530 return (1); 841 531 } 842 532 843 - if (txt_match_uint (ctx, 10, &h) == 0) { 533 + if (txt_match_uint (&ctx->txt, 10, &h) == 0) { 844 534 return (1); 845 535 } 846 536 847 537 if ((c > 255) || (h > 255)) { 848 - txt_error (ctx, "c/h too large"); 538 + txt_error (&ctx->txt, "c/h too large"); 849 539 return (1); 850 540 } 851 541 ··· 884 574 { 885 575 unsigned long pos, cnt, val; 886 576 887 - if (txt_match_uint (ctx, 10, &cnt) == 0) { 577 + if (txt_match_uint (&ctx->txt, 10, &cnt) == 0) { 888 578 return (1); 889 579 } 890 580 ··· 918 608 unsigned dig, cnt; 919 609 unsigned long pos, val; 920 610 921 - if (txt_match (ctx, "RUN", 1)) { 611 + if (txt_match (&ctx->txt, "RUN", 1)) { 922 612 return (txt_enc_weak_run (ctx)); 923 613 } 924 614 925 - txt_match_space (ctx); 615 + txt_match_space (&ctx->txt); 926 616 927 617 pos = txt_get_position (ctx); 928 618 val = 0; 929 619 cnt = 0; 930 620 ok = 0; 931 621 932 - while (txt_match_hex_digit (ctx, &dig)) { 622 + while (txt_match_hex_digit (&ctx->txt, &dig)) { 933 623 val |= (unsigned long) (dig & 0x0f) << (28 - cnt); 934 624 cnt += 4; 935 625 ok = 1; ··· 1009 699 continue; 1010 700 } 1011 701 1012 - if (txt_match (ctx, "CLOCK", 1)) { 702 + if (txt_match (&ctx->txt, "CLOCK", 1)) { 1013 703 if (txt_enc_clock (ctx)) { 1014 - txt_error (ctx, "clock"); 704 + txt_error (&ctx->txt, "clock"); 1015 705 return (1); 1016 706 } 1017 707 } 1018 - else if (txt_match (ctx, "COMM", 1)) { 708 + else if (txt_match (&ctx->txt, "COMM", 1)) { 1019 709 if (txt_enc_comm (ctx)) { 1020 710 return (1); 1021 711 } 1022 712 } 1023 - else if (txt_match (ctx, "INDEX", 1)) { 713 + else if (txt_match (&ctx->txt, "INDEX", 1)) { 1024 714 if (txt_enc_index (ctx)) { 1025 715 return (1); 1026 716 } 1027 717 } 1028 - else if (txt_match (ctx, "MODE", 1)) { 718 + else if (txt_match (&ctx->txt, "MODE", 1)) { 1029 719 if (txt_enc_mode (ctx)) { 1030 720 return (1); 1031 721 } 1032 722 } 1033 - else if (txt_match (ctx, "OFFSET", 1)) { 723 + else if (txt_match (&ctx->txt, "OFFSET", 1)) { 1034 724 if (txt_enc_offset (ctx)) { 1035 725 return (1); 1036 726 } 1037 727 } 1038 - else if (txt_match (ctx, "PRI", 0)) { 728 + else if (txt_match (&ctx->txt, "PRI", 0)) { 1039 729 break; 1040 730 } 1041 - else if (txt_match (ctx, "RATE", 1)) { 731 + else if (txt_match (&ctx->txt, "RATE", 1)) { 1042 732 if (txt_enc_rate (ctx)) { 1043 733 return (1); 1044 734 } 1045 735 } 1046 - else if (txt_match (ctx, "RAW", 1)) { 736 + else if (txt_match (&ctx->txt, "RAW", 1)) { 1047 737 if (txt_enc_raw (ctx)) { 1048 738 return (1); 1049 739 } 1050 740 } 1051 - else if (txt_match (ctx, "ROTATE", 1)) { 741 + else if (txt_match (&ctx->txt, "ROTATE", 1)) { 1052 742 if (txt_enc_rotate (ctx)) { 1053 743 return (1); 1054 744 } 1055 745 } 1056 - else if (txt_match (ctx, "TRACK", 1)) { 746 + else if (txt_match (&ctx->txt, "TRACK", 1)) { 1057 747 if (txt_enc_track_finish (ctx)) { 1058 748 return (1); 1059 749 } ··· 1062 752 return (1); 1063 753 } 1064 754 } 1065 - else if (txt_match (ctx, "WEAK", 1) || txt_match (ctx, "FUZZY", 1)) { 755 + else if (txt_match (&ctx->txt, "WEAK", 1) || txt_match (&ctx->txt, "FUZZY", 1)) { 1066 756 if (txt_enc_weak (ctx)) { 1067 757 return (1); 1068 758 } 1069 759 } 1070 - else if (feof (ctx->fp)) { 760 + else if (feof (ctx->txt.fp)) { 1071 761 break; 1072 762 } 1073 763 else { ··· 1098 788 ctx->encoding = PRI_TEXT_RAW; 1099 789 ctx->index_position = 0; 1100 790 ctx->rotate = 0; 791 + ctx->offset = 0; 1101 792 ctx->crc = 0xffff; 1102 793 1103 794 ctx->mac_check_active = 0; 1104 795 ctx->mac_gap_size = 6; 1105 796 1106 797 while (1) { 1107 - if (txt_match (ctx, "PRI", 1)) { 1108 - if (txt_match_uint (ctx, 10, &val) == 0) { 798 + if (txt_match (&ctx->txt, "PRI", 1)) { 799 + if (txt_match_uint (&ctx->txt, 10, &val) == 0) { 1109 800 return (1); 1110 801 } 1111 802 1112 803 if (val == 0) { 1113 804 if (txt_encode_pri0 (ctx)) { 1114 - return (txt_error (ctx, "PRI")); 805 + return (txt_error (&ctx->txt, "PRI")); 1115 806 } 1116 807 } 1117 808 else { 1118 - return (txt_error (ctx, "bad pri version")); 809 + return (txt_error (&ctx->txt, "bad pri version")); 1119 810 } 1120 811 } 1121 - else if (feof (ctx->fp) == 0) { 1122 - return (txt_error (ctx, "no pri header")); 812 + else if (feof (ctx->txt.fp) == 0) { 813 + return (txt_error (&ctx->txt, "no pri header")); 1123 814 } 1124 815 else { 1125 816 break; ··· 1138 829 int r; 1139 830 pri_text_t ctx; 1140 831 1141 - if ((ctx.fp = fopen (fname, "r")) == NULL) { 832 + memset (&ctx, 0, sizeof (ctx)); 833 + 834 + if ((ctx.txt.fp = fopen (fname, "r")) == NULL) { 1142 835 return (1); 1143 836 } 1144 837 1145 - txt_init (&ctx, ctx.fp); 838 + txt_init (&ctx.txt, ctx.txt.fp); 1146 839 1147 840 ctx.img = img; 1148 841 1149 842 r = txt_encode (&ctx); 1150 843 1151 - fclose (ctx.fp); 844 + txt_free (&ctx.txt); 845 + fclose (ctx.txt.fp); 1152 846 1153 847 return (r); 1154 848 }
+2 -12
src/utils/pri/text.h
··· 29 29 #include <stdio.h> 30 30 31 31 #include <drivers/pri/pri.h> 32 + #include <lib/text.h> 32 33 33 34 34 35 typedef struct { ··· 39 40 40 41 41 42 typedef struct { 42 - FILE *fp; 43 - 44 - unsigned cnt; 45 - char buf[256]; 46 - unsigned line; 43 + pce_text_t txt; 47 44 48 45 pri_img_t *img; 49 46 pri_trk_t *trk; ··· 91 88 92 89 void txt_dec_bits (pri_text_t *ctx, unsigned cnt); 93 90 void txt_dec_event (pri_text_t *ctx, unsigned long type, unsigned long val); 94 - 95 - int txt_error (pri_text_t *ctx, const char *str); 96 - 97 - int txt_match_eol (pri_text_t *ctx); 98 - int txt_match (pri_text_t *ctx, const char *str, int skip); 99 - int txt_match_string (pri_text_t *ctx, char *str, unsigned max); 100 - int txt_match_uint (pri_text_t *ctx, unsigned base, unsigned long *val); 101 91 102 92 int txt_enc_bits_raw (pri_text_t *ctx, unsigned long val, unsigned cnt); 103 93