-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmxtool.c
1171 lines (1060 loc) · 34.3 KB
/
mxtool.c
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
/*
* mxtool.c
* Created by Chad Chabot on 12-02-02.
* Last updated: February 13, 2012 23:15 EST
*
* Author: Chad Chabot
* 0580345
* http://www.chadchabot.com/
*/
// required to use strdup() without warnings
#define _XOPEN_SOURCE 700
#define _GNU_SOURCE
#define mxtoolDebug 1
// #define XSDFILE "MARC21slim.xsd"
/*
* remember to run command "export MXTOOL_XSD=MARC21slim.xsd"
* prior to running mxtool
*/
#define XSDFILE getenv( "MXTOOL_XSD" )
#define DEVTTY "/dev/tty"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <regex.h>
#include <assert.h>
#include <ctype.h>
#include "mxtool.h"
#include "mxutil.h"
void printHelp();
void printArgs();
/* deprecated functions - BEGIN */
void bdataAddLastChar( BibData bdata );
char *copyBufferOrDefaultString( char * buffer, const char * defaultString );
/* deprecated functions - END */
char * stripTrailingSpaces( char * string );
char * stripLeadingSpaces( char * string );
int libOrBibOutput( const XmElem * top, FILE * outfile, int libFormat );
int checkPunctuation( char * string );
int marc21Setup( FILE *inputStream, XmElem **top );
void subLastChar( char * string, char * charToAdd );
void printBibInfo( BibData bibInfo );
void freeBibInfo( BibData bibInfo );
static int cmpstringp(const void *p1, const void *p2);
void printElement( XmElem *ep );
void cleanUpMain( XmElem * top1, XmElem * top2 );
int main( int argc, char * argv[] ){
int functionReturnStatus = 0;
// check to see if there are more than 2 input args
if ( argc > 3 || argc < 2 ) {
fprintf( stderr, "In main(): Too %s command line arguments.\n\n", ( argc > 3 ) ? "many": "few" );
printHelp();
return EXIT_FAILURE;
}
FILE * file1 = NULL;
XmElem * top1 = NULL; // top node of the first xml file read in
XmElem * top2 = NULL; // top node of the second xml file read in
// check to see if the first argument matches any of the valid args
if ( strcmp( argv[1], "-review" ) == 0 ) {
// MARK: -review arg
// check for more arguments
if ( argc != 2 ) {
// ERROR if more args
fprintf( stderr, "Too %s arguments to use -review function; try again\n\n", ( argc > 1 ) ? "many": "few" );
printHelp();
functionReturnStatus = EXIT_FAILURE;
}
else {
// enter Review function
if ( marc21Setup( stdin, &top1 ) != 1) {
fprintf( stderr, "In main(): unable to open [%s] schema file.\n", XSDFILE );
return EXIT_FAILURE;
}
else {
functionReturnStatus = review( top1, stdout );
}
}
}else if ( strcmp( argv[1], "-cat" ) == 0 ) {
// MARK: -cat tool
if ( argc != 3 ) {
fprintf( stderr, "Too %s arguments to use -cat function; try again\n\n", ( argc > 1 ) ? "many": "few" );
printHelp();
}
else {
file1 = fopen( argv[2], "r" );
if ( file1 == NULL ) {
fprintf( stderr, "In main(): unable to open file [%s].\n", argv[2] );
functionReturnStatus = EXIT_FAILURE;
}
else {
if ( marc21Setup( stdin, &top1 ) != 1) {
fprintf( stderr, "In main(): unable to open [%s] schema file.\n", XSDFILE );
functionReturnStatus = EXIT_FAILURE;
}
else {
// two xml files coming in
// one from the command line arg
// one from stdin ( via piping and redirection )
if ( marc21Setup( file1, &top2 ) != 1 ){
functionReturnStatus = EXIT_FAILURE;
}
else {
functionReturnStatus = concat( top1, top2, stdout );
}
}
fclose( file1 );
}
}
}else if ( strcmp( argv[1], "-keep" ) == 0 ){
// MARK: -keep tool
// printf("\tkeep tool\n");
if ( argc != 3 ) {
fprintf( stderr, "Too %s arguments to use -keep function; try again\n\n", ( argc > 1 ) ? "many": "few" );
printHelp();
functionReturnStatus = EXIT_FAILURE;
}else {
// setup XSD files and read in from pipe
if ( marc21Setup( stdin, &top1 ) != 1) {
fprintf( stderr, "In main(): unable to open [%s] schema file.\n", XSDFILE );
functionReturnStatus = EXIT_FAILURE;
}else {
functionReturnStatus = selects( top1, KEEP, argv[2], stdout );
}
}
}else if ( strcmp( argv[1], "-discard" ) == 0 ) {
// MARK: -discard tool
// printf("\tdiscard tool\n");
if ( argc != 3 ) {
fprintf( stderr, "Too %s arguments to use -discard function; try again\n\n", ( argc > 1 ) ? "many": "few" );
printHelp();
functionReturnStatus = EXIT_FAILURE;
}else {
// setup XSD files and read in from pipe
if ( marc21Setup( stdin, &top1 ) != 1) {
// TODO: exit?
fprintf( stderr, "In main(): unable to open [%s] schema file.\n", XSDFILE );
functionReturnStatus = EXIT_FAILURE;
}else {
functionReturnStatus = selects( top1, DISCARD, argv[2], stdout );
}
}
}else if ( strcmp( argv[1], "-bib" ) == 0 ) {
// MARK: -bib tool
if ( argc != 2 ) {
fprintf( stderr, "Too %s arguments to use -bib function; try again\n\n", ( argc > 1 ) ? "many": "few" );
printHelp();
functionReturnStatus = EXIT_FAILURE;
}else {
if ( marc21Setup( stdin, &top1 ) != 1 ){
fprintf( stderr, "In main(): unable to open [%s] schema file.\n", XSDFILE );
functionReturnStatus = EXIT_FAILURE;
}
else {
functionReturnStatus = bibFormat( top1, stdout );
}
}
}else if ( strcmp( argv[1], "-lib" ) == 0 ) {
// MARK: -lib tool
if ( argc != 2 ) {
fprintf( stderr, "Too %s arguments to use -lib function; try again\n\n", ( argc > 1 ) ? "many": "few" );
printHelp();
}
else {
if ( marc21Setup( stdin, &top1 ) != 1 ){
fprintf( stderr, "In main(): unable to open [%s] schema file.\n", XSDFILE );
functionReturnStatus = EXIT_FAILURE;
}
else {
functionReturnStatus = libFormat( top1, stdout );
}
}
}else {
// unrecognized input
fprintf( stderr, "In main():\tunrecognized command line arguments.\n" );
printHelp();
functionReturnStatus = EXIT_FAILURE;
}
cleanUpMain( top1, top2 );
return functionReturnStatus;
}
int review( const XmElem *top, FILE *outfile ){
if ( top == NULL || outfile == NULL ) {
fprintf( stderr, "In review(): %s.\n", top == NULL ? "XmElem *top is NULL": "FILE *outfile is NULL" );
return EXIT_FAILURE;
}
FILE *input = fopen( DEVTTY, "r"); //open the terminal keyboard
FILE *output = fopen( DEVTTY, "w"); //open the terminal screen
if ( input == NULL || output == NULL ) {
fprintf( stderr, "In review(): unable to open %s for %s.\n", DEVTTY, input == NULL ? "reading": "writing" );
return EXIT_FAILURE;
}
system("stty -F /dev/tty -icanon");
system("stty -F /dev/tty -echo");
char keyPressed;
int writeResult = 0; // how many records were written to outfile
int moreRecords = FALSE;
int currentSubelem = 0; // tracks which subelem in top is active
int subelemsOutput = 0; // tracks how many subelems have been written out
int returnStatus = EXIT_SUCCESS;
BibData bibInfo;
if ( top->nsubs > 0)
moreRecords = TRUE;
/* save a new, temporary top which will have the exact
* same values for its internal members,
* with the possibility of nsubs and subelem being different
* based on the user's selections.
*/
XmElem * tempTop = malloc( sizeof( XmElem ) );
assert( tempTop );
tempTop->tag = top->tag;
tempTop->text = top->text;
tempTop->isBlank = top->isBlank;
tempTop->nattribs = top->nattribs;
tempTop->attrib = top->attrib;
tempTop->nsubs = top->nsubs;
tempTop->subelem = malloc( top->nsubs * sizeof( XmElem * ) );
assert( tempTop->subelem );
/* TODO: Is having the moreRecords flag a good idea?
* Is there a better way to do this
* - count how many records are there, and how many have been viewed?
* - just keep going while( record->next ) or something similar?
*/
while ( moreRecords ) {
marc2bib( ( *top->subelem )[currentSubelem], bibInfo );
if ( bibInfo == NULL ) {
// there was an error, jump out?!
// TODO: exit?
returnStatus = EXIT_FAILURE;
}else{
fprintf( output, "%d. %s %s %s\n\n",currentSubelem +1, bibInfo[ AUTHOR ], bibInfo[ TITLE ], bibInfo[ PUBINFO ] );
keyPressed = getc( input );
// echo keyPressed value for testing
// printf( "\t[%c]=>[%d]\n", keyPressed, (int) keyPressed );
// act on the keyPressed by the user
switch ( (int) keyPressed ) {
case 107:
// user pressed 'k'
// write out all remaining records to outfile
// do not show any more to stdout
while ( currentSubelem < top->nsubs ) {
( *tempTop->subelem )[subelemsOutput] = ( *top->subelem )[currentSubelem];
subelemsOutput++;
currentSubelem++;
}
moreRecords = FALSE;
break;
case 100:
// user pressed 'd'
// discard all remaining records
while ( currentSubelem < top->nsubs ) {
currentSubelem++;
}
moreRecords = FALSE;
break;
case 32:
// user pressed 'space'
// skip this record
currentSubelem++;
break;
case 10:
// user pressed 'enter'
// copy record to STDOUT
// will be printed to outfile
( *tempTop->subelem )[subelemsOutput] = ( *top->subelem )[currentSubelem];
subelemsOutput++;
currentSubelem++;
break;
default:
// display help keys
fprintf( output, "\n%s%s%s%s%s%s",
"Not a valid option. Your choices are:\n",
"Enter: record is copied to stdout.\n",
"Spacebar: record is skipped.\n",
"'k' (keep): remaining records are copied and the program exits.\n",
"'d' (discard): remaingin records are skipped and the program exits.\n",
"Any other key displays help (lists these controls), the redisplays the current record.\n"
);
break;
}
}
if ( bibInfo != NULL )
freeBibInfo( bibInfo );
if ( currentSubelem >= top->nsubs )
moreRecords = FALSE;
}
/* clean up tempTop;
* the number of subelems ptrs allocated = nsubs,
* but it has only subelemsOutput of them populated
* realloc that many?
*/
tempTop->nsubs = subelemsOutput;
tempTop->subelem = realloc( tempTop->subelem, tempTop->nsubs * sizeof( XmElem * ) );
writeResult = mxWriteFile( tempTop, outfile );
if ( -1 == writeResult )
returnStatus = EXIT_FAILURE;
if ( input != NULL )
fclose( input );
if ( output != NULL )
fclose( output );
// TODO: free some memory! tempTop and tempTop->subelem at least?
system("stty -F /dev/tty icanon");
system("stty -F /dev/tty echo");
return returnStatus;
}
/* prints out the accepted command line argument formats
*
* PRE the entered command line args are incorrect
* POST none
*/
void printHelp(){
fprintf( stderr, "NAME\n\tmxtool -- open and act on MARCXML files to export reference lists\n\n" );
fprintf( stderr, "SYNOPSIS\n\tmxtool [-OPTIONS [PARAMETERS] ]\n\n" );
fprintf( stderr, "OPTIONS\n" );
fprintf( stderr, "\t-review\n" );
fprintf( stderr, "\t-cat XMLFILENAME\n" );
fprintf( stderr, "\t-keep PATTERN\n" );
fprintf( stderr, "\t-discard PATTERN\n" );
fprintf( stderr, "\t-lib\n" );
fprintf( stderr, "\t-bib\n" );
}
/* prints out the command line args that the user has entered
*
*/
void printArgs( int numArgs, char * args[] ){
int i = 0;
for( i = 0; i < numArgs; i++ ){
printf( "%s ", args[i] );
}
printf( "\n" );
}
/* Used to parse and validate the xmlSchema as well as
* read in and validate an XML file against the schema
*
* PRE inputStream is open for reading
* top is an un-allocated XmElem pointer
* POST Returns 0 (false) if either the schema is not valid
* or if mxReadFile was unable to validate the XML file.
* Returns 1 (true) if successful
*/
int marc21Setup( FILE *inputStream, XmElem **top ){
int mf = 0;
// get MARC 21 XML schema pattern
// and make sure it is valid
xmlSchemaPtr schema = mxInit( XSDFILE );
if ( schema == NULL ) {
// TODO: exit()? or return bad value?
#ifndef mxtoolDebug
printf( "schema is NULL\n" );
#endif
fprintf( stderr, "In marc21Setup(): schema is NULL, unable to validate schema.\n" );
return 0;
}
#ifndef mxtoolDebug
printf( "schema is not NULL\n" );
#endif
// compare input file against schema against
// if it checks out, continue
mf = mxReadFile( inputStream, schema, top );
if ( mf != 0 ) {
#ifndef mxtoolDebug
printf( "mxReadFile() failed\n");
#endif
// TODO: exit()? or return bad value?
fprintf( stderr, "marc21Setup(): mxReadFile() returned [%d]. reason: \"%s\".\n", mf, mf == 1? "failed to parse XML file" : "XML file did not match schema" );
// exit( 1 );
return 0;
}
#ifndef mxtoolDebug
printf( "mxReadFile() success\n");
#endif
mxTerm( schema );
return 1;
}
void marc2bib( const XmElem * mrec, BibData bdata ){
char * stringA = NULL;
char * stringB = NULL;
char * stringC = NULL;
char * stringP = NULL;
char * string4 = NULL;
// check for AUTHOR
if ( 0 < mxFindField( mrec, 100 ) ) {
if ( 0 < mxFindSubfield( mrec, 100, 1, 'a' ) ){
stringA = (char * ) mxGetData( mrec, 100, 1, 'a', 1 );
} else {
stringA = NULL;
}
asprintf( &bdata[ AUTHOR ], "%s", NULL == stringA ? "" : stringA );
} else if ( 0 < mxFindField( mrec, 130 ) ) {
if ( 0 < mxFindSubfield( mrec, 130, 1, 'a' ) ){
stringA = (char * ) mxGetData( mrec, 130, 1, 'a', 1 );
} else {
stringA = NULL;
}
asprintf( &bdata[ AUTHOR ], "%s", NULL == stringA ? "" : stringA );
} else {
asprintf( &bdata[ AUTHOR ], "%s", "na" );
}
// check for TITLE
if ( 0 < mxFindField( mrec, 245 ) ) {
if ( 0 < mxFindSubfield( mrec, 245, 1, 'a' ) ){
stringA = (char * ) mxGetData( mrec, 245, 1, 'a', 1 );
} else {
stringA = NULL;
}
if ( 0 < mxFindSubfield( mrec, 245, 1, 'p' ) ){
stringP = (char * ) mxGetData( mrec, 245, 1, 'p', 1 );
} else {
stringP = NULL;
}
if ( 0 < mxFindSubfield( mrec, 245, 1, 'b' ) ){
stringB = (char * ) mxGetData( mrec, 245, 1, 'b', 1 );
} else {
stringB = NULL;
}
asprintf( &bdata[ TITLE ], "%s%s%s", NULL == stringA ? "" : stringA,
NULL == stringP ? "" : stringP,
NULL == stringB ? "" : stringB );
} else {
asprintf( &bdata[ TITLE ], "%s", "na" );
}
// check for PUBINFO
if ( 0 < mxFindField( mrec, 260 ) || 0 < mxFindField( mrec, 250 ) ) {
if ( 0 < mxFindSubfield( mrec, 260, 1, 'a' ) ){
stringA = (char * ) mxGetData( mrec, 260, 1, 'a', 1 );
} else {
stringA = NULL;
}
if ( 0 < mxFindSubfield( mrec, 260, 1, 'b' ) ){
stringB = (char * ) mxGetData( mrec, 260, 1, 'b', 1 );
} else {
stringB = NULL;
}
if ( 0 < mxFindSubfield( mrec, 260, 1, 'c' ) ){
stringC = (char * ) mxGetData( mrec, 260, 1, 'c', 1 );
} else {
stringC = NULL;
}
if ( 0 < mxFindSubfield( mrec, 250, 1, 'a' ) ){
string4 = (char * ) mxGetData( mrec, 250, 1, 'a', 1 );
} else {
string4 = NULL;
}
asprintf( &bdata[ PUBINFO ], "%s%s%s%s", NULL == stringA ? "" : stringA,
NULL == stringB ? "" : stringB,
NULL == stringC ? "" : stringC,
NULL == string4 ? "" : string4);
} else {
asprintf( &bdata[ PUBINFO ], "%s", "na" );
}
// check for CALLNUM
if ( 0 < mxFindField( mrec, 90 ) ) {
if ( 0 < mxFindSubfield( mrec, 90, 1, 'a' ) ){
stringA = (char * ) mxGetData( mrec, 90, 1, 'a', 1 );
} else {
stringA = NULL;
}
if ( 0 < mxFindSubfield( mrec, 90, 1, 'b' ) ){
stringB = (char * ) mxGetData( mrec, 90, 1, 'b', 1 );
} else {
stringB = NULL;
}
asprintf( &bdata[ CALLNUM ], "%s%s", NULL == stringA ? "" : stringA,
NULL == stringB ? "" : stringB );
} else if ( 0 < mxFindField( mrec, 50 ) ) {
if ( 0 < mxFindSubfield( mrec, 50, 1, 'a' ) ){
stringA = (char * ) mxGetData( mrec, 50, 1, 'a', 1 );
} else {
stringA = NULL;
}
if ( 0 < mxFindSubfield( mrec, 50, 1, 'b' ) ){
stringB = (char * ) mxGetData( mrec, 50, 1, 'b', 1 );
} else {
stringB = NULL;
}
asprintf( &bdata[ CALLNUM ], "%s%s", NULL == stringA ? "" : stringA,
NULL == stringB ? "" : stringB );
} else {
asprintf( &bdata[ CALLNUM ], "%s", "na" );
}
}
/* Funnels all members of a BibData struct to subLastChar()
* to check for/add a "." at the end of each field.
*
* PRE bdata is an allocated and filled BibData struct
* POST bdata's members are altered based on what happened
* in subLastChar().
*/
void bdataAddLastChar( BibData bdata ){
char period[] = ".";
subLastChar( bdata[ TITLE ], period );
subLastChar( bdata[ AUTHOR ], period );
subLastChar( bdata[ CALLNUM ], period );
subLastChar( bdata[ PUBINFO ], period );
}
/********
* A poorly named function that will check from the last printable character
* of string is the character specifed by charToAdd.
* If charToAdd is not found, it will add it at the end of string.
* Only called by bdataAddLastChar.
*
* PRE: string is not NULL and of length > 1
* charToAdd is a char, and not NULL
* POST: string will have an ending character which matches charToAdd
********/
void subLastChar( char * string, char * charToAdd ){
// make sure that the character is actually only one character long
if ( strlen( charToAdd ) == 1 ) {
int i = 0;
for ( i = ( strlen( string ) -1 ) ; i >= 0; i-- ) {
if ( string[i] == charToAdd[0] ) {
break;
}
else {
// !!!: invalid write of size 1
if ( (int)string[i] > 31 && (int)string[i] < 127) {
string = strcat( string, charToAdd );
break;
}
}
}
}
else {
// print to stderr?
// no return value required… but I should do something?
}
}
/********
* Will take in a bibInfo object (should it be a BibData * instead?)
* and free each of the components, provided the have not already been freed
*
* PRE: bibInfo is not NULL
* POST: all members of bibInfo have been free'd
********/
void freeBibInfo( BibData bibInfo ){
if ( bibInfo[ AUTHOR ] != NULL )
free( bibInfo[ AUTHOR ] );
if ( bibInfo[ TITLE ] != NULL )
free( bibInfo[ TITLE ] );
if ( bibInfo[ CALLNUM ] != NULL )
free( bibInfo[ CALLNUM ] );
if ( bibInfo[ PUBINFO ] != NULL )
free( bibInfo[ PUBINFO ] );
}
/********
* Takes in two strings, and will return either buffer or defaultString
* based on whether buffer is NULL or not.
*
* PRE: buffer is a char * that may or may not be NULL
* defaultString is a char * that is not NULL.
* POST: a non NULL char * is returned.
********/
char *copyBufferOrDefaultString( char * buffer, const char * defaultString ){
/* TODO: pre- and post-conditions
* usage & reason for existing
* ???: this returns gibberish. Fix before using
*/
if ( buffer != NULL ) {
return buffer;
}else {
return ( char * ) defaultString;
}
}
/********
* Prints out, with field names, all members of a BibData struct
*
* PRE: bibInfo is not NULL
* POST: stuff is printed out…
********/
void printBibInfo( BibData bibInfo ){
// TODO: pre and post conditions; usage
printf( "title: [%s]\nauthor: [%s]\npubinfo: [%s]\ncallnum: [%s]\n",
bibInfo[ TITLE ] == NULL ? "NULL" : bibInfo[ TITLE ],
bibInfo[ AUTHOR ] == NULL ? "NULL" : bibInfo[ AUTHOR ],
bibInfo[ PUBINFO ] == NULL ? "NULL" : bibInfo[ PUBINFO ],
bibInfo[ CALLNUM ] == NULL ? "NULL" : bibInfo[ CALLNUM ]);
}
int match( const char * data, const char * regex ){
if ( data == NULL || regex == NULL ) {
// should never exit here, but just in case
fprintf( stderr, "In match():\t%s was a NULL string\n", data == NULL ? "DATA" : "REGEX" );
return 0;
}
regex_t pattern;
int match = 0;
// compile regular expression
if ( regcomp( &pattern, regex, 0 ) == 0 ) {
// execute regex search
match = regexec( &pattern, data, 0, NULL, 0);
if ( !match ) {
regfree( &pattern );
return 1;
}
regfree( &pattern );
return 0;
}
else {
// unable to compile regular expression
// no error message required! Handled by calling function.
fprintf( stderr, "In match():\tunable to compile regular express from \"%s\" string\n", regex );
return 0;
}
}
int concat( const XmElem *top1, const XmElem *top2, FILE *outfile ){
// take the records from top1 and top2, and add them to a new XmElem * joinedTop
// create a new top, joinedTop
// copy the elements of top1 to joinedTop
// create a new collection element – subelem[0] of joinedTop – with the same contents as (*top1->subelem)[0]
int i = 0;
int j = 0;
int numRecordsWritten = 0;
XmElem * joinedTop = malloc( sizeof( XmElem ) );
assert( joinedTop );
joinedTop->tag = top1->tag;
joinedTop->text = top1->text;
joinedTop->isBlank = top1->isBlank;
joinedTop->nattribs = top1->nattribs;
joinedTop->attrib = top1->attrib;
joinedTop->nsubs = top1->nsubs + top2->nsubs;
joinedTop->subelem = malloc( ( top1->nsubs + top2->nsubs ) * sizeof( XmElem * ) );
assert( joinedTop->subelem );
// add records from top1
for ( i = 0; i < top1->nsubs; i++ )
( *joinedTop->subelem )[i] = (* top1->subelem )[i];
// add records from top2
for ( j = 0; j < top2->nsubs; j++, i++ )
( *joinedTop->subelem )[i] = ( *top2->subelem )[j];
// now all records are part of joinedTop
numRecordsWritten = mxWriteFile( joinedTop, outfile );
/* the only thing malloc'd in this function is the array of XmElem ptrs
* this is freeing the array, not the array members:
* those are still "owned" and reachable via top1, top2.
*/
if ( NULL != joinedTop->subelem )
free( joinedTop->subelem );
if ( numRecordsWritten == joinedTop->nsubs ) {
if ( NULL != joinedTop )
free( joinedTop );
return EXIT_SUCCESS;
}else {
if ( NULL != joinedTop )
free( joinedTop );
return EXIT_FAILURE;
}
}
int selects( const XmElem *top, const enum SELECTOR sel, const char *pattern, FILE *outfile ){
// if no records exist to match against
// technically, this isn't an error…
// so EXIT_FAILURE would be incorrect
if ( top->nsubs == 0){
return EXIT_SUCCESS;
}
if ( pattern[1] != '=' ) {
fprintf( stderr, "In selects():\tregex pattern [%s] does not conform to format %s.\n",
pattern,
"\"<field>=<regex>\"\n\t\twhere <field> is (a)uthor, (p)ublisher, or (t)tile" );
return EXIT_FAILURE;
}
char * regexString = (char *)(pattern+2); // use pointer arithmetic to avoid copying the regex pattern
int fieldToFind = (int) pattern[0];
int writeResult = 0;
int matchFound = 0;
int subelemsRead = 0;
int subelemsWriting = 0;
int subelemCounter = 0;
BibData bibInfo;
/* save a new, temporary top which will have the exact
* same values for its internal members,
* with the possibility of nsubs and subelem being different
* based on the user's selections.
*/
XmElem * tempTop = malloc( sizeof( XmElem ) );
assert( tempTop );
tempTop->tag = top->tag;
tempTop->text = top->text;
tempTop->isBlank = top->isBlank;
tempTop->nattribs = top->nattribs;
tempTop->attrib = top->attrib;
tempTop->nsubs = top->nsubs;
tempTop->subelem = malloc( top->nsubs * sizeof( XmElem * ) );
assert( tempTop->subelem );
switch ( fieldToFind ) {
case 97:
// looking for author
/* TODO: clean this up; it should be refactored into a more compact function
*/
for ( subelemCounter = 0; subelemCounter < top->nsubs ; subelemCounter++) {
marc2bib( ( *top->subelem)[ subelemCounter ], bibInfo );
if ( bibInfo == NULL ) {
// clean up tempTop
return EXIT_FAILURE;
}
// printBibInfo( bibInfo );
matchFound = match( bibInfo[ AUTHOR ], regexString );
if ( matchFound != 0 ) {
if ( sel == KEEP ) {
// add this record to tempTop, at position subelemsWriting
( *tempTop->subelem )[ subelemsWriting ] = ( *top->subelem )[ subelemCounter ];
// increment numRecordsToWrite
subelemsWriting++;
}
//
}else {
// if sel == KEEP
// do nothing
if ( sel == DISCARD ) {
// add this record to tempTop, at position subelemsWriting
( *tempTop->subelem )[ subelemsWriting ] = ( *top->subelem )[ subelemCounter ];
// increment numRecordsToWrite
subelemsWriting++;
}
}
subelemsRead++;
if ( bibInfo != NULL )
freeBibInfo( bibInfo );
}
break;
case 116:
// looking for author
for ( subelemCounter = 0; subelemCounter < top->nsubs ; subelemCounter++) {
marc2bib( ( *top->subelem)[ subelemCounter ], bibInfo );
if ( bibInfo == NULL ) {
// clean up tempTop
return EXIT_FAILURE;
}
// printBibInfo( bibInfo );
matchFound = match( bibInfo[ TITLE ], regexString );
if ( matchFound != 0 ) {
if ( sel == KEEP ) {
// add this record to tempTop, at position subelemsWriting
( *tempTop->subelem )[ subelemsWriting ] = ( *top->subelem )[ subelemCounter ];
// increment numRecordsToWrite
subelemsWriting++;
}
//
}else {
// if sel == KEEP
// do nothing
if ( sel == DISCARD ) {
// add this record to tempTop, at position subelemsWriting
( *tempTop->subelem )[ subelemsWriting ] = ( *top->subelem )[ subelemCounter ];
// increment numRecordsToWrite
subelemsWriting++;
}
}
subelemsRead++;
if ( bibInfo != NULL )
freeBibInfo( bibInfo );
}
break;
case 112:
// looking for author
for ( subelemCounter = 0; subelemCounter < top->nsubs ; subelemCounter++) {
marc2bib( ( *top->subelem)[ subelemCounter ], bibInfo );
if ( bibInfo == NULL ) {
// clean up tempTop
return EXIT_FAILURE;
}
//printBibInfo( bibInfo );
matchFound = match( bibInfo[ PUBINFO ], regexString );
if ( matchFound != 0 ) {
if ( sel == KEEP ) {
// add this record to tempTop, at position subelemsWriting
( *tempTop->subelem )[ subelemsWriting ] = ( *top->subelem )[ subelemCounter ];
// increment numRecordsToWrite
subelemsWriting++;
}
//
}else {
// if sel == KEEP
// do nothing
if ( sel == DISCARD ) {
// add this record to tempTop, at position subelemsWriting
( *tempTop->subelem )[ subelemsWriting ] = ( *top->subelem )[ subelemCounter ];
// increment numRecordsToWrite
subelemsWriting++;
}
}
subelemsRead++;
if ( bibInfo != NULL )
freeBibInfo( bibInfo );
}
break;
default:
// unrecognized search field
fprintf( stderr, "In selects():\tregex pattern [%s] does not conform to format %s.\n",
pattern,
"\"<field>=<regex>\"\n\t\twhere <field> is (a)uthor, (p)ublisher, or (t)tile" );
return EXIT_FAILURE;
break;
}
// now realloc the size of tempTop->subelem
tempTop->nsubs = subelemsWriting;
tempTop->subelem = realloc( tempTop->subelem, tempTop->nsubs * sizeof( XmElem * ) );
writeResult = mxWriteFile( tempTop, outfile );
/* ???: why do the following 4 lines give
* me invalid reads?
*/
if ( tempTop->subelem != NULL )
free( tempTop->subelem );
if ( tempTop != NULL )
free( tempTop );
return EXIT_SUCCESS;
}
int checkPunctuation( char * string ) {
int i = strlen( string ) - 1;
while ( i >= -1 ) {
if ( isspace( string[i] ) ){
i--;
}
else {
if ( !ispunct( string[i] ) ) {
// printf( "no punctuation in [%s]\n", string );
return 1;
}
else {
// printf("punctuation in [%s] @ [%d] position: [%c]\n", string, i + 1, string[i] );
return 0;
}
}
}
// printf( "no punctuation in [%s]\n", string );
return 1;
}
char * stripTrailingSpaces( char * string ){
int i = strlen( string ) - 1;
char * outputString = strdup( string );
// TODO: figure out how to copy from the input string to the output string
while ( 0 <= i) {
if ( !isspace( outputString[i] ) ){
break;
} else {
outputString[i] = '\0';
i--;
}
}
return outputString;
}
/* start by using pointer arithmatic to move starting position of sting forward,
* send string+offset to strip trailing spaces, and then copy/return the first N characters in string+offset
*
*/
char * stripLeadingSpaces( char * string ){
int i = 0;
while ( 0 < strlen( string ) ) {
if ( !isspace( string[i] ) )
break;
else
i++;
}
return stripTrailingSpaces( string+i );
}
int bibFormat( const XmElem *top, FILE *outfile ){
return libOrBibOutput( top, outfile, 0 );
}
int libFormat( const XmElem *top, FILE *outfile ){
return libOrBibOutput( top, outfile, 1 );
}
int libOrBibOutput( const XmElem * top, FILE * outfile, int libFormat ){
if ( top == NULL ) {
fprintf( stderr, "In %s(): parameter XmElem * top was NULL.\n", 1 == libFormat ? "libFormat" : "bibFormat" );
return EXIT_FAILURE;
}
if ( outfile == NULL ) {
fprintf( stderr, "In %s(): parameter FILE * outfile was NULL.\n", 1 == libFormat ? "libFormat" : "bibFormat" );
return EXIT_FAILURE;
}
const char *keys[top->nsubs];
BibData bibInfo;
int i = 0;
int numRecords = top->nsubs;
// get bibInfo for each record
for ( i = 0; i < numRecords; i++ ) {
marc2bib( (*top->subelem)[i], bibInfo );
if ( bibInfo == NULL ) {
fprintf( stderr, "Error generating bibInfo of [%d]th subelem in %s\n", i, 1 == libFormat ? "libFormat" : "bibFormat" );
return EXIT_FAILURE;
}
if ( 1 == libFormat ) {
keys[ i ] = strdup( bibInfo[ CALLNUM ] );
}else {
keys[ i ] = strdup( bibInfo[ AUTHOR ] );
}
// printBibInfo( bibInfo );
freeBibInfo ( bibInfo );
// printf( "*[%s] key created\n", keys[ i ]);
}
// don't send top in to sortRecs. Copy all of top's direct members, no descendants.
// TODO: create duplicateXmElem() function in mxutil.c
// duplicate XmElem node
XmElem * sortedTop = malloc( sizeof( XmElem ) );
assert( sortedTop );
sortedTop->tag = top->tag;
sortedTop->text = top->text;
sortedTop->isBlank = top->isBlank;
sortedTop->nattribs = top->nattribs;
sortedTop->attrib = top->attrib;
sortedTop->nsubs = top->nsubs;
sortedTop->subelem = malloc( top->nsubs * sizeof( XmElem * ) );
for ( i = 0; i < sortedTop->nsubs; i++) {
( *sortedTop->subelem )[ i ] = ( *top->subelem )[ i ];
}
sortRecs( sortedTop, keys );
// free the keys array
for (i = 0; i < numRecords; i++)
free( (char * )keys[i] );
// output marc2Bib records and echo them
for ( i = 0; i < numRecords; i++ ) {
marc2bib( (*sortedTop->subelem)[i], bibInfo );
if ( bibInfo == NULL ) {
return EXIT_FAILURE;
}