-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmisc.cpp
1268 lines (1092 loc) · 31.7 KB
/
misc.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
//
// AYA version 5
//
// 雑用関数
// written by umeici. 2004
//
#if defined(WIN32) || defined(_WIN32_WCE)
# include "stdafx.h"
#endif
#include <ctime>
#include <string>
#include <vector>
#include "manifest.h"
#include "misc.h"
#if defined(POSIX) || defined(__MINGW32__)
# include "posix_utils.h"
#endif
#include "globaldef.h"
#include "wsex.h"
#include "function.h"
#include "sysfunc.h"
//////////DEBUG/////////////////////////
#ifdef _WINDOWS
#ifdef _DEBUG
#include <crtdbg.h>
#define new new( _NORMAL_BLOCK, __FILE__, __LINE__)
#endif
#endif
////////////////////////////////////////
/* -----------------------------------------------------------------------
* 関数名 : Split
* 機能概要: 文字列を分割して余分な空白を削除します
*
* 返値 : 0/1=失敗/成功
* -----------------------------------------------------------------------
*/
char Split(const yaya::string_t &str, yaya::string_t &dstr0, yaya::string_t &dstr1, const yaya::char_t *sepstr)
{
yaya::string_t::size_type seppoint = str.find(sepstr);
if (seppoint == yaya::string_t::npos) {
dstr0 = str;
dstr1.erase();
return 0;
}
dstr0.assign(str, 0, seppoint);
seppoint += ::wcslen(sepstr);
dstr1.assign(str, seppoint, str.size() - seppoint);
CutSpace(dstr0);
CutSpace(dstr1);
return 1;
}
//----
char Split(const yaya::string_t &str, yaya::string_t &dstr0, yaya::string_t &dstr1, const yaya::string_t &sepstr)
{
yaya::string_t::size_type seppoint = str.find(sepstr);
if (seppoint == yaya::string_t::npos) {
dstr0 = str;
dstr1.erase();
return 0;
}
dstr0.assign(str, 0, seppoint);
seppoint += sepstr.size();
dstr1.assign(str, seppoint, str.size() - seppoint);
CutSpace(dstr0);
CutSpace(dstr1);
return 1;
}
/* -----------------------------------------------------------------------
* 関数名 : SplitOnly
* 機能概要: 文字列を分割します
*
* 返値 : 0/1=失敗/成功
* -----------------------------------------------------------------------
*/
char SplitOnly(const yaya::string_t &str, yaya::string_t &dstr0, yaya::string_t &dstr1, const yaya::char_t *sepstr)
{
yaya::string_t::size_type seppoint = str.find(sepstr);
if (seppoint == yaya::string_t::npos) {
dstr0 = str;
dstr1.erase();
return 0;
}
dstr0.assign(str, 0, seppoint);
seppoint += ::wcslen(sepstr);
dstr1.assign(str, seppoint, str.size() - seppoint);
return 1;
}
/* -----------------------------------------------------------------------
* 関数名 : Find_IgnoreDQ
* 機能概要: ダブル/シングルクォート内を無視して文字列を検索
*
* 返値 : 負=失敗 0・正=成功
* -----------------------------------------------------------------------
*/
yaya::string_t::size_type Find_IgnoreDQ(const yaya::string_t &str, const yaya::char_t *findstr)
{
yaya::string_t::size_type findpoint = 0;
yaya::string_t::size_type nextdq = 0;
while(true){
findpoint = str.find(findstr, findpoint);
if (findpoint == yaya::string_t::npos)
return yaya::string_t::npos;
nextdq = IsInDQ(str, nextdq, findpoint);
if (nextdq >= IsInDQ_npos) {
if (nextdq == IsInDQ_runaway) { //クオートが終わらないまま終了
return yaya::string_t::npos;
}
break; //みつかった
}
else { //クオート内部。無視して次へ
findpoint = nextdq;
}
}
return findpoint;
}
yaya::string_t::size_type Find_IgnoreDQ(const yaya::string_t &str, const yaya::string_t &findstr)
{
return Find_IgnoreDQ(str,findstr.c_str());
}
/* -----------------------------------------------------------------------
* 関数名 : find_last_str
* 機能概要: 一番最後に見つかった文字列の位置を返す
*
* 返値 : npos=失敗 0・正=成功
* -----------------------------------------------------------------------
*/
yaya::string_t::size_type find_last_str(const yaya::string_t &str, const yaya::char_t *findstr)
{
yaya::string_t::size_type it = yaya::string_t::npos;
yaya::string_t::size_type found;
while ( (found = str.find(findstr,it)) != yaya::string_t::npos ) {
it = found;
}
return it;
}
yaya::string_t::size_type find_last_str(const yaya::string_t &str, const yaya::string_t &findstr)
{
return find_last_str(str,findstr.c_str());
}
/* -----------------------------------------------------------------------
* 関数名 : Split_IgnoreDQ
* 機能概要: 文字列を分割して余分な空白を削除します
* ただしダブル/シングルクォート内では分割しません
*
* 返値 : 0/1=失敗/成功
* -----------------------------------------------------------------------
*/
char Split_IgnoreDQ(const yaya::string_t &str, yaya::string_t &dstr0, yaya::string_t &dstr1, const yaya::char_t *sepstr)
{
yaya::string_t::size_type seppoint = Find_IgnoreDQ(str,sepstr);
if ( seppoint == yaya::string_t::npos ) {
dstr0 = str;
dstr1.erase();
return 0;
}
dstr0.assign(str, 0, seppoint);
seppoint += wcslen(sepstr);
dstr1.assign(str, seppoint, str.size() - seppoint);
CutSpace(dstr0);
CutSpace(dstr1);
return 1;
}
//----
char Split_IgnoreDQ(const yaya::string_t &str, yaya::string_t &dstr0, yaya::string_t &dstr1, const yaya::string_t &sepstr)
{
return Split_IgnoreDQ(str,dstr0,dstr1,sepstr.c_str());
}
/* -----------------------------------------------------------------------
* 関数名 : SplitToMultiString
* 機能概要: 文字列を分割してvectorに格納します
*
* 返値 : 分割数(array.size())
* -----------------------------------------------------------------------
*/
size_t SplitToMultiString(const yaya::string_t &str, std::vector<yaya::string_t> *array, const yaya::string_t &delimiter)
{
if (!str.size())
return 0;
const yaya::string_t::size_type dlmlen = delimiter.size();
yaya::string_t::size_type beforepoint = 0,seppoint;
size_t count = 1;
for( ; ; ) {
// デリミタの発見
seppoint = str.find(delimiter,beforepoint);
if (seppoint == yaya::string_t::npos) {
if ( array ) {
array->emplace_back(yaya::string_t(str.begin()+beforepoint,str.end()));
}
break;
}
// 取り出しとvectorへの追加
if ( array ) {
array->emplace_back(yaya::string_t(str.begin()+beforepoint,str.begin()+seppoint));
}
// 取り出した分を削除
beforepoint = seppoint + dlmlen;
++count;
}
return count;
}
/* -----------------------------------------------------------------------
* 関数名 : CutSpace
* 機能概要: 与えられた文字列の前後に半角空白かタブがあった場合、すべて削除します
* -----------------------------------------------------------------------
*/
void CutSpace(yaya::string_t &str)
{
CutEndSpace(str);
CutStartSpace(str);
}
void CutStartSpace(yaya::string_t &str)
{
int len = str.size();
// 前方
int erasenum = 0;
for(int i = 0; i < len; i++) {
if (IsSpace(str[i])) {
erasenum++;
}
else {
break;
}
}
if (erasenum) {
str.erase(0, erasenum);
}
}
void CutEndSpace(yaya::string_t &str)
{
int len = str.size();
// 後方
int erasenum = 0;
for(int i = len - 1; i >= 0; i--) {
if (IsSpace(str[i])) {
erasenum++;
}
else {
break;
}
}
if (erasenum) {
str.erase(len - erasenum, erasenum);
}
}
/* -----------------------------------------------------------------------
* 関数名 : UnescapeSpecialString
* 機能概要: (ヒアドキュメント仕様用の)有害文字エスケープを戻します
* CParser0::LoadDictionary1 も参照してください
* -----------------------------------------------------------------------
*/
void UnescapeSpecialString(yaya::string_t &str)
{
if ( str.size() <= 1 ) {
return;
}
size_t len = str.size()-1; //1文字手前まで
for ( size_t i = 0 ; i < len ; ++i ) {
if ( str[i] == 0xFFFFU ) {
if ( str[i+1] == 0x0001U ) {
str[i] = L'\r';
str[i+1] = L'\n';
}
else if ( str[i+1] == 0x0002U ) {
str.erase(i, 1);
str[i] = L'"';
len -= 1;
}
else if ( str[i+1] == 0x0003U ) {
str.erase(i, 1);
str[i] = L'\'';
len -= 1;
}
}
}
}
/* -----------------------------------------------------------------------
* 関数名 : CutDoubleQuote
* 機能概要: 与えられた文字列の前後にダブルクォートがあった場合削除します
* -----------------------------------------------------------------------
*/
void CutDoubleQuote(yaya::string_t &str)
{
size_t len = str.size();
if (!len)
return;
// 前方
if (str[0] == L'\"') {
str.erase(0, 1);
len--;
if (!len)
return;
}
// 後方
if (str[len - 1] == L'\"')
str.erase(len - 1, 1);
}
/* -----------------------------------------------------------------------
* 関数名 : CutSingleQuote
* 機能概要: 与えられた文字列の前後にシングルクォートがあった場合削除します
* -----------------------------------------------------------------------
*/
void CutSingleQuote(yaya::string_t &str)
{
size_t len = str.size();
if (!len)
return;
// 前方
if (str[0] == L'\'') {
str.erase(0, 1);
len--;
if (!len)
return;
}
// 後方
if (str[len - 1] == L'\'')
str.erase(len - 1, 1);
}
void EscapingInsideDoubleDoubleQuote(yaya::string_t &str) {
yaya::ws_replace(str, L"\"\"", L"\"");
}
void EscapingInsideDoubleSingleQuote(yaya::string_t &str) {
yaya::ws_replace(str, L"\'\'", L"\'");
}
/* -----------------------------------------------------------------------
* 関数名 : AddDoubleQuote
* 機能概要: 与えられた文字列をダブルクォートで囲みます
* -----------------------------------------------------------------------
*/
void AddDoubleQuote(yaya::string_t &str)
{
str = L"\"" + str + L"\"";
}
/* -----------------------------------------------------------------------
* 関数名 : CutCrLf
* 機能概要: 与えられた文字列の後端に改行(CRLF)があった場合削除します
* -----------------------------------------------------------------------
*/
void CutCrLf(yaya::string_t &str)
{
yaya::ws_eraseend(str, L'\n');
yaya::ws_eraseend(str, L'\r');
}
/* -----------------------------------------------------------------------
* 関数名 : GetDateString
* 機能概要: 年月日/時分秒の文字列を作成して返します
* -----------------------------------------------------------------------
*/
yaya::string_t GetDateString()
{
char buf[128];
time_t t = time(NULL);
struct tm* tm = localtime(&t);
strftime(buf, 127, "%Y/%m/%d %H:%M:%S", tm);
#if !defined(POSIX) && !defined(__MINGW32__)
yaya::char_t wbuf[64];
for ( size_t i = 0 ; i < 64 ; ++i ) {
wbuf[i] = buf[i];
if ( wbuf[i] == 0 ) { break; }
}
return wbuf;
#else
return widen(std::string(buf));
#endif
}
/* -----------------------------------------------------------------------
* 関数名 : IsInDQ
* 機能概要: 文字列内の指定位置がダブル/シングルクォート範囲内かをチェックします
*
* 返値 : -1 = ダブル/シングルクォートの外部
* !=-1 = ダブル/シングルクォートの内部
* -2 = ダブル/シングルクォートの内部のままテキスト終了
* >=0 = 次に外部になる位置
* -----------------------------------------------------------------------
*/
const yaya::string_t::size_type IsInDQ_notindq = static_cast<yaya::string_t::size_type>(-1);
const yaya::string_t::size_type IsInDQ_runaway = static_cast<yaya::string_t::size_type>(-2);
const yaya::string_t::size_type IsInDQ_npos = static_cast<yaya::string_t::size_type>(-2);
yaya::string_t::size_type IsInDQ(const yaya::string_t &str, yaya::string_t::size_type startpoint, yaya::string_t::size_type checkpoint)
{
bool dq = false;
bool quote = false;
yaya::string_t::size_type len = str.size();
yaya::string_t::size_type found = startpoint;
while(true) {
if (found >= len) {
found = IsInDQ_runaway;
break;
}
found = str.find_first_of(L"'\"",found);
if (found == yaya::string_t::npos) {
found = IsInDQ_runaway;
break;
}
else {
if (found >= checkpoint) {
if ( (dq && str[found] == L'\"') || (quote && str[found] == L'\'') ) {
found += 1;
break;
}
if ( ! dq && ! quote ) {
break;
}
}
if (str[found] == L'\"') {
if (!quote) {
dq = !dq;
}
}
else if (str[found] == L'\'') {
if (!dq ) {
quote = !quote;
}
}
found += 1;
}
}
if ( dq || quote ) {
return found;
}
else {
return IsInDQ_notindq;
}
}
/* -----------------------------------------------------------------------
* 関数名 : IsDoubleButNotIntString
* 機能概要: 文字列がIntを除く実数数値として正当かを検査します
* 注意 : 整数値もDoubleとして正当な値なので実装時はIsIntStringとあわせること
*
* 返値 : 0/1=×/○
* -----------------------------------------------------------------------
*/
char IsDoubleButNotIntString(const yaya::string_t &str)
{
int len = str.size();
if (!len)
return 0;
int advance = (str[0] == L'-' || str[0] == L'+') ? 1 : 0;
int i = advance;
int dotcount = 0;
for( ; i < len; i++) {
// if (!::iswdigit((int)str[i])) {
if (str[i] < L'0' || str[i] > L'9') {
if (str[i] == L'.') {
dotcount++;
}
else {
return 0;
}
}
}
return dotcount == 1 && (len-advance) > 0;
}
/* -----------------------------------------------------------------------
* 関数名 : IsIntString
* 機能概要: 文字列が10進整数数値として正当かを検査します
*
* 返値 : 0/1=×/○
* -----------------------------------------------------------------------
*/
char IsIntString(const yaya::string_t &str)
{
int len = str.size();
if (!len)
return 0;
int advance = (str[0] == L'-' || str[0] == L'+') ? 1 : 0;
int i = advance;
//64bit
//9223372036854775807
if ( (len-i) > 19 ) { return 0; }
for( ; i < len; i++) {
// if (!::iswdigit((int)str[i]))
if (str[i] < L'0' || str[i] > L'9') {
return 0;
}
}
if ( (len-advance) == 19 ) {
if ( wcscmp(str.c_str(),L"9223372036854775807") > 0 ) {
return 0; //Overflow
}
}
return (len-advance) ? 1 : 0;
}
/* -----------------------------------------------------------------------
* 関数名 : IsIntBinString
* 機能概要: 文字列が2進整数数値として正当かを検査します
* 引数 : header 0/1=先頭"0x"なし/あり
*
* 返値 : 0/1=×/○
* -----------------------------------------------------------------------
*/
char IsIntBinString(const yaya::string_t &str, char header)
{
int len = str.size();
if (!len)
return 0;
int advance = (str[0] == L'-' || str[0] == L'+') ? 1 : 0;
int i = advance;
if (header) {
if (::wcsncmp(PREFIX_BIN, str.c_str() + i,PREFIX_BASE_LEN))
return 0;
i += PREFIX_BASE_LEN;
}
//64bit
if ( (len-i) > 64 ) { return 0; }
for( ; i < len; i++) {
yaya::char_t j = str[i];
if (j != L'0' && j != L'1')
return 0;
}
return (len-advance) ? 1 : 0;
}
/* -----------------------------------------------------------------------
* 関数名 : IsIntHexString
* 機能概要: 文字列が16進整数数値として正当かを検査します
*
* 返値 : 0/1=×/○
* -----------------------------------------------------------------------
*/
char IsIntHexString(const yaya::string_t &str, char header)
{
int len = str.size();
if (!len)
return 0;
int advance = (str[0] == L'-' || str[0] == L'+') ? 1 : 0;
int i = advance;
if (header) {
if (::wcsncmp(PREFIX_HEX, str.c_str() + i,PREFIX_BASE_LEN))
return 0;
i += PREFIX_BASE_LEN;
}
//64bit
//7fffffffffffffff
if ( (len-i) > 16 ) { return 0; }
for( ; i < len; i++) {
yaya::char_t j = str[i];
if (j >= L'0' && j <= L'9')
continue;
else if (j >= L'a' && j <= L'f')
continue;
else if (j >= L'A' && j <= L'F')
continue;
return 0;
}
return (len-advance) ? 1 : 0;
}
/* -----------------------------------------------------------------------
* 関数名 : IsLegalFunctionName
* 機能概要: 文字列が関数名として適正かを判定します
*
* 返値 : 0/非0=○/×
*
* 1/2/3/4/5/6=空文字列/数値のみで構成/先頭が数値もしくは"_"/使えない文字を含んでいる
* システム関数と同名/制御文もしくは演算子と同名
* -----------------------------------------------------------------------
*/
char IsLegalFunctionName(const yaya::string_t &str)
{
int len = str.size();
if (!len)
return 1;
if (IsIntString(str))
return 2;
// if (::iswdigit(str[0]) || str[0] == L'_')
// if ((str[0] >= L'0' && str[0] <= L'9') || str[0] == L'_') //チェックする必要はなさそう
// return 3;
if (str[0] == L'_') //頭がアンダースコアは蹴らないとローカル変数とカブる
return 3;
for(int i = 0; i < len; i++) {
yaya::char_t c = str[i];
if ((c >= (yaya::char_t)0x0000 && c <= (yaya::char_t)0x0026) ||
(c >= (yaya::char_t)0x0028 && c <= (yaya::char_t)0x002d) ||
c == (yaya::char_t)0x002f ||
(c >= (yaya::char_t)0x003a && c <= (yaya::char_t)0x0040) ||
c == (yaya::char_t)0x005b ||
(c >= (yaya::char_t)0x005d && c <= (yaya::char_t)0x005e) ||
c == (yaya::char_t)0x0060 ||
(c >= (yaya::char_t)0x007b && c <= (yaya::char_t)0x007f))
return 4;
}
ptrdiff_t sysidx = CSystemFunction::FindIndex(str);
if( sysidx >= 0 ) { return 5; }
for(size_t i= 0; i < FLOWCOM_NUM; i++) {
if (str == flowcom[i]) {
return 6;
}
}
for(size_t i= 0; i < FORMULATAG_NUM; i++) {
// if (str == formulatag[i])
if (str.find(formulatag[i]) != yaya::string_t::npos) {
return 6;
}
}
return 0;
}
/* -----------------------------------------------------------------------
* 関数名 : IsLegalVariableName
* 機能概要: 文字列が変数名として適正かを判定します
*
* 返値 : 0/1~6/16非0=○(グローバル変数)/×/○(ローカル変数)
*
* 1/2/3/4/5/6=空文字列/数値のみで構成/先頭が数値/使えない文字を含んでいる
* システム関数と同名/制御文もしくは演算子と同名
* -----------------------------------------------------------------------
*/
char IsLegalVariableName(const yaya::string_t &str)
{
int len = str.size();
if (!len)
return 1;
if (IsIntString(str))
return 2;
// if (::iswdigit((int)str[0]))
// if (str[0] >= L'0' && str[0] <= L'9') //チェックする必要はなさそう
// return 3;
for(int i = 0; i < len; i++) {
yaya::char_t c = str[i];
if ((c >= (yaya::char_t)0x0000 && c <= (yaya::char_t)0x0026) ||
(c >= (yaya::char_t)0x0028 && c <= (yaya::char_t)0x002d) ||
c == (yaya::char_t)0x002f ||
(c >= (yaya::char_t)0x003a && c <= (yaya::char_t)0x0040) ||
c == (yaya::char_t)0x005b ||
(c >= (yaya::char_t)0x005d && c <= (yaya::char_t)0x005e) ||
c == (yaya::char_t)0x0060 ||
(c >= (yaya::char_t)0x007b && c <= (yaya::char_t)0x007f))
return 4;
}
ptrdiff_t sysidx = CSystemFunction::FindIndex(str);
if( sysidx >= 0 ) { return 5; }
for(size_t i= 0; i < FLOWCOM_NUM; i++) {
if (str == flowcom[i]) {
return 6;
}
}
for(size_t i= 0; i < FORMULATAG_NUM; i++) {
// if (str == formulatag[i])
if (str.find(formulatag[i]) != yaya::string_t::npos) {
return 6;
}
}
return (str[0] == L'_') ? 16 : 0;
}
/* -----------------------------------------------------------------------
* 関数名 : IsLegalStrLiteral
* 機能概要: ダブルクォートで囲まれているべき文字列の正当性を検査します
*
* 返値 : 0/1/2/3=正常/ダブルクォートが閉じていない/
* ダブルクォートで囲まれているがその中にダブルクォートが包含されている/
* ダブルクォートで囲まれていない
* -----------------------------------------------------------------------
*/
char IsLegalStrLiteral(const yaya::string_t &str)
{
int len = str.size();
if (!len)
return 3;
// 先頭のダブルクォートチェック
int flg = (str[0] == L'\"') ? 1 : 0;
// 後端のダブルクォートチェック
if (len > 1)
if (str[len - 1] == L'\"')
flg += 2;
// 内包されているダブルクォートの探索
if(len > 2) {
int lenm1 = len - 1;
int i = 1;
while(i < lenm1) {
if(str[i] == L'\"') {
if(str[i + 1] != L'\"') {
flg = 4;
break;
}
else
i++;
}
i++;
}
}
// 結果を返します
switch(flg) {
case 3:
return 0;
case 1:
case 2:
case 5:
case 6:
return 1;
case 7:
return 2;
default:
return 3;
};
}
/* -----------------------------------------------------------------------
* 関数名 : IsLegalPlainStrLiteral
* 機能概要: シングルクォートで囲まれているべき文字列の正当性を検査します
*
* 返値 : 0/1/2/3=正常/ダブルクォートが閉じていない/
* ダブルクォートで囲まれているがその中にダブルクォートが包含されている/
* ダブルクォートで囲まれていない
* -----------------------------------------------------------------------
*/
char IsLegalPlainStrLiteral(const yaya::string_t &str)
{
int len = str.size();
if (!len)
return 3;
// 先頭のシングルクォートチェック
int flg = (str[0] == L'\'') ? 1 : 0;
// 後端のシングルクォートチェック
if (len > 1)
if (str[len - 1] == L'\'')
flg += 2;
// 内包されているシングルクォートの探索
if (len > 2) {
int lenm1 = len - 1;
int i = 1;
while(i < lenm1) {
if(str[i] == L'\'') {
if(str[i + 1] != L'\'') {
flg = 4;
break;
}
else
i++;
}
i++;
}
}
// 結果を返します
switch(flg) {
case 3:
return 0;
case 1:
case 2:
case 5:
case 6:
return 1;
case 7:
return 2;
default:
return 3;
};
}
/* -----------------------------------------------------------------------
* 関数名 : IsUnicodeAware
* 機能概要: Unicode系APIが使えるかどうかを返します
* POSIXは常にtrue / Win9x/Meのみfalse
* -----------------------------------------------------------------------
*/
#if defined(WIN32) || defined(_WIN32_WCE)
class IsUnicodeAwareHelper
{
public:
bool isnt;
IsUnicodeAwareHelper() {
OSVERSIONINFO osVer;
osVer.dwOSVersionInfoSize = sizeof(osVer);
::GetVersionEx(&osVer);
isnt = (osVer.dwPlatformId == VER_PLATFORM_WIN32_NT);
}
};
bool IsUnicodeAware(void)
{
static IsUnicodeAwareHelper h;
return h.isnt;
}
#else
bool IsUnicodeAware(void)
{
return true;
}
#endif
/* -----------------------------------------------------------------------
* 関数名 : GetEpochTime
* 機能概要: 64bit対応の time() 相当
* -----------------------------------------------------------------------
*/
#if defined(WIN32) || defined(_WIN32_WCE)
static yaya::time_t FileTimeToEpochTime(FILETIME &ft)
{
ULARGE_INTEGER ul;
ul.LowPart = ft.dwLowDateTime;
ul.HighPart = ft.dwHighDateTime;
yaya::time_t tv = ul.QuadPart;
tv -= LL_DEF(116444736000000000);
tv /= LL_DEF(10000000);
return tv;
}
static FILETIME EpochTimeToFileTime(yaya::time_t &tv)
{
union {
ULARGE_INTEGER ul;
FILETIME ft;
} tc;
tc.ul.QuadPart = tv;
tc.ul.QuadPart *= LL_DEF(10000000);
tc.ul.QuadPart += LL_DEF(116444736000000000);
return tc.ft;
}
static unsigned int month_to_day_table[] = {
0,
31,
31+28,
31+28+31,
31+28+31+30,
31+28+31+30+31,
31+28+31+30+31+30,
31+28+31+30+31+30+31,
31+28+31+30+31+30+31+31,
31+28+31+30+31+30+31+31+30,
31+28+31+30+31+30+31+31+30+31,
31+28+31+30+31+30+31+31+30+31+30,
};
static bool IsLeapYear(unsigned int year)
{
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
return true;
}
else {
return false;
}
}
else {
return true;
}
}
else {
return false;
}
}
#endif
yaya::time_t GetEpochTime()
{
#if defined(WIN32) || defined(_WIN32_WCE)
FILETIME ft;
::GetSystemTimeAsFileTime(&ft);
return FileTimeToEpochTime(ft);
#else
time_t tv;
time(&tv);
return (yaya::time_t)tv;
#endif
}
struct tm EpochTimeToLocalTime(yaya::time_t tv)
{
#if defined(WIN32) || defined(_WIN32_WCE)
FILETIME ft = EpochTimeToFileTime(tv);
FILETIME lft;
::FileTimeToLocalFileTime(&ft,&lft);
SYSTEMTIME stime;
::FileTimeToSystemTime(&lft,&stime);
struct tm tmtime;
tmtime.tm_sec = stime.wSecond;
tmtime.tm_min = stime.wMinute;
tmtime.tm_hour = stime.wHour;
tmtime.tm_mday = stime.wDay;
tmtime.tm_mon = stime.wMonth - 1; //struct tm 0-11 = SYSTEMTIME 1-12
tmtime.tm_year = stime.wYear - 1900; //struct tm 100 = SYSTEMTIME 2000
tmtime.tm_wday = stime.wDayOfWeek;
tmtime.tm_yday = month_to_day_table[stime.wMonth-1] + stime.wDay;
if ( IsLeapYear(stime.wYear) && stime.wMonth >= 3 ) { tmtime.tm_yday += 1; }
TIME_ZONE_INFORMATION tzinfo;
tmtime.tm_isdst = ::GetTimeZoneInformation(&tzinfo) == TIME_ZONE_ID_DAYLIGHT;
return tmtime;
#else
time_t tc = tv;
return *localtime(&tc);
#endif
}
yaya::time_t LocalTimeToEpochTime(struct tm &tm)
{
#if defined(WIN32) || defined(_WIN32_WCE)
__int64 t1 = tm.tm_year;
if ( tm.tm_mon < 0 || tm.tm_mon > 11 ) { //1-12より外