forked from richgel999/ufo_data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudb.cpp
1805 lines (1517 loc) · 58.1 KB
/
udb.cpp
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
// Copyright (C) 2023 Richard Geldreich, Jr.
#include "udb.h"
#include "udb_tables.h"
const uint32_t UDB_RECORD_SIZE = 112;
const uint32_t UDB_REC_TEXT_SIZE = 78;
enum
{
cFlagMAP, cFlagGND, cFlagCST, cFlagSEA, cFlagAIR, cFlagObsMIL, cFlagObsCIV, cFlagHQO, // loc/obs flags
cFlagSCI, cFlagTLP, cFlagNWS, cFlagMID, cFlagHOX, cFlagCNT, cFlagODD, cFlagWAV, // misc flags
cFlagSCR, cFlagCIG, cFlagDLT, cFlagNLT, cFlagPRB, cFlagFBL, cFlagSUB, cFlagNFO, // type of ufo craft flags
cFlagOID, cFlagRBT, cFlagPSH, cFlagMIB, cFlagMON, cFlagGNT, cFlagFIG, cFlagNOC, // aliens monsters flags
cFlagOBS, cFlagRAY, cFlagSMP, cFlagMST, cFlagABD, cFlagOPR, cFlagSIG, cFlagCVS, // apparent ufo occupant activities flags
cFlagNUC, cFlagDRT, cFlagVEG, cFlagANI, cFlagHUM, cFlagVEH, cFlagBLD, cFlagLND, // places visited and things affected flags
cFlagPHT, cFlagRDR, cFlagRDA, cFlagEME, cFlagTRC, cFlagTCH, cFlagHST, cFlagINJ, // evidence and special effects flags
cFlagMIL, cFlagBBK, cFlagGSA, cFlagOGA, cFlagSND, cFlagODR, cFlagCOV, cFlagCMF, // misc details flags
cTotalFlags = 64
};
#pragma pack(push, 1)
struct udb_rec
{
private:
int16_t m_year;
uint8_t m_unknown_and_locale; // nibbles
uint8_t m_unknown_and_month; // nibbles
uint8_t m_ref_index_high_day; // 3 bits ref index high, low 5 bits day
uint8_t m_time;
uint8_t m_ymdt; // 2-bit fields: TDMY accuracy, T lowest, 0=invalid, 1=?, 2=~, 3=accurate
uint8_t m_duration;
uint8_t m_unknown1;
int16_t m_enc_longtitude;
int16_t m_enc_latitude;
int16_t m_elevation;
int16_t m_rel_altitude;
uint8_t m_unknown2;
uint8_t m_continent_country; // nibbles
uint8_t m_state_or_prov[3];
uint8_t m_unknown3;
#if 0
uint8_t m_loc_flags;
uint8_t m_misc_flags;
uint8_t m_type_of_ufo_craft_flags;
uint8_t m_aliens_monsters_flags;
uint8_t m_apparent_ufo_occupant_activities_flags;
uint8_t m_places_visited_and_things_affected_flags;
uint8_t m_evidence_and_special_effects_flags;
uint8_t m_miscellaneous_details_flags;
#else
uint8_t m_flags[8];
#endif
uint8_t m_text[UDB_REC_TEXT_SIZE];
uint8_t m_reference;
uint8_t m_ref_index;
uint8_t m_strangeness_credibility; // nibbles
public:
const uint8_t* get_text() const { return m_text; }
int get_year() const { return m_year; }
uint32_t get_month() const { return m_unknown_and_month & 0xF; }
uint32_t get_day() const { return m_ref_index_high_day & 31; }
// meters
int get_elevation() const { return m_elevation; }
int get_rel_altitude() const { return m_rel_altitude; }
uint32_t get_strangeness() const { return m_strangeness_credibility >> 4; }
uint32_t get_credibility() const { return m_strangeness_credibility & 0xF; }
uint32_t get_reference() const { return m_reference; }
uint32_t get_reference_index() const { return m_ref_index | ((m_ref_index_high_day >> 5) << 8); }
uint32_t get_continent_code() const { return m_continent_country >> 4; }
uint32_t get_country_code() const { return m_continent_country & 0xF; }
uint32_t get_locale() const { return m_unknown_and_locale & 0xF; }
std::string get_state_or_prov() const
{
const uint32_t c0 = m_state_or_prov[0];
const uint32_t c1 = m_state_or_prov[1];
const uint32_t c2 = m_state_or_prov[2];
return dos_to_utf8(string_format("%c%c%c", (c0 >= ' ') ? c0 : ' ', (c1 >= ' ') ? c1 : ' ', (c2 >= ' ') ? c2 : ' '));
}
double get_latitude() const { return ((double)m_enc_latitude / 200.0f) * 1.11111111111f; }
double get_longitude() const { return -((double)m_enc_longtitude / 200.0f) * 1.11111111111f; }
std::string get_latitude_dms() const { double lat = get_latitude(); return get_deg_to_dms(lat) + ((lat <= 0) ? " S" : " N"); }
std::string get_longitude_dms() const { double lon = get_longitude(); return get_deg_to_dms(lon) + ((lon <= 0) ? " W" : " E"); }
// minutes
uint32_t get_duration() const { return m_duration; }
enum
{
cAccuracyInvalid = 0,
cAccuracyQuestionable = 1,
cAccuracyApproximate = 2,
cAccuracyGood = 3
};
bool get_time(std::string& time) const
{
uint32_t time_accuracy = m_ymdt & 3;
if (time_accuracy == cAccuracyInvalid)
return false;
uint32_t hour = m_time / 6;
uint32_t minute = (m_time % 6) * 10;
if (hour > 23)
{
assert(0);
return false;
}
time = string_format("%02u:%02u", hour, minute);
if (time_accuracy == cAccuracyQuestionable)
time += "?";
else if (time_accuracy == cAccuracyApproximate)
time = "~" + time;
return true;
}
bool get_date(event_date& date) const
{
uint32_t year_accuracy = (m_ymdt >> 6) & 3;
uint32_t month_accuracy = (m_ymdt >> 4) & 3;
uint32_t day_accuracy = (m_ymdt >> 2) & 3;
int year = year_accuracy ? get_year() : 0;
uint32_t month = month_accuracy ? get_month() : 0;
uint32_t day = day_accuracy ? get_day() : 0;
if ((day < 1) || (day > 31))
{
day = 0;
day_accuracy = cAccuracyInvalid;
}
if ((month < 1) || (month > 12))
{
month = 0;
month_accuracy = cAccuracyInvalid;
}
if (!year)
return false;
uint32_t min_accuracy = year;
date.m_year = year;
if (month)
{
date.m_month = month;
if (!day)
{
min_accuracy = std::min(year_accuracy, month_accuracy);
}
else
{
min_accuracy = std::min(std::min(year_accuracy, month_accuracy), day_accuracy);
date.m_day = day;
}
}
if (min_accuracy == cAccuracyApproximate)
date.m_approx = true;
else if (min_accuracy == cAccuracyQuestionable)
date.m_fuzzy = true;
return true;
}
enum { cMaxFlags = 64 };
// LOC, MISC, TYPE, ALIENS/MONSTERS, ACTIVITIES, VISITED/THINGS, EVIDENCE/SPECIAL, MISC_DETAILS
bool get_flag(uint32_t index) const
{
assert(index < cMaxFlags);
return (m_flags[index >> 3] & (1 << (index & 7))) != 0;
}
#if 0
uint8_t get_loc_flags() const { return m_loc_flags; }
uint8_t get_misc_flags() const { return m_misc_flags; }
uint8_t get_type_of_ufo_craft_flags() const { return m_type_of_ufo_craft_flags; }
uint8_t get_aliens_monsters_flags() const { return m_aliens_monsters_flags; }
uint8_t get_apparent_ufo_occupant_activities_flags() const { return m_apparent_ufo_occupant_activities_flags; }
uint8_t get_places_visited_and_things_affected_flags() const { return m_places_visited_and_things_affected_flags; }
uint8_t get_evidence_and_special_effects_flags() const { return m_evidence_and_special_effects_flags; }
uint8_t get_miscellaneous_details_flags() const { return m_miscellaneous_details_flags; }
#endif
void get_geo(std::string& country_name, std::string& state_or_prov_name) const
{
std::string state_or_prov_str(get_state_or_prov());
string_trim_end(state_or_prov_str);
if (state_or_prov_str.back() == '.')
state_or_prov_str.pop_back();
if (state_or_prov_str.back() == '.')
state_or_prov_str.pop_back();
get_hatch_geo(get_continent_code(), get_country_code(), state_or_prov_str, country_name, state_or_prov_name);
if (state_or_prov_str == "UNK")
state_or_prov_name = "Unknown";
}
std::string get_full_refs() const
{
std::string ref(g_hatch_refs_tab[get_reference()]);
if (g_hatch_refs_tab[get_reference()])
{
uint32_t ref_index = get_reference_index();
if (get_reference() == 93)
{
for (const auto& x : g_hatch_refs_93)
if (x.m_ref == ref_index)
{
ref += x.m_pDesc;
break;
}
}
else if (get_reference() == 96)
{
for (const auto& x : g_hatch_refs_96)
if (x.m_ref == ref_index)
{
ref += x.m_pDesc;
break;
}
}
else if (get_reference() == 97)
{
for (const auto& x : g_hatch_refs_97)
if (x.m_ref == ref_index)
{
ref += x.m_pDesc;
break;
}
}
else if (get_reference() == 98)
{
for (const auto& x : g_hatch_refs_98)
if (x.m_ref == ref_index)
{
ref += x.m_pDesc;
break;
}
}
else
{
ref += string_format(" (Index %u)", ref_index);
}
}
return ref;
}
};
#pragma pack(pop)
static std::unordered_map<std::string, std::string> g_dictionary;
struct token
{
std::string m_token;
bool m_cap_check;
bool m_replaced_flag;
token() :
m_cap_check(false),
m_replaced_flag(false)
{
}
token(const std::string& token, bool cap_check, bool replaced_flag) :
m_token(token),
m_cap_check(cap_check),
m_replaced_flag(replaced_flag)
{
}
};
std::unordered_set<std::string> g_unique_tokens;
std::vector<string_vec> g_hatch_exception_tokens;
static void init_hatch_cap_exception_tokens()
{
g_hatch_exception_tokens.resize(std::size(g_cap_exceptions));
std::string cur_etoken;
for (uint32_t e = 0; e < std::size(g_cap_exceptions); e++)
{
const std::string exception_str(g_cap_exceptions[e]);
string_vec& etokens = g_hatch_exception_tokens[e];
for (uint32_t i = 0; i < exception_str.size(); i++)
{
uint8_t c = exception_str[i];
if (c == ' ')
{
if (cur_etoken.size())
{
etokens.push_back(cur_etoken);
cur_etoken.clear();
}
}
else if (c == '-')
{
if (cur_etoken.size())
{
etokens.push_back(cur_etoken);
cur_etoken.clear();
}
std::string s;
s.push_back(c);
etokens.push_back(s);
}
else
{
cur_etoken.push_back(c);
}
}
if (cur_etoken.size())
{
etokens.push_back(cur_etoken);
cur_etoken.resize(0);
}
}
}
static std::string fix_capitilization(std::vector<token>& toks, uint32_t& tok_index)
{
if (toks[tok_index].m_replaced_flag)
return toks[tok_index].m_token;
const uint32_t toks_remaining = (uint32_t)toks.size() - tok_index;
// Peak ahead on the tokens to see if we need to correct any capitilization using the exception table.
for (uint32_t e = 0; e < std::size(g_cap_exceptions); e++)
{
const string_vec& etokens = g_hatch_exception_tokens[e];
if (toks_remaining >= etokens.size())
{
uint32_t i;
for (i = 0; i < etokens.size(); i++)
if ((string_icompare(etokens[i], toks[tok_index + i].m_token.c_str()) != 0) || toks[tok_index + i].m_replaced_flag)
break;
if (i == etokens.size())
{
for (i = 0; i < etokens.size(); i++)
{
toks[tok_index + i].m_token = etokens[i];
toks[tok_index + i].m_replaced_flag = true;
}
std::string res(toks[tok_index].m_token);
return res;
}
}
}
std::string str(toks[tok_index].m_token);
if (!toks[tok_index].m_cap_check)
return str;
string_vec wtokens;
std::string cur_wtoken;
for (uint32_t i = 0; i < str.size(); i++)
{
uint8_t c = str[i];
if (isalpha(c) || isdigit(c) || ((c == '\'') && (i != 0) && (i != str.size() - 1)))
{
cur_wtoken.push_back(c);
}
else
{
if (cur_wtoken.size())
{
wtokens.push_back(cur_wtoken);
cur_wtoken.clear();
}
std::string s;
s.push_back(c);
wtokens.push_back(s);
}
}
if (cur_wtoken.size())
{
wtokens.push_back(cur_wtoken);
cur_wtoken.clear();
}
for (uint32_t wtoken_index = 0; wtoken_index < wtokens.size(); wtoken_index++)
{
std::string& substr = wtokens[wtoken_index];
if (substr == "A")
substr = "a";
else if (substr.size() >= 2)
{
bool is_all_uppercase = true;
for (uint8_t c : substr)
{
if (!isupper(c) && (c != '\''))
{
is_all_uppercase = false;
break;
}
}
if (is_all_uppercase)
{
auto res = g_dictionary.find(string_lower(substr));
if (res != g_dictionary.end())
{
substr = res->second;
}
else
{
substr = string_lower(substr);
g_unique_tokens.insert(substr);
}
}
}
}
std::string res;
for (uint32_t wtoken_index = 0; wtoken_index < wtokens.size(); wtoken_index++)
res += wtokens[wtoken_index];
return res;
}
static std::unordered_map<std::string, hatch_abbrev> g_hatch_abbreviations_map;
static void init_hatch_abbreviations_map()
{
for (uint32_t abbrev_index = 0; abbrev_index < std::size(g_hatch_abbreviations); abbrev_index++)
{
auto res = g_hatch_abbreviations_map.insert(std::make_pair(string_lower(g_hatch_abbreviations[abbrev_index].pAbbrev), g_hatch_abbreviations[abbrev_index]));
if (!res.second)
panic("Mutiple Hatch abbreviation: %s", res.first->first.c_str());
}
}
// Expand abbreviations
static void expand_abbreviations_internal(bool first_line, std::string orig_token, const string_vec& tokens, uint32_t cur_tokens_index, std::vector<token>& toks)
{
const uint32_t MAX_ABBREVS = 5;
uint32_t k;
for (k = 0; k < MAX_ABBREVS; k++)
{
std::string new_token(orig_token);
auto find_res = g_hatch_abbreviations_map.find(string_lower(orig_token));
if (find_res != g_hatch_abbreviations_map.end())
{
if (!first_line || !find_res->second.m_forbid_firstline)
{
new_token = find_res->second.pExpansion;
if (new_token.size())
toks.push_back(token(new_token, !first_line && (new_token == orig_token), false));
break;
}
}
if ((orig_token.size() >= 4) && (uisupper(orig_token[0])))
{
std::string month_suffix(orig_token);
month_suffix.erase(0, 3);
if ((month_suffix.size() <= 4) && string_is_digits(month_suffix))
{
std::string month_prefix(orig_token);
month_prefix.erase(3, month_prefix.size() - 3);
std::string search_prefix(string_upper(month_prefix));
static const char* g_hmonths[12] =
{
"JAN", "FEB", "MAR", "APR", "MAY", "JUN",
"JLY", "AUG", "SEP", "OCT", "NOV", "DEC"
};
uint32_t m;
for (m = 0; m < 12; m++)
if (search_prefix == g_hmonths[m])
break;
if (m < 12)
{
toks.push_back(token(g_months[m], !first_line, false));
// TODO: This can be improved by checking the # before the token
long long val = atoll(month_suffix.c_str());
if (val > 31)
month_suffix = '\'' + month_suffix;
toks.push_back(token(month_suffix, !first_line, false));
break;
}
}
}
size_t p;
if ((p = orig_token.find_first_of('.')) == std::string::npos)
{
// No period(s) - we're done.
if (new_token.size())
toks.push_back(token(new_token, !first_line, false));
break;
}
// Specifically detect abbrev. first names like "A." etc. and expand them.
if (!first_line && (orig_token.size() > 4) && (p == 1) && uisupper(orig_token[0]) && uisupper(orig_token[2]))
{
std::string first_name(orig_token);
first_name.erase(2, first_name.size() - 2);
toks.push_back(token(first_name, false, false));
orig_token.erase(0, p + 1);
}
else
{
// Detect words starting with an abbreviation ending in "."
std::string prefix(orig_token);
prefix.erase(p + 1, prefix.size() - (p + 1));
find_res = g_hatch_abbreviations_map.find(string_lower(prefix));
if ((find_res != g_hatch_abbreviations_map.end()) && (!first_line || !find_res->second.m_forbid_firstline))
{
new_token = find_res->second.pExpansion;
toks.push_back(token(new_token, false, false));
orig_token.erase(0, p + 1);
}
else
{
if (new_token.size())
toks.push_back(token(new_token, !first_line, false));
break;
}
}
} // k
if (k == MAX_ABBREVS)
{
if (orig_token.size())
toks.push_back(token(orig_token, !first_line, false));
}
}
static bool is_sentence_ender(uint8_t c)
{
return (c == '!') || (c == '.') || (c == '?');
}
static void expand_abbreviations(bool first_line, std::string orig_token, const string_vec& tokens, uint32_t cur_tokens_index, std::vector<token>& toks)
{
std::string new_token(orig_token);
// Temporarily remove " and ' prefix/suffix chars from the token, before the abbrev checks.
std::string prefix_char, suffix_char;
if (orig_token.size() >= 3)
{
if ((orig_token[0] == '\'') || (orig_token[0] == '\"'))
{
prefix_char.push_back(orig_token[0]);
orig_token.erase(0, 1);
new_token = orig_token;
}
if ((orig_token.back() == '\'') || (orig_token.back() == '\"'))
{
suffix_char.push_back(orig_token.back());
orig_token.pop_back();
new_token = orig_token;
}
}
const size_t first_tok = toks.size();
expand_abbreviations_internal(first_line, orig_token, tokens, cur_tokens_index, toks);
const size_t num_toks = toks.size() - first_tok;
assert(num_toks);
const size_t last_tok = first_tok + num_toks - 1;
if (prefix_char.size())
toks[first_tok].m_token = prefix_char + toks[first_tok].m_token;
if (suffix_char.size())
toks[last_tok].m_token = toks[last_tok].m_token + suffix_char;
}
static std::string decode_hatch(const std::string& str, bool first_line)
{
std::string res;
string_vec tokens;
std::string cur_token;
bool inside_space = false;
int prev_c = -1;
// Phase 1: Tokenize the input string based off examination of (mostly) individual chars, previous chars and upcoming individual chars.
for (uint32_t i = 0; i < str.size(); i++)
{
uint8_t c = str[i];
const bool is_two_dots = (c == '.') && ((i + 1) < str.size()) && (str[i + 1] == '.');
const bool is_one_equals = (c == '1') && ((i + 1) < str.size()) && (str[i + 1] == '=');
const bool prev_is_digit = i && uisdigit(str[i - 1]);
const bool next_is_plus = ((i + 1) < str.size()) && (str[i + 1] == '+');
//const bool has_prev = (i != 0);
//const bool has_next = (i + 1) < str.size();
if (c == ' ')
{
if (cur_token.size())
{
tokens.push_back(cur_token);
cur_token.clear();
}
inside_space = true;
}
else if (is_one_equals)
{
if (cur_token.size())
{
tokens.push_back(cur_token);
cur_token.clear();
}
tokens.push_back("1=");
i++;
inside_space = false;
}
else if (
(c == ';') || ((c >= 0x18) && (c <= 0x1b)) || (c == '<') || (c == '>') ||
(c == '=') ||
(c == '/') ||
(c == ',') ||
(c == '?') || (c == '!') ||
((!prev_is_digit || next_is_plus) && (c == '+')) ||
(c == '@') || (c == '-') ||
is_two_dots
)
{
if (cur_token.size())
{
tokens.push_back(cur_token);
cur_token.clear();
}
std::string s;
s.push_back(c);
if (is_two_dots)
{
s += ".";
i++;
}
tokens.push_back(s);
inside_space = false;
}
else
{
cur_token.push_back(c);
inside_space = false;
if ((c == 0xf8) || // code page 437 degree sym
(prev_is_digit && (c == '+') && !next_is_plus))
{
tokens.push_back(cur_token);
cur_token.clear();
}
}
prev_c = c;
}
if (cur_token.size())
tokens.push_back(cur_token);
// Phase 2: Exceptional fixups that change or split tokens up into multiple tokens.
string_vec new_tokens;
for (uint32_t i = 0; i < tokens.size(); i++)
{
std::string tok(tokens[i]);
// Convert "BBK#"
if (string_begins_with(tok, "BBK#") && (tok.size() > 4))
{
new_tokens.push_back("Project Bluebook Case #");
tok.erase(0, 4);
new_tokens.push_back(tok);
continue;
}
// Split "k'alt"
if (string_ends_in(tok, "k'alt"))
{
tok.erase(tok.size() - 3, 3);
new_tokens.push_back(tok);
new_tokens.push_back("Alt");
continue;
}
// Convert "HI+LO"
if ((i + 2 < tokens.size()) && (tokens[i] == "HI") && (tokens[i + 1] == "+") && (tokens[i + 2] == "LO"))
{
tokens.push_back("high and low");
i += 2;
continue;
}
// Don't split "4rth" to "4 rth" etc.
if ((string_icompare(tok, "4RTH") == 0) || (string_icompare(tok, "3rds") == 0) || (string_icompare(tok, "16th") == 0))
{
new_tokens.push_back(tok);
continue;
}
if (string_ends_in(tok, "Kmph"))
{
new_tokens.push_back(tok);
continue;
}
if (tok == "12Ocm")
{
new_tokens.push_back("120cm");
continue;
}
if (string_icompare(tok, "3OOM") == 0)
{
new_tokens.push_back("300m");
continue;
}
// If the first char isn't a digit then just continue now, because the rest of this code is concerned with splitting numbers away from words.
if (!isdigit(tok[0]))
{
new_tokens.push_back(tok);
continue;
}
if (tok.size() >= 3)
{
// Check for 1-7 digits then ' followed by 1- letters and split
uint32_t j;
for (j = 1; j < tok.size(); j++)
if (tok[j] == '\'')
break;
if ((j < tok.size()) && (j != tok.size() - 1) && (j <= 7))
{
uint32_t k;
for (k = 1; k < j; k++)
if (!uisdigit(tok[k]) && (utolower(tok[k]) != 'x') && (utolower(tok[k]) != 'k') && (tok[k] != '.'))
break;
if ((k == j) && (uisalpha(tok[j + 1])))
{
int sp = j + 1;
std::string new_tok(tok);
new_tok.erase(0, sp);
std::string n(tok);
n.erase(sp, n.size() - sp);
new_tokens.push_back(n);
new_tokens.push_back(new_tok);
continue;
}
}
}
// Won't split digits away for tokens < 4 chars
if ((tok.size() < 4) || (tok == "6F6s"))
{
new_tokens.push_back(tok);
continue;
}
// Check for 1-2 digits and alpha and split
// TODO: support 3-4 digits
int split_point = -1;
if (uisalpha(tok[1]))
split_point = 1;
else if (uisdigit(tok[1]) && uisalpha(tok[2]) && uisalpha(tok[3]))
split_point = 2;
if (split_point > 0)
{
std::string new_tok(tok);
new_tok.erase(0, split_point);
// Don't split the number digits from some special cases, like hr, cm, mph, etc.
if ((string_icompare(new_tok, "hr") != 0) &&
(string_icompare(new_tok, "nd") != 0) &&
(string_icompare(new_tok, "kw") != 0) &&
(string_icompare(new_tok, "cm") != 0) &&
(string_icompare(new_tok, "km") != 0) &&
(string_icompare(new_tok, "mph") != 0) &&
(string_icompare(new_tok, "kph") != 0) &&
(!string_begins_with(new_tok, "K'")))
{
std::string n(tok);
n.erase(split_point, n.size() - split_point);
new_tokens.push_back(n);
if (new_tok == "min")
new_tok = "minute(s)";
new_tokens.push_back(new_tok);
}
else
{
new_tokens.push_back(tok);
}
}
else
{
new_tokens.push_back(tok);
}
}
tokens.swap(new_tokens);
std::vector<token> toks;
// Phase 3: Compose new string, expanding abbreviations and tokens to one or more words, or combining together special sequences of tokens into specific phrases.
// Also try to carefully insert spaces into the output, as needed.
for (uint32_t i = 0; i < tokens.size(); i++)
{
const uint32_t num_tokens_left = ((uint32_t)tokens.size() - 1) - i;
const bool has_prev_token = i > 0, has_next_token = (i + 1) < tokens.size();
const bool next_token_is_slash = (has_next_token) && (tokens[i + 1][0] == '/');
bool is_next_dir = false;
if (has_next_token)
{
uint32_t ofs = 1;
if (tokens[i + 1] == ">")
{
ofs = 2;
}
if ((i + ofs) < tokens.size())
{
std::string next_tok = string_upper(tokens[i + ofs]);
if ((next_tok.back() == '.') && (next_tok.size() >= 2))
next_tok.pop_back();
if ((next_tok == "N") || (next_tok == "S") || (next_tok == "E") || (next_tok == "W") ||
(next_tok == "SW") || (next_tok == "SE") || (next_tok == "NW") || (next_tok == "NE") ||
(next_tok == "NNE") || (next_tok == "NNW") || (next_tok == "SSE") || (next_tok == "SSW") ||
(next_tok == "ESE"))
{
is_next_dir = true;
}
}
}
std::string orig_token(tokens[i]);
std::string new_token(orig_token);
if (!orig_token.size())
continue;
// Handle various exceptions before expending abbreviations
// TODO: Refactor to table(s)
// Special handling for RUSS/RUSS.
if ((tokens[i] == "RUSS") || (tokens[i] == "RUSS.") || (tokens[i] == "RUS") || (tokens[i] == "RUS."))
{
if (first_line)
new_token = "Russia";
else
new_token = "Russian";
}
// AA FLITE #519 - exception
// AA LINER
else if ((tokens[i] == "AA") && (num_tokens_left >= 1) && ((tokens[i + 1] == "FLITE#519") || (tokens[i + 1] == "LINER")))
{
new_token = "AA";
}
// bright Lt.
else if ((tokens[i] == "VBRITE") && (num_tokens_left >= 1) && (tokens[i + 1] == "LT"))
{
new_token = "vibrant bright light";
i++;
}
// ENERGY SRC
else if ((tokens[i] == "ENERGY") && (num_tokens_left >= 1) && (tokens[i + 1] == "SRC"))
{
new_token = "energy source";
i++;
}
// mid air - exception
else if ((tokens[i] == "MID") && (num_tokens_left >= 1) && (tokens[i + 1] == "AIR"))
{
new_token = "mid";
}
// /FORMN or /formation - exception
else if ((string_icompare(tokens[i], "/") == 0) && (num_tokens_left >= 1) && ((string_icompare(tokens[i + 1], "FORMN") == 0) || (string_icompare(tokens[i + 1], "formation") == 0)))
{
new_token = "in formation";
i++;
}
// /FORMNs - exception
else if ((string_icompare(tokens[i], "/") == 0) && (num_tokens_left >= 1) && ((string_icompare(tokens[i + 1], "FORMNs") == 0) || (string_icompare(tokens[i + 1], "formations") == 0)))
{
new_token = "in formations";
i++;
}
// LOST/CLOUDS - exception
else if ((string_icompare(tokens[i], "LOST") == 0) && (num_tokens_left >= 2) && (tokens[i + 1] == "/") && (string_icompare(tokens[i + 2], "CLOUDS") == 0))
{
new_token = "lost in clouds";
i += 2;
}
// LOST/DISTANCE - exception
else if ((string_icompare(tokens[i], "LOST") == 0) && (num_tokens_left >= 2) && (tokens[i + 1] == "/") && (string_icompare(tokens[i + 2], "DISTANCE") == 0))
{
new_token = "lost in the distance";
i += 2;
}
// W-carbide - exception