-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLily_basic.cpp
More file actions
793 lines (746 loc) · 12.9 KB
/
Lily_basic.cpp
File metadata and controls
793 lines (746 loc) · 12.9 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
#ifdef in_PC
#define _CRT_SECURE_NO_WARNINGS
#include "Lily_help.h"
#include <string.h>
#include <iostream>
using namespace std;
int new_count = 0;
#else
#include <string.h>
#include <stdlib.h>
#include "Lily_help.h"
#endif // in_PC
char str_startwith(char *s, char *cmd)
{
int i, n;
n = strlen(cmd);
if (strlen(s) < n)
return 0;
for (i = 0; i < n; i++)
if (s[i] != cmd[i])
return 0;
return 1;
}
int str_contains(char *s, char c)
{
for (int i = 0; s[i] != '\0'; i++)
if (s[i] == c)
return i + 1;
return 0;
}
int str_contains_by_str(char *s, char *c)
{
for (int i = 0; s[i] != '\0'; i++)
if (str_contains(c, s[i]))
return 1;
//if (s[i] == c)return 1;
return 0;
}
char str_equal(const char *s, const char *cmd)
{
if (s[0] == '\0')
return 0;
int i = 0;
char a, b;
do
{
a = s[i];
b = cmd[i];
i++;
if (a != b)
return 0;
} while (s[i] != '\0' && cmd[i] != '\0' && cmd[i] != ':'); // the next char is not \0
a = s[i];
b = cmd[i];
if (a == '\0') //b!=0, ahi = ahi[12]
{
if (isA(b) || isD(b) || b == '_')
return 0;
return i;
}
//b=='\0'
if (isA(a) || isD(a) || a == '_')
return 0;
// if ('0' <= a && a <= '9')
// return 0;
return i;
}
// return -1 if not found
int str_index(char *s, char c)
{
int i;
for (i = 0; s[i] != '\0'; i++)
if (s[i] == c)
return i;
return -1;
}
int str_index_right(char *s, char c)
{
int i;
int n = strlen(s);
for (i = n - 1; i; i--)
if (s[i] == c)
return i;
if (s[i] == c)
return i;
return -1;
}
char str_lower(char *str)
{
if (str == NULL)
return 0;
char *p = str;
while (*p != '\0')
{
if (*p >= 'A' && *p <= 'Z')
*p = (*p) + 0x20;
p++;
}
return p - str;
}
char str_replace(char *s, char from, char to)
{
char times = 0;
int i;
for (i = 0; s[i] != '\0'; i++)
{
if (s[i] == from)
{
s[i] = to;
times++;
}
}
return times;
}
char str_replace_by_str(char *s, const char *from, char to)
{
char times = 0;
int i;
for (i = 0; s[i] != '\0'; i++)
{
const char *p = from;
while (*p != '\0')
{
if (*p == s[i])
{
s[i] = to;
times++;
break;
}
p++;
}
}
return times;
}
// '-123.45', '+13.890'
int str_is_numeric(char *name)
{
if (!isD(*name) && *name != '+' && *name != '-')
return 0;
if (!isD(*name))
{
name++;
if (*name == '\0')
return 0;
}
else
name++;
char lastIsDecimal = 0;
for (; *name != '\0'; name++)
{
if (isD(*name))
{
lastIsDecimal = 0;
continue;
}
else if (*name == '.')
{
if (lastIsDecimal)
return 0;
lastIsDecimal = 1;
continue;
}
return 0;
}
return 1;
}
// a, _ah_haha_, _label_text3
int str_is_name(char *name)
{
if (!isA(*name) && *name != '_')
return 0;
if (*name == '_')
{
name++;
if (*name == '\0')
return 0;
}
else
name++;
while (*name != '\0')
{
if (!isA(*name) && !isD(*name) && *name != '_')
return 0;
name++;
}
return 1;
}
//in place operation,
// note: delete list returned in time
Li_List str_split(char *str, char split_char)
{
Li_List list = new_list(sizeof(char *), 4);
if (*str == '\0')
return list;
char *last; // , * next;
char *now = str;
if (*now != split_char)
{
list_add(list, &str);
}
last = now;
now++;
// next = now;
while (*now != '\0')
{
if (*last == split_char && *now != split_char)
{
li_add(list, now);
/**last = '\0';*/
}
if (*last != split_char && *now == split_char)
{
*now = '\0';
last = &split_char;
now++; // = next;
//next++;
continue;
}
last = now;
now++; //= next;
//next++;
}
return list;
}
//in place operation,
// note: delete list returned in time
Li_List str_split_by_str(char *str, char *split_char)
{
Li_List list = new_list(sizeof(char *), 4);
if (*str == '\0')
return list;
char *last; //, * next;
char *now = str;
if (!str_contains(split_char, *now))
//if (*now != split_char)
{
list_add(list, &str);
}
last = now;
now++;
// next = now;
while (*now != '\0')
{
if (str_contains(split_char, *last) && !str_contains(split_char, *now))
//if (*last == split_char && *now != split_char)
{
li_add(list, now);
/**last = '\0';*/
}
if (!str_contains(split_char, *last) && str_contains(split_char, *now))
//if (*last != split_char && *now == split_char)
{
*now = '\0';
last = split_char;
now++; // = next;
//next++;
continue;
}
last = now;
now++; //= next;
//next++;
}
return list;
}
// note: delete list returned in time
// find indexs of chars in find in string s
//e.g. str_find("1+2*3/4", "+-*/")
// return [1,3,5]
Li_List str_find(char *s, char *find)
{
Li_List list = new_list(sizeof(int), 4);
if (list == NULL)
return list;
for (int i = 0; s[i] != '\0'; i++)
{
if (str_contains(find, s[i]))
li_add(list, i);
}
return list;
}
Li_List str_find_sort(char *s, char *find)
{
Li_List list = new_list(sizeof(int), 4);
if (list == NULL)
return list;
for (int i = 0; find[i] != '\0'; i++)
{
for (int j = 0; s[j] != '\0'; j++)
if (s[j] == find[i])
li_add(list, j);
}
return list;
}
// add the num to a string
// return: length of num
char int_to_string(int n, char *s)
{
if (n == 0)
{
s[0] = '0';
return 1;
}
char add = 0;
if (n < 0)
{
n *= -1;
s[0] = '-';
s++;
add = 1;
}
int i;
for (i = 0; n > 0; i++, n /= 10)
{
s[i] = n % 10 + '0';
}
int j;
char a;
for (j = 0; j < i / 2; j++)
{
a = s[j];
s[j] = s[i - j - 1];
s[i - j - 1] = a;
}
s[i + add] = '\0';
return i + add;
}
float str_to_float(char *s)
{
int integer = 0;
float decimal = 0.0f;
float kd = 0.1;
int in_decimal = 0;
char is_neg = 0;
if (*s == '-')
{
is_neg = 1;
s++;
}
else if (*s == '+')
{
s++;
}
for (; *s != '\0'; s++)
{
if (*s == '.')
{
in_decimal = 1;
continue;
}
if (!isD(*s))
return 0.0f;
if (in_decimal)
{
float d = *s - '0';
decimal += d * kd;
kd /= 10.0f;
}
else
{
int d = *s - '0';
integer *= 10;
integer += d;
}
}
if (is_neg)
{
return -(integer + decimal);
}
else
return (integer + decimal);
}
float *get_nums_from_rx(char *rs, int *length_back)
{
static float nums[10];
int i, k = 0;
float j = 10.0f;
bool negative = false;
j = 10.0f;
bool has_decimal = false;
float integer = 0.0f, decimal = 0.0f;
char c;
bool last_is_numeric = false;
for (i = 0; rs[i] != '\0'; i++)
{
c = rs[i];
if ('0' <= c && c <= '9') //number
{
if (!last_is_numeric) //at the begin
{
integer = (c - '0');
decimal = 0;
has_decimal = false;
j = 10;
}
else if (has_decimal) //go on
{
decimal += (float)(c - '0') / j;
j *= 10.0f;
}
else
{
integer *= 10.0f;
integer += (c - '0');
}
last_is_numeric = true;
continue;
}
// not numeric
else if (c == '.' && last_is_numeric) //xxx.
{
has_decimal = True;
last_is_numeric = true;
continue;
}
else if (c == '-') // -
{
negative = !negative; //clear in end
}
else if (last_is_numeric) //end
{
nums[k] = decimal + integer;
if (negative)
{
nums[k] *= -1;
negative = false;
}
k++;
}
last_is_numeric = False;
}
if (last_is_numeric) //end
{
nums[k] = decimal + integer;
if (negative)
nums[k] *= -1;
k++;
}
if (k == 0)
{
*length_back = 0;
return nums;
}
*length_back = k;
return nums;
}
// assert a word starts with a letter a-z
char **get_paras_from_rx(char *rx, int *len)
{
static char s[5][8]; //5 rows, 7 chars a string maximum
static char *p[5];
int i, j;
bool is_last_a_letter;
for (i = 0; i < 5; i++)
p[i] = s[i];
i = 0;
//while (*rx != '\0')//skip the cmd
//{
// if (*rx < 'a' || *rx>'z')//not a letter
// break;
// rx++;
//}
while (*rx != '\0') //skip the cmd
{
if ('a' <= *rx && *rx <= 'z') // a letter
break;
rx++;
}
if (*rx == '\0')
{
*len = 0;
return p;
}
i = 0;
j = 0;
is_last_a_letter = false;
while (*rx != '\0')
{
if ('a' <= *rx && *rx <= 'z' || *rx == '_') // a letter
{
if (!is_last_a_letter) //begin
{
j = 0;
}
//go on
s[i][j++] = *rx;
is_last_a_letter = true;
rx++;
continue;
}
else if (is_last_a_letter) //end
{
s[i][j] = '\0';
i++;
}
rx++;
is_last_a_letter = false;
}
s[i][j] = '\0';
if (is_last_a_letter) //end
{
i++;
}
*len = i;
return p;
}
void limit_to(float *x, float max)
{
if (*x > max)
*x = max;
else if (*x < -max)
*x = -max;
}
void limit_from_to(float *x, float lower, float upper)
{
if (*x < lower)
*x = lower;
else if (*x > upper)
*x = upper;
}
//int li_count = 0;
int init_list(Lily_List *list, unsigned int unit_size2, unsigned int init_cap)
{
list->type_size = unit_size2;
list->cap = 0;
list->content = (char *)calloc(init_cap, unit_size2);
#ifdef in_PC
new_count++;
cout << "#" << new_count << "#";
#endif // in_PC
if (list->content == NULL)
return 1;
list->cap = init_cap;
list->count = 0;
return 0;
}
Lily_List *new_list(unsigned int type, unsigned int init_cap)
{
//li_count++;
//static char tx[30];
//sprintf(tx,"+>%d\n",li_count);
//lily_out(tx);
Lily_List *list = (Lily_List *)malloc(sizeof(Lily_List));
#ifdef in_PC
new_count++;
cout << "#" << new_count << "#";
#endif // in_PC
if (list == NULL)
return list;
list->count = 0;
list->type_size = type;
list->content = (char *)calloc(init_cap, type);
#ifdef in_PC
new_count++;
cout << "#" << new_count << "#";
#endif // in_PC
if (list->content == NULL)
list->cap = 0;
else
list->cap = init_cap;
return list;
}
int list_recap(Lily_List *list, unsigned int new_cap)
{
void *p;
p = realloc(list->content, new_cap * list->type_size);
if (p == NULL)
{
return -1;
}
list->content = (char *)p;
list->cap = new_cap;
return 0;
}
int list_add(Lily_List *list, void *c)
{
if (list->count >= list->cap)
if (list_recap(list, list->cap + 5))
return -1;
memcpy((char *)(list->content) + list->count * list->type_size, c, list->type_size);
list->count++;
return list->count - 1;
}
int list_remove_at(Lily_List *list, unsigned int index)
{
if (index >= list->count)
return -1;
char *i, *n, *p;
list->count--;
n = list->content + list->count * list->type_size;
i = list->content + index * list->type_size;
p = i + list->type_size;
for (; i < n; i++, p++)
*i = *p;
//while (i < n) *(i++) = *(p++);
return 0;
}
bool list_item_equal(void *item_a, void *item_b, unsigned int type)
{
int i;
char *item1 = (char *)item_a;
char *item2 = (char *)item_b;
for (i = 0; i < type; i++)
{
if (item1[i] != item2[i])
return false;
}
return true;
}
int list_remove(Lily_List *list, void *item)
{
int i;
char *p = list->content;
for (i = 0; i < list->count; i++, p += list->type_size)
if (list_item_equal(item, p, list->type_size))
break;
if (i == list->count)
return -1;
list_remove_at(list, i);
return 0;
}
int list_find(Lily_List *list, void *item)
{
int i;
char *p = list->content;
for (i = 0; i < list->count; i++, p += list->type_size)
if (list_item_equal(item, p, list->type_size))
return i;
return -1;
}
int delete_list(Lily_List *list)
{
if (list == NULL)
return -1;
if (list->content != NULL)
free(list->content);
free(list);
#ifdef in_PC
new_count -= 2;
cout << "#" << new_count << "#";
#endif // in_PC
return 0;
}
//depracted
// use new_string_by to creat sample string field
Li_String new_li_string_by(char *str)
{
Li_String li_string = (Li_String)malloc(sizeof(Li_String_def));
if (li_string == NULL)
return NULL;
int n = strlen(str);
li_string->str = (char *)malloc(n + 1);
#ifdef in_PC
new_count += 2;
cout << "#" << new_count << "#";
#endif // in_PC
if (li_string->str == NULL)
{
free(li_string);
#ifdef in_PC
new_count--;
cout << "#" << new_count << "#";
#endif // in_PC
return NULL;
}
strcpy(li_string->str, str);
li_string->length = n;
return li_string;
}
char *new_string_by(char *str)
{
int n = strlen(str) + 1;
void *p = malloc(n);
#ifdef in_PC //start malloc(.*);\r\n#if
new_count++;
cout << "#" << new_count << "#";
#endif // in_PC
#ifdef in_debug
if (p == NULL)
lily_out("new_string_by failed");
#endif // in_debug
if (p == NULL)
return NULL;
strcpy((char *)p, str);
return (char *)p;
}
void delete_li_string(Li_String li)
{
free(li->str);
free(li);
#ifdef in_PC
new_count -= 2;
cout << "#" << new_count << "#";
#endif // in_PC
}
int assign_li_string(Li_String li, char *source)
{
int n = strlen(source);
if (n > li->length)
{
void *p = realloc(li->str, n + 1);
if (p == NULL)
{
return -1;
}
li->str = (char *)p;
}
strcpy(li->str, source);
li->length = n;
return n;
}
// remove special characters in s
int str_wrap(char *s, char c)
{
int i = 0, j = 0;
while (1)
{
if (s[i] != c && s[i] != '\0')
s[j++] = s[i++]; //若相同则赋值,且j累加
else if (s[i++] == '\0')
{
break; //到\0则跳出循环
}
}
s[j] = '\0';
return j;
}
int str_wrap_by_str(char *s, char *cs)
{
int i, j;
while (*cs != '\0')
{
i = 0;
j = 0;
while (1)
{
if (s[i] != *cs && s[i] != '\0')
s[j++] = s[i++]; //若相同则赋值,且j累加
else if (s[i++] == '\0')
{
break; //到\0则跳出循环
}
}
}
s[j] = '\0';
return j;
}