-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnbibToBib.c
1364 lines (1094 loc) · 42 KB
/
nbibToBib.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
/*#########################################################
# Name: getPMCPdf.h
# Use:
# - Holds structures and functions to downloads a pdf from
# pubmed central (PMC) using curl
# Dependencies:
# x "getPMCPdf.h" (Only if -DPDF is used)
# - NCBI blocks curl, so does not work. The code was
# left here for future reference on how to use curl.
# (for my self).
# o "cnvtNbibToBib.h"
# o "nbibToBibSettings.h"
# o "cStrFun.h"
# o <stdlib.h>
# o <string.h>
# o <stdio.h>
# o "unac.h" (Only if -DNORMACCENT is not used)
# o libiconv-1.9.1
# x <curl/curl.h> (Only if -DPDF is used)
# Note to self:
# http://graphics.stanford.edu/~seander/bithacks.html
# Has log2 by bit shift and other optiontions
# General note:
# I found out later that what I thought was a .pubmed
# format was acutally a .nbib format. This means my
# structure names are off (pubmed/pub instead of nbib).
#########################################################*/
// Allow user to turn off
#ifdef PDF
#include "getPMCPdf.h"
#endif
#include "cnvtNbibToBib.h"
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
' SOP: Start Of Program
' - main:
' o Driver for converting .pubmed to .bib
' - fun-01 getUserInput:
' o Gets the user input from the command line
' - fun-02 cStrToUS:
' o converst a c-string into unsigned short
' - fun-03 tenToX:
' o Return a power of 10
' - Fun-04 setBibPVal:
' o Sets weather to print a bibtext entry or not
' - fun-05 pHelpMesg:
' o Prints out the general help message for this program
' - fun-06 pBibEntryHelp:
' o Prints out the help message for selecting which
' bibtex entries to print
\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*--------------------------------------------------------\
| Output:
| - Modifies:
| o All input,except argsIn &numArgsI to hold user input
| - Returns:
| o 0 if succeded
| o Pointer to the proplematic paramter if failed
\--------------------------------------------------------*/
char * getUserInput(
char *argsIn[], // Arguments input by the user
int numArgsI, // Number of args input by the user
char **pubFileCStr, // Name of the .pubmed file to read
char **bibFileCStr, // Name of the .bib file to output
#ifdef PDF // NCBI blocks this so disabled
char *getPdfBl, // get pdf if PMC is present
#endif
struct pubmedStruct *pubS, // Holds line wraping & tags
struct pubOut *pubOutST
// Has bolleanas that tell which bibtex entries to
// print out
); /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
' Fun-01 TOC: Sec-01 Sub-01: getUserInput
' - Gets the user input from the command line
\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*--------------------------------------------------------\
| Output:
| - Returns: pointer to the last unconverted character
| - Modifies: retUS to hold an unsigned short
| Note:
| - This function assumes an unsigned short is 16 bits
\--------------------------------------------------------*/
char * cStrToUS(
char *inCStr, //C-string to convert to number
unsigned short *retUS /*Holds converted number*/
); /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
' Fun-02 TOC: Sec-01 Sub-01: cStrToUS
' - converst a c-string into an unsigned short
' o fun-02 sec-01: Convert the first digit
' o fun-02 sec-02: Convert digits with no overlfow risk
' o fun-02 sec-03: Convert last 2 digits (can overflow)
\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*--------------------------------------------------------\
| Output: Returns power of ten
\--------------------------------------------------------*/
unsigned long tenToX(
char powTenUC // Power to raise 10 to
);/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
' Fun-03 TOC: Sec-01 Sub-01: tenToX
' - Return a power of 10
\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*--------------------------------------------------------\
| Output:
| - Modifes: boolean value in pubOutST
| - Returns;
| o 0 for success
| o 2 for invalid input
\--------------------------------------------------------*/
unsigned char setBibPVal(
char *parmCStr, // Parameter input by user
struct pubOut *pubOutST // Has print values to set
);/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
' Fun-04 TOC: Sec-01 Sub-01: setBibPVal
' - Sets weather to print a bibtext entry or not
\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*--------------------------------------------------------\
| Output:
| - Prints:
| o Prints the help message to outFILE
\--------------------------------------------------------*/
void pHelpMesg(
FILE *outFILE
);/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
' Fun-05 TOC: pHelpMesg
' - Prints out the general help message for this program
\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*--------------------------------------------------------\
| Output: prints out bibtex entry help message to stdout
\--------------------------------------------------------*/
void pBibEntyHelp(
);/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
' Fun-06 TOC: pBibEntryHelp
' - Prints out the help message for selecting which
' bibtex entries to print
\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*--------------------------------------------------------\
| Output:
| - Prints bibtex (.bib) file to stdout or input file
\--------------------------------------------------------*/
int main(
int numArgsI,
char *argsIn[]
){ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
' Main TOC:
' - Converts pubmed format to bibtex
' o main sec-01: Variable declerations
' o Main sec-02: Get user input
' o main sec-03: Check user file input
' o main sec-04: Initialize curl variables (if needed)
\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Main Sec-01: Variable declerations
^ o main sec-01 sub-01: non-help message variables
^ o main sec-01 sub-02: help message
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
/******************************************************\
* Main Sec-01 Sub-01: non-help message variables
\******************************************************/
// usr input
char *pubFileCStr = 0; // .pubmed file to convert
char *bibFileCStr = 0; // .bib file to output
char *tmpCStr = 0;
unsigned long numPdfUL = 0;
unsigned char pubErrUC = 0;
#ifdef PDF
char getPdfBl = defGetPdf;
struct webCrawl *webCrawlST = 0;
struct webGetPdf *webGetPdfST = 0;
unsigned char pdfErrUC = 0;
#endif
struct pubmedStruct pubST;
struct pubOut pubOutST;
FILE *pubFILE = 0; // input .pubmed file
FILE *bibFILE = 0; // output bibtex (.bib) file
/******************************************************\
* Main Sec-01 Sub-02: help message
\******************************************************/
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Main Sec-02: Get user input
^ o main sec-02 sub-01: Get user input
^ o main sec-02 sub-02: Check if printing help message
^ o main sec-02 sub-03: printing version number?
^ o main sec-02 sub-04: Error from -line-wrap input?
^ o main sec-02 sub-05: Error is from unknown parameter
^ o main sec-02 sub-06: Is line wrap is large enough?
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
/*****************************************************\
* Main Sec-02 Sub-01: Get user input
\*****************************************************/
// Set up the default values (1,1 for line wrap & tags)
blankPubmedST(1, 1, 1, 1, &pubST);
blankPubOutST(&pubOutST);
tmpCStr =
getUserInput(
argsIn,
numArgsI,
&pubFileCStr,
&bibFileCStr,
#ifdef PDF
&getPdfBl, // Only here if grabbing the pdf
#endif
&pubST,
&pubOutST
);
/******************************************************\
* Main Sec-02 Sub-02: Check if printing help message
\******************************************************/
if(tmpCStr != 0)
{ // If their was invalid input
if(strcmp(tmpCStr, "-h") == 0 ||
strcmp(tmpCStr, "--h") == 0 ||
strcmp(tmpCStr, "-help") == 0 ||
strcmp(tmpCStr, "--help") == 0 ||
strcmp(tmpCStr, "help") == 0
) { // If the user wanted the help message
pHelpMesg(stdout);
exit(0);
} // If the user wanted the help message
if(
strcmp(tmpCStr, "-bib-help") == 0 ||
strcmp(tmpCStr, "-bib-h") == 0 ||
strcmp(tmpCStr, "bib-h") == 0 ||
strcmp(tmpCStr, "bib-help") == 0
){ // If user wanted the bibtext help message
pBibEntyHelp(stdout);
exit(0);
} // If user wanted the bibtext help message
/**************************************************\
* Main Sec-02 Sub-03: Printing version number?
\**************************************************/
if(strcmp(tmpCStr, "-v") == 0 ||
strcmp(tmpCStr, "-V") == 0 ||
strcmp(tmpCStr, "--v") == 0 ||
strcmp(tmpCStr, "--V") == 0 ||
strcmp(tmpCStr, "-version") == 0 ||
strcmp(tmpCStr, "--version") == 0 ||
strcmp(tmpCStr, "version") == 0
) { // If the user wanted the version number (date)
printf("pubmedToBib version: %d\n", defVersion);
exit(0);
} // If the user wanted the version number (date)
/**************************************************\
* Main Sec-02 Sub-04: Error from -line-wrap input?
\**************************************************/
if(strcmp(tmpCStr, "-line-wrap") == 0)
{ // If user input an invalid value for line wraping
for(int iArg = 1; iArg < numArgsI; ++iArg)
{ // Loop till on the invalid input
tmpCStr = *(argsIn + iArg);
if(strcmp(tmpCStr,"-line-wrap") == 0)
{ // If found the line-wrap input
tmpCStr = *(argsIn + iArg + 1);
break;
} // If found the line-wrap input
} // Loop till on the invalid input
pubFileCStr = cStrToUS(tmpCStr, &pubST.wrapUS);
if(*pubFileCStr > 47 && *pubFileCStr < 58)
{ // If the number was to large
fprintf(
stderr,
"-line-wrap %s is > 65536\n",
tmpCStr
);
exit(-1);
} // If the number was to large
fprintf(
stderr,
"-line-wrap (%s) has Non-numeric value\n",
tmpCStr
);
exit(-1);
} // If user input an invalid value for line wraping
/**************************************************\
* Main Sec-02 Sub-05: Error is unknown parameter
\**************************************************/
pHelpMesg(stderr);
fprintf(
stderr,
"Invalid paramter (%s) input\n",
tmpCStr
); // Let user know about the error
exit(-1);
} // If their was invalid input
/******************************************************\
* Main Sec-02 Sub-06: Is line wrap is large enough?
\******************************************************/
if(pubST.wrapUS > 0 && pubST.wrapUS < 32)
{ // If line wrap setting is to low
fprintf(
stderr,
"-line-wrap %u, is smaller than 32\n",
pubST.wrapUS
); // Let user know the error
exit(-1);
} // If line wrap setting is to low
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Main Sec-03: Check user file input
^ o main sec-03 sub-01: Can I open input .pubmed file?
^ o main sec-03 sub-02: Can I open the output file?
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
/******************************************************\
* Main Sec-03 Sub-01: Can I open input .pubmed file?
\******************************************************/
if(pubFileCStr == 0)
{ // If the user id not input a fil to process
fprintf(stderr, "No file input with -nbib\n");
exit(-1);
} // If the user id not input a fil to process
pubFILE = fopen(pubFileCStr, "r");
if(pubFILE == 0)
{ // If I could not open the input file
fprintf(
stderr,
"Could not open -nbib file (%s)\n",
pubFileCStr
); // Let user know about the failure
exit(-1);
} // If I could not open the input file
/******************************************************\
* Main Sec-03 Sub-02: Can I open the output file?
\******************************************************/
if(bibFileCStr == 0) bibFILE = stdout;
else bibFILE = fopen(bibFileCStr, "a");
if(bibFILE == 0)
{ // If I could not open the output file
fclose(pubFILE);
fprintf(
stderr,
"Could not open -bib file (%s)\n",
bibFileCStr
); // Let the user know about the failure
exit(-1);
} // If I could not open the output file
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Main Sec-04: Initialize curl variables (if needed)
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
#ifdef PDF
if(getPdfBl != 0)
{ // If using curl
// intailize curl
curl_global_init(CURL_GLOBAL_DEFAULT);
webCrawlST = makeWebCrawlST("");
webGetPdfST = makeWebGetPdfST(0, 0);
if(webCrawlST == 0)
{ // If i could not build the webCrawl structure
fprintf(
stderr,
"Error in get pdf setup (main: struct-1)\n"
); // Let user know why stopped
fclose(pubFILE);
fclose(bibFILE);
exit(-1);
} // If i could not build the webCrawl structure
if(webGetPdfST == 0)
{ // If I could not build the webGetPdf structure
fprintf(
stderr,
"Error in get pdf setup (main: struct-2)\n"
); // Let user know why stoped
fclose(pubFILE);
fclose(bibFILE);
freeWebCrawlST(0, webCrawlST);
exit(-1);
} // If I could not build the webGetPdf structure
} // If using curl
#endif
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Main Sec-04: Process pubmed entries and download pdfs
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
finshPubmedSTTag(&pubST); // Clean up the tag entry
pubErrUC = 1; // So the loop fires
// FILE CHECKS AND USER INPUT HERE
while(pubErrUC & 1)
{ // While I have .pubmed entries to extract
// Extract data from the bpumed file
pubErrUC = processPubMed(pubFILE, &pubST);
if(pubErrUC & 2)
{ // If was a blank line, see if next is entry
pubErrUC = 1;
continue;
} // If was a blank line, see if next is entry
if(pubST.titleCStr[0] == '\0')
continue; // No title, likely blank
if(pubErrUC > 1) break; // error
++numPdfUL;
pubmedSTToBib(&pubST, &pubOutST, bibFILE);
// Add a blank space if there will be another
// entry
if(pubErrUC & 1)
fprintf(bibFILE, "%s", pubST.lineBreakCStr);
#ifdef PDF // If using curl support at compile
if(getPdfBl == 0) continue; // Not extracting pdf
if(pubST.pmcidCStr[0] == '\0') continue;
// no pmc to extract pdf with
// Set up for grabbing the pdf
webGetPdfChangeFile(pubST.fileCStr, webGetPdfST);
pdfErrUC =
getPMCPdf(
pubST.pmcidCStr,
webCrawlST,
webGetPdfST
); // Get the pdf
if(!(pdfErrUC & 1)) break; // Error
#endif
} // While I have .pubmed entries to extract
// Only use if user wanted to complie with curl support
#ifdef PDF
if(getPdfBl == 0)
{ // If I used curl, then I need to do clean up
freeWebCrawlST(0, webCrawlST);
freeWebGetPdfST(0, webGetPdfST);
webCrawlST = 0;
curl_global_cleanup();
} // If I used curl, then I need to do clean up
#endif
if(pubFILE != 0) fclose(pubFILE);
if(bibFILE != 0) fclose(bibFILE);
if(pubErrUC & 4)
{ // If had an invalid pubmed file
fprintf(
stderr,
"-nbib %s is an invalid file\n",
pubFileCStr
);
exit(-1);
} // If had an invalid pubmed file
if(pubErrUC & 8)
{ // If failed to make the citation key
fprintf(
stderr,
"Failed to make the citation key for\n"
);
fprintf(
stderr,
" entry number: %lu (PMID %s / PMC %s)\n",
numPdfUL,
pubST.pmidCStr,
pubST.pmcidCStr
);
exit(-1);
} // If failed to make the citation key
#ifdef PDF
if(getPdfBl != 0 && !(pdfErrUC & 1))
{ // If had a problem downloading the pdf file
fprintf(stderr, "Failed to get pdf\n");
fprintf(stderr,"Problem was with entry number");
fprintf(
stderr,
" %lu (PMID %s / PMC %s)\n",
numPdfUL,
pubST.pmidCStr,
pubST.pmcidCStr
);
exit(-1);
} // If had a problem downloading the pdf file
#endif
exit(0);
} // main
/*--------------------------------------------------------\
| Output:
| - Modifies:
| o All input,except argsIn &numArgsI to hold user input
| - Returns:
| o 0 if succeded
| o Pointer to the proplematic paramter if failed
\--------------------------------------------------------*/
char * getUserInput(
char *argsIn[], // Arguments input by the user
int numArgsI, // Number of args input by the user
char **pubFileCStr, // Name of the .pubmed file to read
char **bibFileCStr, // Name of the .bib file to output
#ifdef PDF // NCBI blocks this so disabled
char *getPdfBl, // get pdf if PMC is present
#endif
struct pubmedStruct *pubST,// Holds line wraping & tags
struct pubOut *pubOutST
// Has bolleanas that tell which bibtex entries to
// print out
){ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
' Fun-01 TOC: Sec-01 Sub-01: getUserInput
' - Gets the user input from the command line
\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
char *inputCStr = 0;
char *parameterCStr = 0;
for(int iArg = 1; iArg < numArgsI; ++iArg)
{ // For all user input arguments
parameterCStr = *(argsIn + iArg);
inputCStr = *(argsIn + iArg + 1);
if(strcmp(parameterCStr, "-nbib") == 0)
{ // If the user input a pubmed file
*pubFileCStr = inputCStr;
++iArg;
continue;
} // If the user input a pubmed file
if(strcmp(parameterCStr, "-bib") == 0)
{ // If user provided a name for output bibtex file
*bibFileCStr = inputCStr;
++iArg;
continue;
} // If user provided a name for output bibtex file
if(strcmp(parameterCStr, "-line-wrap") == 0)
{ // If the user wanted to set the line wrapping
inputCStr = cStrToUS(inputCStr, &pubST->wrapUS);
if(*inputCStr > 32) return parameterCStr;
++iArg;
continue;
} // If the user wanted to set the line wrapping
#ifdef PDF
if(strcmp(parameterCStr, "-get-pmc-pdf") == 0)
{ // If user wanted to download pmc pdfs
*getPdfBl = !defGetPdf;
continue;
} // If user wanted to download pmc pdfs
#endif
if(strcmp(parameterCStr, "-tag") == 0)
{ // If the user wanted to supply a tag
pubmedSTAddTag(
inputCStr,
strlen(inputCStr),
pubST
); // Add tags for all pubmed entries
++iArg;
continue;
} // If the user wanted to supply a tag
if(strcmp(parameterCStr, "-break-unix") == 0)
{ // If the user wanted the linux line break
pubmedSTSetBreak("\n", pubST);
continue;
} // If the user wanted the linux line break
if(strcmp(parameterCStr, "-break-win") == 0)
{ // If user wanted a windows line break
pubmedSTSetBreak("\r\n", pubST);
continue;
} // If user wanted a windows line break
if(setBibPVal(parameterCStr, pubOutST) == 0)
continue; // Already set the correct value
return parameterCStr; // Invalid input
} // For all user input arguments
return 0;
} // getUserInput
/*--------------------------------------------------------\
| Output:
| - Returns: pointer to the last unconverted character
| - Modifies: retUS to hold an unsigned short
| Note:
| - This function assumes an unsigned short is 16 bits
\--------------------------------------------------------*/
char * cStrToUS(
char *inCStr, // C-string to convert to number
unsigned short *retUS // Holds converted number
){/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
' Fun-02 TOC: Sec-01 Sub-01: cStrToUS
' - converst a c-string into an unsigned short
' o fun-02 sec-01: Convert the first digit
' o fun-02 sec-02: Convert digits with no overlfow risk
' o fun-02 sec-03: Convert last 2 digits (can overflow)
\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
unsigned short firstDigUS = *inCStr - 48;
unsigned short highestPowTenUS =
tenToX((sizeof(unsigned short) << 3) / 3.333333333);
/* Logic
- sizeof(unsigned short) << 3 gives me the number of
bits in an unsigned short
- (bits in unsinged short) / 3.333333333 gives me the
closest power of 10 to the maximum short value.
- tenToX does a while loop to find the power of 10
- This step should be complied out under O2 or O3 and
makes the code handle any size of unsigned short.
*/
unsigned short maxUSDig =
((1 << (sizeof(unsigned short) << 3)) - 1)
/ highestPowTenUS;
/*Logic:
- sizeof(unsigned short) << 3 gives me the number of
bits in an unsigned short
- (1 << bits in unsigned short) gives me a mutiple of
two higher that is maximum short valute + 1.
- (highest multiple) - 1 gives me the maximum value
for a short.
- (maximum unsinged short / closest power 10) gives
me the digit in the highest postion.
- This step should be complied out under O2 or O3 and
makes the code handle any size of unsigned short.
*/
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Fun-02 Sec-01 Sub-01: Convert the first digit
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
/*Convert first number*/
if(firstDigUS >= 0 && firstDigUS < 9) /*1st digit*/
*retUS = firstDigUS;
else
{ /*Else non-numeric*/
*retUS = 0;
return inCStr;
} /*Else non-numeric*/
++inCStr;
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Fun-02 Sec-02 Sub-01: Digits with no overlfow risk
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
// o ((sizeof(unsignedshort) << 3) / 3.333333) - 3 is
// here to give the number of safe bits to convert.
// o sizeof(unsigned short) << 3 gives me the number
// of bits in an unsigned short
// o /3.3333 converts bits to number of base 10 digits
// o - 2 accounts for the first bit converted and the
// last bit at end (has a risk of overflow)
for(
int iDig = 0;
iDig < ((sizeof(unsigned short) << 3) / 3.333333) - 2;
++iDig
) { // Loop through all safe digits to convert
if(*inCStr < 58 && *inCStr > 47)
*retUS = *retUS * 10 + *inCStr - 48;
else return inCStr;
++inCStr;
} // Loop through all safe digits to convert
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Fun-02 Sec-03 Sub-01: Convert last digits (overflow)
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
if(*inCStr > 47)
{ /*If have one more number*/
// Check if there is a 0 there will be no change
if(*inCStr == '0') return inCStr + 1;
// Check if number will overflow by a large amount
if(firstDigUS > maxUSDig) return inCStr;
if(*inCStr < 58)
{ // If this is a number greater than 0
firstDigUS = 10 * *retUS + *inCStr - 48;
/* Check if had an overflow
- The idea is that an overflow will result
in the number being shorter than the nearest
highest power of 10, while any non overflow
will be at least as high as the nearest power
of 10. This is ensured by discarding any values
that have the highest power digit higher than
the highest power unsigned short digit.
*/
if(firstDigUS < highestPowTenUS)
return inCStr;
*retUS = firstDigUS;
++inCStr;
} // If this is a number greater than 0
} /*If have one more number*/
return inCStr;
} /*cStrToUS*/
/*--------------------------------------------------------\
| Output: Returns power of ten
\--------------------------------------------------------*/
unsigned long tenToX(
char powTenUC // Power to raise 10 to
){/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
' Fun-03 TOC: Sec-01 Sub-01: tenToX
' - Return a power of 10
\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
unsigned long tenUL = 1;
while(powTenUC > 0)
{ // While have a power of 10 to find
tenUL *= 10;
--powTenUC;
} // While have a power of 10 to find
return tenUL;
} // tenToX
/*--------------------------------------------------------\
| Output:
| - Modifes: boolean value in pubOutST
| - Returns;
| o 0 for success
| o 2 for invalid input
\--------------------------------------------------------*/
unsigned char setBibPVal(
char *parmCStr, // Parameter input by user
struct pubOut *pubOutST // Has print values to set
){/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
' Fun-04 TOC: Sec-01 Sub-01: setBibPVal
' - Sets weather to print a bibtext entry or not
\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
if(*(parmCStr + 1) == 'p')
{ // If setting a entry to be printed out
if(strcmp(parmCStr, "-p-citekey") == 0)
pubOutST->citeKeyBl = 1;
else if(strcmp(parmCStr, "-p-journal-id") == 0)
pubOutST->journalIDBl = 1;
else if(strcmp(parmCStr, "-p-month") == 0)
pubOutST->monthBl = 1;
else if(strcmp(parmCStr, "-p-day") == 0)
pubOutST->dayBl = 1;
else if(strcmp(parmCStr, "-p-volume") == 0)
pubOutST->volBl = 1;
else if(strcmp(parmCStr, "-p-issue") == 0)
pubOutST->issueBl = 1;
else if(strcmp(parmCStr, "-p-doi") == 0)
pubOutST->doiBl = 1;
else if(strcmp(parmCStr, "-p-ppi") == 0)
pubOutST->ppiBl = 1;
else if(strcmp(parmCStr, "-p-page-number") == 0)
pubOutST->pgBl = 1;
else if(strcmp(parmCStr, "-p-edition") == 0)
pubOutST->editionBl = 1;
else if(strcmp(parmCStr, "-p-pmid") == 0)
pubOutST->pmidBl = 1;
else if(strcmp(parmCStr, "-p-pmc") == 0)
pubOutST->pmcBl = 1;
else if(strcmp(parmCStr, "-p-isbn") == 0)
pubOutST->isbnBl = 1;
else if(strcmp(parmCStr, "-p-issn") == 0)
pubOutST->issnBl = 1;
else if(strcmp(parmCStr, "-p-url") == 0)
pubOutST->urlBl = 1;
else if(strcmp(parmCStr, "-p-abstract") == 0)
pubOutST->abstractBl = 1;
else if(strcmp(parmCStr, "-p-journal-short") == 0)
pubOutST->abvJournalBl = 1;
else if(strcmp(parmCStr, "-p-article-type") == 0)
pubOutST->articleTypeBl = 1;
else if(strcmp(parmCStr, "-p-language") == 0)
pubOutST->langBl = 1;
else if(strcmp(parmCStr, "-p-mesh-terms") == 0)
pubOutST->meshBl = 1;
else if(strcmp(parmCStr, "-p-keywords") == 0)
pubOutST->keyWordsBl = 1;
else if(strcmp(parmCStr, "-p-file-tag") == 0)
pubOutST->fileNameBl = 1;
else if(strcmp(parmCStr, "-p-tags-tag") == 0)
pubOutST->tagsBl = 1;
else if(strcmp(parmCStr, "-p-supplemental-tag") == 0)
pubOutST->supBl = 1;
else if(strcmp(parmCStr, "-p-notesp-tag") == 0)
pubOutST->notesBl = 1;
else return 2; // invalid entry
} // If setting a entry to be printed out
else if(*(parmCStr + 1) =='n' && *(parmCStr + 2) == 'o')
{ // Else if disabling printing out of an entry
if(strcmp(parmCStr, "-no-citekey") == 0)
pubOutST->citeKeyBl = 0;
else if(strcmp(parmCStr, "-no-journal-id") == 0)
pubOutST->journalIDBl = 0;
else if(strcmp(parmCStr, "-no-month") == 0)
pubOutST->monthBl = 0;
else if(strcmp(parmCStr, "-no-day") == 0)
pubOutST->dayBl = 0;
else if(strcmp(parmCStr, "-no-volume") == 0)
pubOutST->volBl = 0;
else if(strcmp(parmCStr, "-no-issue") == 0)
pubOutST->issueBl = 0;
else if(strcmp(parmCStr, "-no-doi") == 0)
pubOutST->doiBl = 0;
else if(strcmp(parmCStr, "-no-ppi") == 0)
pubOutST->ppiBl = 0;
else if(strcmp(parmCStr, "-no-page-number") == 0)
pubOutST->pgBl = 0;
else if(strcmp(parmCStr, "-no-edition") == 0)
pubOutST->editionBl = 0;
else if(strcmp(parmCStr, "-no-pmid") == 0)
pubOutST->pmidBl = 0;
else if(strcmp(parmCStr, "-no-pmc") == 0)
pubOutST->pmcBl = 0;
else if(strcmp(parmCStr, "-no-isbn") == 0)
pubOutST->isbnBl = 0;
else if(strcmp(parmCStr, "-no-issn") == 0)
pubOutST->issnBl = 0;
else if(strcmp(parmCStr, "-no-url") == 0)
pubOutST->urlBl = 0;
else if(strcmp(parmCStr, "-no-abstract") == 0)
pubOutST->abstractBl = 0;
else if(strcmp(parmCStr, "-no-journal-short") == 0)
pubOutST->abvJournalBl = 0;
else if(strcmp(parmCStr, "-no-article-type") == 0)
pubOutST->articleTypeBl = 0;
else if(strcmp(parmCStr, "-no-language") == 0)
pubOutST->langBl = 0;
else if(strcmp(parmCStr, "-no-mesh-terms") == 0)
pubOutST->meshBl = 0;
else if(strcmp(parmCStr, "-no-keywords") == 0)
pubOutST->keyWordsBl = 0;
else if(strcmp(parmCStr, "-no-file-tag") == 0)
pubOutST->fileNameBl = 0;
else if(strcmp(parmCStr, "-no-tags-tag") == 0)
pubOutST->tagsBl = 0;
else if(strcmp(parmCStr, "-no-supplemental-tag") == 0)
pubOutST->supBl = 0;
else if(strcmp(parmCStr, "-no-notesp-tag") == 0)
pubOutST->notesBl = 0;
else return 2; // invalid entry
} // Else if disabling printing out of an entry
else return 2; // invalid entry
return 0; // Success
} // setBibPVal
/*--------------------------------------------------------\
| Output:
| - Prints:
| o Prints the help message to outFILE
\--------------------------------------------------------*/
void pHelpMesg(
FILE *outFILE
){/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
' Fun-05 TOC: pHelpMesg
' - Prints out the general help message for this program
\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
char *helpCStr = "\
\n Use: Converts a .nbib file to a bibtex (.bib) file\
\n Run: pubmedToBib -nbib file.nbib [options...]\
\n Input:\
\n -nbib file.pubmed: [Required]\
\n o .nbib file to convert to bibtex (.bib) file.\
\n o Make sure each citation (entry) in the file is\
\n separated by a blank line.\
\n -bib file.bib: [stdout]\
\n o Name of the output bibtex (.bib) file.";
fprintf(outFILE, "%s", helpCStr);
fprintf(
outFILE,
"\n -line-wrap %i: [%i]",
defLineWrap,
defLineWrap
);
helpCStr="\
\n o Number of characters to have before breaking a\
\n a line (32 to 60000 characters).\
\n o -line-wrap 0: to turn of line wrapping.\
\n o This only applies to the title, abstract,\
\n authors, keywords, mesh terms, and tags.";