-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncs.cpp
203 lines (201 loc) · 4.22 KB
/
funcs.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
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <sys/timeb.h>
using namespace std;
struct strings {
char** strs;
int max;
};
bool is_integer(const float a) {
int temp=static_cast<int>(a);
if(temp==a) {
return(true);
}
return(false);
}
void print_str(const char* str) {
if(str!=NULL) {
int i=0;
while(str[i]!='\0') {
cerr << str[i];
i++;
}
cerr << "... END AT " << i << " (" << strlen(str) <<")" << endl;
}
}
bool str(const char* a,const char* b) {
if(a==NULL||b==NULL) {
return(false);
}
else {
return(strcmp(a,b)==0);
}
}
unsigned long long int mtime() {
static timeb* tp=new timeb;ftime(tp);
return(tp->time*1000+tp->millitm-1906000000);
}
char* strcp(const char* src) {
if(src==NULL){return(NULL);}
char* destination=new char[strlen(src)+1];
unsigned int i=0;
while(src[i]!='\0') {
destination[i]=src[i];
i++;
}
destination[strlen(src)]='\0';
return(destination);
}
char* stradd(char* l, const char* r){
if(l==NULL) {return(strcp(r));}
const unsigned int offset=strlen(l);
const unsigned int maxindex=offset+strlen(r);
char* d=new char[maxindex+2];
unsigned int i=0;
bool l_end=false;
bool r_end=false;
while(!r_end) {
if(l[i]=='\0') {
l_end=true;
}
if(l_end) {
if(r[i-offset]!='\0') {
d[i]=r[i-offset];
}
else {
r_end=true;
}
}
else {
d[i]=l[i];
}
i++;
}
d[maxindex]='\0';
return(d);
}
char* stradd(char* l,char r) {
char* a=new char[2];
a[0]=r;
a[1]=0;
return(stradd(l,a));
}
strings* explode(const char* src,char sym=' ') {
unsigned int i=0;
strings* res=new strings;
res->max=-1;
unsigned int smax=strlen(src)-1;
//cerr << "[" << smax << "]";
if(smax==0||src==NULL||strlen(src)<=1) {
return(res);
}
res->max=0;
res->strs=new char*[smax];
char* prevs=NULL,*temp=NULL;
while(i<=(smax+1)) {
if(src[i]==sym||i>smax) {
if(prevs!=NULL) {
res->strs[(res->max)++]=prevs;
prevs=NULL;
}
}
else {
temp=new char[2];
temp[0]=src[i];
temp[1]='\0';
prevs=stradd(prevs,temp);
}
i++;
}
res->max--;
return(res);
}
bool strchar(const char* h,const char* n) {
if(strlen(n)>1) {
return(false);
}
unsigned int i=0;
while((h[i]!='\0')&&(h[i]!=n[0])) {
i++;
}
return(i!=strlen(h));
}
bool strchar(const char* h, char n) {
char* a=new char[2];
a[0]=n;
a[1]=0;
return(strchar(h,a));
}
char* char_replace(char* src,char a,char b) {
char* result=new char[strlen(src)];
int i=0;
while(src[i]!='\0') {
if(src[i]==a) {
result[i]=b;
}
else {
result[i]=src[i];
}
i++;
}
result[strlen(src)]='\0';
return(result);
}
char* decimal_point(char* src) {
return(char_replace(src,'.',','));
}
char* print_num(float a) {
char* h=new char[15];
if(is_integer(a)) {
sprintf(h,"%d",static_cast<int>(a));
}
else {
sprintf(h,"%f",a);
}
return(decimal_point(h));
}
void var_dump(const char* a) {
cerr << "String (" << strlen(a) << "): [" << a << "]" << endl;
}
char* repeat(const char* str,int count=1) {
int i=1;
char* res=NULL;
while(i<=count) {
res=stradd(res,str);
i++;
}
return(res);
}
void remove_nl(char* a) {
if(a[strlen(a)-1]=='\n') {
a[strlen(a)-1]=0;
}
}
void remove_nl(strings* a) {
int i=0;
while(i<=a->max) {
remove_nl(a->strs[i]);
i++;
}
}
char* strings_merge(strings* a,const char *del=" ") {
int i=0;
char* res=strcp("");
while(i<=a->max) {
res=stradd(res,a->strs[i]);
if(i<a->max) {
res=stradd(res,del);
}
i++;
}
return(res);
}
const char* print(bool a) {
if(a) return("1");
return("0");
}
bool atob(const char* a) {
if(a&&str(a,"1")) return(true);
return(false);
}