-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
837 lines (752 loc) · 21.4 KB
/
main.cpp
File metadata and controls
837 lines (752 loc) · 21.4 KB
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
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <poppler-document.h>
#include <poppler-page.h>
#include <sys/mman.h>
#include <unistd.h>
#include <cstdlib>
#include <cstring>
#include <cstdint>
#include <cstdio>
#include <cerrno>
#define BUFFER_SIZE 256
int main()
{
int64_t rc = 0;
uint64_t const pagesz = sysconf(_SC_PAGESIZE);
uint64_t len_mmap = (pagesz << 1);
errno = 0;
void *buf = mmap(NULL, len_mmap, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (MAP_FAILED == buf) {
if (errno) {
fprintf(stderr, "%s\n", strerror(errno));
}
exit(EXIT_FAILURE);
}
rc = madvise(buf, len_mmap, MADV_WILLNEED);
if (-1 == rc) {
fprintf(stderr, "%s", "error: madvise fast access failed\n");
if (errno) {
fprintf(stderr, "%s\n", strerror(errno));
}
exit(EXIT_FAILURE);
}
std::unique_ptr<poppler::document> doc(poppler::document::load_from_file("doc.pdf"));
if (!doc) {
fprintf(stderr, "%s", "error: failed to load document from file\n");
exit(EXIT_FAILURE);
}
// NOTE: we are assuming that the important data is on the first page
std::unique_ptr<poppler::page> page(doc->create_page(0));
if (!page) {
fprintf(stderr, "%s", "error: failed to create page\n");
exit(EXIT_FAILURE);
}
// NOTE: Even though the code is returning an error, this can be used to signal
// that the medical document must be processed manually probably because
// of poor resolution. This is the right way to respond to that case.
poppler::byte_array bytes = page->text().to_utf8();
if (!bytes.size()) {
fprintf(stderr, "%s", "error: poppler failed to extract text data\n");
exit(EXIT_FAILURE);
}
uint64_t bytes_written = 0;
char unsigned *data = (char unsigned*) buf;
for (uint64_t i = 0; i != bytes.size(); ++i, ++bytes_written) {
data[bytes_written] = bytes[i];
if ((len_mmap - bytes_written) <= pagesz) {
buf = mremap(buf, len_mmap, (len_mmap << 1), MREMAP_MAYMOVE);
if (MAP_FAILED == buf) {
if (errno) {
fprintf(stderr, "%s\n", strerror(errno));
}
exit(EXIT_FAILURE);
}
len_mmap <<= 1;
data = (char unsigned*) buf;
}
}
errno = 0;
void *dstbuf = mmap(NULL, len_mmap, PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (MAP_FAILED == dstbuf) {
fprintf(stderr, "%s", "error: destination chat memory mapping failed\n");
if (errno) {
fprintf(stderr, "%s\n", strerror(errno));
}
_exit(1);
}
rc = madvise(dstbuf, len_mmap, MADV_WILLNEED);
if (-1 == rc) {
fprintf(stderr, "%s", "error: dest mmap sequential access failed\n");
if (errno) {
fprintf(stderr, "%s\n", strerror(errno));
}
_exit(1);
}
uint64_t len = 0;
uint64_t count = 0;
char unsigned *srcbuf = (char unsigned*) buf;
char unsigned *dst = (char unsigned*) dstbuf;
char unsigned *txt = (char unsigned*) srcbuf;
// excludes non-ASCII characters from the content
while (bytes_written > count) {
if (0x80u > (*txt)) {
if (((*txt) < 0x0au)) {
*dst = 0x20u;
}
else if (((*txt) >= 0x0bu) && ((*txt) < 0x20u)) {
*dst = 0x20u;
}
else if (((*txt) == 0x22u) || ((*txt) == 0x27u)) {
*dst = 0x20u;
}
else if (((*txt) >= 0x41u) && ((*txt) < 0x5bu)) {
*dst = (((*txt) - 0x41u) + 0x61u);
}
else if ((0x7fu == (*txt))) {
*dst = 0x20u;
}
else {
*dst = *txt;
}
txt += 1;
dst += 1;
count += 1;
len += 1;
}
else if (0xc2 > (*txt)) {
fprintf(stderr, "%s", "error: invalid utf-8 prefix\n");
_exit(1);
}
else if (0xe0u > (*txt)) {
uint16_t const value = ((txt[1] << 8) | txt[0]);
if ((value >= 0x80c3u) && (value < 0x86c3u)) {
*dst = 'a';
dst += 1;
len += 1;
}
else if ((value >= 0x88c3u) && (value < 0x8cc3u)) {
*dst = 'e';
dst += 1;
len += 1;
}
else if ((value >= 0x8cc3u) && (value < 0x90c3u)) {
*dst = 'i';
dst += 1;
len += 1;
}
else if ((value >= 0x92c3u) && (value < 0x97c3u)) {
*dst = 'o';
dst += 1;
len += 1;
}
else if ((value >= 0x99c3u) && (value < 0x9ec3u)) {
*dst = 'u';
dst += 1;
len += 1;
}
else if ((value >= 0xa0c3u) && (value < 0xa6c3u)) {
*dst = 'a';
dst += 1;
len += 1;
}
else if ((value >= 0xa8c3u) && (value < 0xacc3u)) {
*dst = 'e';
dst += 1;
len += 1;
}
else if ((value >= 0xacc3u) && (value < 0xb0c3u)) {
*dst = 'i';
dst += 1;
len += 1;
}
else if ((value == 0xb1c3u)) {
*dst = 'n';
dst += 1;
len += 1;
}
else if ((value >= 0xb2c3u) && (value < 0xb7c3u)) {
*dst = 'o';
dst += 1;
len += 1;
}
else if ((value >= 0xb9c3u) && (value < 0xbdc3u)) {
*dst = 'u';
dst += 1;
len += 1;
}
txt += 2;
count += 2;
}
else if (0xf0u > (*txt)) {
txt += 3;
count += 3;
}
else {
txt += 4;
count += 4;
}
}
if (bytes_written != count) {
fprintf(stderr, "%s", "error: bytes read and filesize mismatch\n");
exit(EXIT_FAILURE);
}
else {
fprintf(stdout, "%s %lu\n", "bytes-raw:", bytes_written);
fprintf(stdout, "%s %lu\n", "bytes-kept:", len);
}
// filters garbage from some medical records that use watermarks
data = (char unsigned*) dstbuf;
for (uint64_t i = 0; i != len; ++i) {
if (('e' == data[i]) && ('p' == data[i + 1]) && ((' ' == data[i + 2]) || ('\n' == data[i + 2]))) {
data[i] = ' ';
data[i + 1] = ' ';
}
if (('s' == data[i]) && ('u' == data[i + 1]) && ((' ' == data[i + 2]) || ('\n' == data[i + 2]))) {
data[i] = ' ';
data[i + 1] = ' ';
}
if (((' ' == data[i]) || ('\n' == data[i])) && ('a' == data[i + 1]) && ((' ' == data[i + 2]) || ('\n' == data[i + 2]))) {
data[i + 1] = ' ';
}
if (((' ' == data[i]) || ('\n' == data[i])) && ('r' == data[i + 1]) && ((' ' == data[i + 2]) || ('\n' == data[i + 2]))) {
data[i + 1] = ' ';
}
if (((' ' == data[i]) || ('\n' == data[i])) && ('s' == data[i + 1]) && ((' ' == data[i + 2]) || ('\n' == data[i + 2]))) {
data[i + 1] = ' ';
}
if (((' ' == data[i]) || ('\n' == data[i])) && ('e' == data[i + 1]) && ((' ' == data[i + 2]) || ('\n' == data[i + 2]))) {
data[i + 1] = ' ';
}
}
fprintf(stdout, "%s", data);
// extracts data based on provider
char placeholder[BUFFER_SIZE];
char patient_name[BUFFER_SIZE];
char patient_document[BUFFER_SIZE];
char physician_name[BUFFER_SIZE];
char *csi = strstr((char*) dstbuf, "clinica san ignacio");
if (csi) {
fprintf(stdout, "%s", "processing: clinica san ignacio document\n");
char paciente[] = "paciente";
char *patient = strstr((char*) dstbuf, paciente);
if (!patient) {
fprintf(stderr, "%s", "error: missing patient info\n");
exit(EXIT_FAILURE);
}
char *type = strstr((char*) dstbuf, "tipo paciente");
if (!type) {
fprintf(stderr, "%s", "error: missing patient-type info\n");
exit(EXIT_FAILURE);
}
if (patient >= type) {
fprintf(stderr, "%s", "error: unexpected layout\n");
exit(EXIT_FAILURE);
}
if ((type - patient) >= BUFFER_SIZE) {
fprintf(stderr, "%s", "error: would overrun buffer\n");
exit(EXIT_FAILURE);
}
patient += sizeof(paciente);
uint64_t sz = (type - patient);
memset(patient_name, 0, BUFFER_SIZE);
memcpy(patient_name, patient, sz);
// truncates patient name so that it does not look weird on the console
char *str = patient_name;
while (*str) {
if (
((' ' == str[0]) || ('\n' == str[0])) &&
((' ' == str[1]) || ('\n' == str[1]))
) {
str[0] = 0;
break;
}
++str;
}
char numero[] = "numero";
char *document = strstr((char*) dstbuf, numero);
if (!document) {
fprintf(stderr, "%s", "error: missing patient-document info\n");
exit(EXIT_FAILURE);
}
char *age = strstr((char*) dstbuf, "edad");
if (!age) {
fprintf(stderr, "%s", "error: missing patient-age info\n");
exit(EXIT_FAILURE);
}
if (document >= age) {
fprintf(stderr, "%s", "error: unexpected layout\n");
exit(EXIT_FAILURE);
}
if ((age - document) >= BUFFER_SIZE) {
fprintf(stderr, "%s", "error: would overrun buffer\n");
exit(EXIT_FAILURE);
}
document += sizeof(numero);
sz = (age - document);
memset(patient_document, 0, BUFFER_SIZE);
memcpy(patient_document, document, sz);
// extracts the physician name
char *physician = strstr((char*) dstbuf, "t. profesional");
if (!physician) {
fprintf(stderr, "%s", "error: missing physician-id info\n");
exit(EXIT_FAILURE);
}
str = physician;
char *beg = physician;
char *end = physician;
int end_found = 0;
while (*str) {
if (!end_found) {
if (
((' ' == str[ 0]) || ('\n' == str[ 0])) &&
((' ' == str[-1]) || ('\n' == str[-1])) &&
((str[-2] >= 0x61) && (str[-2] < 0x7B)) &&
((str[-3] >= 0x61) && (str[-3] < 0x7B))
) {
end_found = 1;
end = &str[-1];
}
}
else if (
((str[ 0] >= 0x61) && (str[ 0] < 0x7B)) &&
((str[-1] >= 0x61) && (str[-1] < 0x7B)) &&
((' ' == str[-2]) || ('\n' == str[-2])) &&
((' ' == str[-3]) || ('\n' == str[-3]))
) {
beg = &str[-1];
break;
}
--str;
}
if ((physician == end) || (physician == beg)) {
fprintf(stderr, "%s", "error: failed to extract physician name\n");
exit(EXIT_FAILURE);
}
if ((end - beg) >= BUFFER_SIZE) {
fprintf(stderr, "%s", "error: would overrun buffer\n");
exit(EXIT_FAILURE);
}
sz = (end - beg);
memset(physician_name, 0, BUFFER_SIZE);
memcpy(physician_name, beg, sz);
fprintf(stdout, "name: %s\n", patient_name);
fprintf(stdout, "id: %s\n", patient_document);
fprintf(stdout, "physician: %s\n", physician_name);
exit(EXIT_SUCCESS);
}
char *cs = strstr((char*) dstbuf, "coopsana");
if (cs) {
fprintf(stdout, "%s", "processing: coopsana document\n");
char paciente[] = "paciente";
char *patient = strstr((char*) dstbuf, paciente);
if (!patient) {
fprintf(stderr, "%s", "error: missing patient info\n");
exit(EXIT_FAILURE);
}
char *pid = strstr((char*) dstbuf, "cedula");
if (!pid) {
fprintf(stderr, "%s", "error: missing patient-cedula info\n");
exit(EXIT_FAILURE);
}
if (patient >= pid) {
fprintf(stderr, "%s", "error: unexpected layout\n");
exit(EXIT_FAILURE);
}
if ((pid - patient) >= BUFFER_SIZE) {
fprintf(stderr, "%s", "error: would overrun buffer\n");
exit(EXIT_FAILURE);
}
patient += sizeof(paciente);
uint64_t sz = (pid - patient);
memset(placeholder, 0, BUFFER_SIZE);
memcpy(placeholder, patient, sz);
// truncates patient name so that it does not look weird on the console
int name_found = 0;
char *str = placeholder;
char *beg = placeholder;
char *end = placeholder;
while (*str) {
if (!name_found) {
if ((*str >= 0x61) && (*str < 0x7B)) {
name_found = 1;
beg = str;
}
}
else if (
((' ' == str[0]) || ('\n' == str[0])) &&
((' ' == str[1]) || ('\n' == str[1]))
) {
str[0] = 0;
end = str;
break;
}
++str;
}
memset(patient_name, 0, BUFFER_SIZE);
memcpy(patient_name, beg, (end - beg));
char cedula[] = "cedula";
char *document = strstr((char*) dstbuf, cedula);
if (!document) {
fprintf(stderr, "%s", "error: missing patient-document info\n");
exit(EXIT_FAILURE);
}
char *address = strstr((char*) document, "direccion");
if (!address) {
fprintf(stderr, "%s", "error: missing patient-address info\n");
exit(EXIT_FAILURE);
}
if (document >= address) {
fprintf(stderr, "%s", "error: unexpected layout\n");
exit(EXIT_FAILURE);
}
if ((address - document) >= BUFFER_SIZE) {
fprintf(stderr, "%s", "error: would overrun buffer\n");
exit(EXIT_FAILURE);
}
document += sizeof(cedula);
sz = (address - document);
memset(patient_document, 0, BUFFER_SIZE);
memcpy(patient_document, document, sz);
// extracts the physician name
char profesional[] = "profesional";
char *physician = strstr((char*) dstbuf, profesional);
if (!physician) {
fprintf(stderr, "%s", "error: missing physician info\n");
exit(EXIT_FAILURE);
}
document = strstr(physician, cedula);
if (!document) {
fprintf(stderr, "%s", "error: missing physician id\n");
exit(EXIT_FAILURE);
}
if (physician >= document) {
fprintf(stderr, "%s", "error: unexpected layout\n");
exit(EXIT_FAILURE);
}
if ((document - physician) >= BUFFER_SIZE) {
fprintf(stderr, "%s", "error: would overrun buffer\n");
exit(EXIT_FAILURE);
}
physician += sizeof(profesional);
sz = (document - physician);
memset(physician_name, 0, BUFFER_SIZE);
memcpy(physician_name, physician, sz);
fprintf(stdout, "name: %s\n", patient_name);
fprintf(stdout, "id: %s\n", patient_document);
fprintf(stdout, "physician: %s\n", physician_name);
exit(EXIT_SUCCESS);
}
char *ste = strstr((char*) dstbuf, "salud total eps");
if (ste) {
fprintf(stdout, "%s", "processing: salud total document\n");
char anchor[] = "tipo documento";
char *doctype = strstr((char*) dstbuf, anchor);
if (!doctype) {
fprintf(stderr, "%s", "error: missing doctype info\n");
exit(EXIT_FAILURE);
}
char nombre[] = "nombre:";
char *patient = strstr((char*) doctype, nombre);
if (!patient) {
fprintf(stderr, "%s", "error: missing patient info\n");
exit(EXIT_FAILURE);
}
char *date = strstr((char*) doctype, "fecha");
if (!date) {
fprintf(stderr, "%s", "error: missing date info\n");
exit(EXIT_FAILURE);
}
if (patient >= date) {
fprintf(stderr, "%s", "error: unexpected layout\n");
exit(EXIT_FAILURE);
}
if ((date - patient) >= BUFFER_SIZE) {
fprintf(stderr, "%s", "error: would overrun buffer\n");
exit(EXIT_FAILURE);
}
uint64_t sz = (date - (patient + sizeof(nombre)));
memset(patient_name, 0, BUFFER_SIZE);
memcpy(patient_name, patient + sizeof(nombre), sz);
char documento[] = "documento:";
char *document = strstr(doctype + sizeof(anchor), documento);
if (!document) {
fprintf(stderr, "%s", "error: missing document info\n");
exit(EXIT_FAILURE);
}
if (document >= patient) {
fprintf(stderr, "%s", "error: unexpected layout\n");
exit(EXIT_FAILURE);
}
if ((patient - document) >= BUFFER_SIZE) {
fprintf(stderr, "%s", "error: would overrun buffer\n");
exit(EXIT_FAILURE);
}
document += sizeof(documento);
sz = (patient - document);
memset(patient_document, 0, BUFFER_SIZE);
memcpy(patient_document, document, sz);
// extracts physician name
char prescritor[] = "prescritor";
char *section = strstr((char*) dstbuf, prescritor);
if (!section) {
fprintf(stderr, "%s", "error: missing physician-section info\n");
exit(EXIT_FAILURE);
}
char *physician = strstr(section, nombre);
if (!section) {
fprintf(stderr, "%s", "error: missing physician-name info\n");
exit(EXIT_FAILURE);
}
char *tel = strstr(section, "telefono");
if (!tel) {
fprintf(stderr, "%s", "error: missing physician-telephone\n");
exit(EXIT_FAILURE);
}
if (physician >= tel) {
fprintf(stderr, "%s", "error: unexpected layout\n");
exit(EXIT_FAILURE);
}
if ((tel - physician) >= BUFFER_SIZE) {
fprintf(stderr, "%s", "error: would overrun buffer\n");
exit(EXIT_FAILURE);
}
physician += sizeof(nombre);
sz = (tel - physician);
memset(physician_name, 0, BUFFER_SIZE);
memcpy(physician_name, physician, sz);
fprintf(stdout, "name: %s\n", patient_name);
fprintf(stdout, "id: %s\n", patient_document);
fprintf(stdout, "physician: %s\n", physician_name);
exit(EXIT_SUCCESS);
}
char *se = strstr((char*) dstbuf, "sanitas");
if (se) {
fprintf(stdout, "%s", "processing: sanitas document\n");
char nombre[] = "nombre";
char *patient = strstr((char*) dstbuf, nombre);
if (!patient) {
fprintf(stderr, "%s", "error: missing patient info\n");
exit(EXIT_FAILURE);
}
char *type = strstr((char*) dstbuf, "tipo");
if (!type) {
fprintf(stderr, "%s", "error: missing user-type info\n");
exit(EXIT_FAILURE);
}
if (patient >= type) {
fprintf(stderr, "%s", "error: unexpected layout\n");
exit(EXIT_FAILURE);
}
if ((type - patient) >= BUFFER_SIZE) {
fprintf(stderr, "%s", "error: would overrun buffer\n");
exit(EXIT_FAILURE);
}
patient += sizeof(nombre);
uint64_t sz = (type - patient);
memset(patient_name, 0, BUFFER_SIZE);
memcpy(patient_name, patient, sz);
char identificacion[] = "identificacion: c.c";
char *number = strstr((char*) dstbuf, identificacion);
if (!number) {
fprintf(stderr, "%s", "error: missing patient number info\n");
exit(EXIT_FAILURE);
}
char *sex = strstr((char*) dstbuf, "- sex");
if (!number) {
fprintf(stderr, "%s", "error: missing patient gender info\n");
exit(EXIT_FAILURE);
}
if (number >= sex) {
fprintf(stderr, "%s", "error: unexpected layout\n");
exit(EXIT_FAILURE);
}
if ((sex - number) >= BUFFER_SIZE) {
fprintf(stderr, "%s", "error: would overrun buffer\n");
exit(EXIT_FAILURE);
}
number += sizeof(identificacion);
sz = (sex - number);
memset(patient_document, 0, BUFFER_SIZE);
memcpy(patient_document, number, sz);
char anchor[] = " cc";
char *signature = strstr((char*) dstbuf, anchor);
if (!signature) {
fprintf(stderr, "%s", "error: missing physician document number\n");
exit(EXIT_FAILURE);
}
int found_title = 0;
char *beg = signature;
char *end = signature;
char *str = signature;
while (*str) {
if (!found_title) {
if ((' ' == str[0]) && (' ' == str[-1])) {
found_title = 1;
end = &str[-1];
}
}
else if (
((' ' == str[0]) && (' ' == str[-1])) ||
((' ' == str[0]) && ((str[-1] < 0x61) || (str[-1] > 0x7A)))
) {
beg = &str[1];
break;
}
--str;
}
if ((signature == end) || (signature == beg)) {
fprintf(stderr, "%s", "error: failed to extract physician name\n");
exit(EXIT_FAILURE);
}
if ((end - beg) >= BUFFER_SIZE) {
fprintf(stderr, "%s", "error: would overrun buffer\n");
exit(EXIT_FAILURE);
}
sz = (end - beg);
memset(physician_name, 0, BUFFER_SIZE);
memcpy(physician_name, beg, sz);
fprintf(stdout, "name: %s\n", patient_name);
fprintf(stdout, "id: %s\n", patient_document);
fprintf(stdout, "physician: %s\n", physician_name);
exit(EXIT_SUCCESS);
}
char *sure = strstr((char*) dstbuf, "sura");
if (sure) {
fprintf(stdout, "%s", "processing: sura document\n");
char ccpattern[] = "cc -";
char *cc = strstr((char*) dstbuf, ccpattern);
if (!cc) {
fprintf(stderr, "%s", "error: missing patient id\n");
exit(EXIT_FAILURE);
}
// finds the initial part of the patient name
int sw = 0;
char *str = cc;
str += sizeof(ccpattern);
while (*str) {
if ((*str >= 0x61) && (*str < 0x7B)) {
sw = 1;
break;
}
++str;
}
if (!sw) {
fprintf(stderr, "%s", "error: missing patient name\n");
exit(EXIT_FAILURE);
}
if ((str - cc) >= BUFFER_SIZE) {
fprintf(stderr, "%s", "error: would overrun buffer\n");
exit(EXIT_FAILURE);
}
// extracts the document id (cedula)
uint64_t sz = (str - (cc + sizeof(ccpattern)));
memset(patient_document, 0, BUFFER_SIZE);
memcpy(patient_document, cc + sizeof(ccpattern), sz);
char afiliado[] = "afiliado";
char *patient = strstr((char*) dstbuf, afiliado);
if (!patient) {
fprintf(stderr, "%s", "error: missing patient info\n");
exit(EXIT_FAILURE);
}
if (str >= patient) {
fprintf(stderr, "%s", "error: surprising layout\n");
exit(EXIT_FAILURE);
}
if ((str - patient) >= BUFFER_SIZE) {
fprintf(stderr, "%s", "error: would overrun buffer\n");
exit(EXIT_FAILURE);
}
// copy the first part of the name
sz = (patient - str);
memset(patient_name, 0, BUFFER_SIZE);
memcpy(patient_name, str, sz);
char *truncate = patient_name;
while (*truncate) {
if (
((' ' == truncate[0]) || ('\n' == truncate[0])) &&
((' ' == truncate[1]) || ('\n' == truncate[1]))
) {
truncate[0] = ' ';
truncate[1] = 0;
break;
}
++truncate;
}
patient += sizeof(afiliado);
while (*patient) {
if ((*patient >= 0x61) && (*patient < 0x7B)) {
break;
}
++patient;
}
char *type = strstr((char*) dstbuf, "ips afiliado");
if (!type) {
fprintf(stderr, "%s", "error: missing patient-type info\n");
exit(EXIT_FAILURE);
}
if (patient >= type) {
fprintf(stderr, "%s", "error: unexpected layout\n");
exit(EXIT_FAILURE);
}
if ((type - patient) >= BUFFER_SIZE) {
fprintf(stderr, "%s", "error: would overrun buffer\n");
exit(EXIT_FAILURE);
}
sz = (type - patient);
if ((strlen(patient_name) + sz + 1) >= BUFFER_SIZE) {
fprintf(stderr, "%s", "error: would overrun buffer\n");
exit(EXIT_FAILURE);
}
strncat(patient_name, patient, sz);
// truncates patient name so that it does not look weird on the console
str = patient_name;
while (*str) {
if (
((' ' == str[0]) || ('\n' == str[0])) &&
((' ' == str[1]) || ('\n' == str[1]))
) {
str[0] = 0;
break;
}
++str;
}
// extracts physician name
char *physician = strstr(type, ccpattern);
if (!physician) {
fprintf(stderr, "%s", "error: missing physician document number\n");
exit(EXIT_FAILURE);
}
physician += sizeof(ccpattern);
str = physician;
while (*str) {
if ((str[0] >= 0x61) && (str[0] < 0x7B)) {
break;
}
++str;
}
physician = str;
char *pat = strstr(type, "responsable");
if (!pat) {
fprintf(stderr, "%s", "error: unexpected layout\n");
exit(EXIT_FAILURE);
}
if (physician >= pat) {
fprintf(stderr, "%s", "error: unexpected layout\n");
exit(EXIT_FAILURE);
}
if ((pat - physician) >= BUFFER_SIZE) {
fprintf(stderr, "%s", "error: would overrun buffer\n");
exit(EXIT_FAILURE);
}
sz = (pat - physician);
memset(physician_name, 0, BUFFER_SIZE);
memcpy(physician_name, physician, sz);
fprintf(stdout, "name: %s\n", patient_name);
fprintf(stdout, "id: %s\n", patient_document);
fprintf(stdout, "physician: %s\n", physician_name);
exit(EXIT_SUCCESS);
}
return 0;
}