-
Notifications
You must be signed in to change notification settings - Fork 1
/
x.c
158 lines (147 loc) · 3.36 KB
/
x.c
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
#include "x.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <math.h>
#include <ctype.h>
void* xmalloc(size_t s) {
void *p=0;
if(!(p=malloc(s))) {
printf("error: xmalloc(): memory allocation failed\n");
exit(1);
}
return p;
}
void xfree(void *p) {
if(p) free(p);
}
void* xcalloc(size_t n, size_t s) {
void *p=0;
if(!(p=calloc(n, s))) {
printf("error: xcalloc(): memory allocation failed\n");
exit(1);
}
return p;
}
void* xrealloc(void *p, size_t s) {
void *p2=0;
if(!(p2=realloc(p, s))) {
printf("error: xrealloc(): memory allocation failed\n");
exit(1);
}
return p2;
}
void* xstrdup(const char *s) {
void *p=0;
if(!(p=strdup(s))) {
printf("error: xstrdup(): memory allocation failed\n");
exit(1);
}
return p;
}
void* xstrndup(const char *s, size_t n) {
void *p=xcalloc(n+1,1);
memcpy(p,s,n);
return p;
}
int xatoi(char *s) {
int r;
long a;
char *e;
if(!s||!strlen(s)) return r=INT_MIN;
else if(!strcmp(s,"0I")) r=INT_MAX;
else if(!strcmp(s,"0N")) r=INT_MIN;
else if(!strcmp(s,"-0N")) r=INT_MIN;
else if(!strcmp(s,"-0I")) r=INT_MIN+1;
else {
a=strtol(s,&e,10);
if((size_t)(e-s)!=strlen(s)) r=INT_MIN;
else if(a>INT_MAX) r=INT_MAX;
else if(a<INT_MIN) r=INT_MIN;
else r=a;
}
return r;
}
float xstrtof(char *s) {
float r;
char *e;
if(!s||!strlen(s)) r=NAN;
else if(!strcmp(s,"0i")||!strcmp(s,"0I")) r=INFINITY;
else if(!strcmp(s,"0n")||!strcmp(s,"0N")) r=NAN;
else if(!strcmp(s,"-0n")||!strcmp(s,"-0N")) r=NAN;
else if(!strcmp(s,"-0i")||!strcmp(s,"-0I")) r=-INFINITY;
else {
r=strtof(s,&e);
if((size_t)(e-s)!=strlen(s)) r=NAN;
}
return r;
}
char* xesc(char *p) {
char *ss;
int n,i,j=0;
if(!p) return 0;
n=strlen(p);
ss=xmalloc(1+strlen(p)*4);
for(i=0;i<n;i++) {
if(p[i]<32||p[i]>126) {
if(p[i]=='\b') { ss[j++]='\\'; ss[j++]='b'; }
else if(p[i]=='\t') { ss[j++]='\\'; ss[j++]='t'; }
else if(p[i]=='\n') { ss[j++]='\\'; ss[j++]='n'; }
else if(p[i]=='\r') { ss[j++]='\\'; ss[j++]='r'; }
else j+=sprintf(&ss[j],"\\%03o",(unsigned char)p[i]);
} else {
if(p[i]=='"') { ss[j++]='\\'; ss[j++]='"'; }
else if(p[i]=='\\') { ss[j++]='\\'; ss[j++]='\\'; }
else ss[j++]=p[i];
}
}
ss[j]=0;
return ss;
}
char* xunesc(char *p) {
char *ss;
int n,i,j=0,s=0;
unsigned char o;
if(!p) return 0;
n=strlen(p);
ss=xmalloc(1+n);
for(i=0;i<n;i++) {
switch(s) {
case 0:
if(strchr("\b\t\n\r\"",*p)) ss[j++]=*p;
else if(*p=='\\') s=1;
else ss[j++]=*p;
break;
case 1: /* escape */
if(isdigit(*p)&&*p<='7') { o=*p-48; s=2; } /* octal */
else { ss[j++]=*p; s=0; }
break;
case 2: /* octal */
if(isdigit(*p)&&*p<='7') { o*=8; o+=*p-48; s=3; }
else if(*p=='\\') { ss[j++]=o; s=1; }
else { ss[j++]=o; ss[j++]=*p; s=0; }
break;
case 3: /* octal */
if(isdigit(*p)&&*p<='7') { o*=8; o+=*p-48; ss[j++]=o; s=0; }
else if(*p=='\\') { ss[j++]=o; s=1; }
else { ss[j++]=o; ss[j++]=*p; s=0; }
break;
default: return 0; /* error */
}
++p;
}
if(s==2||s==3) ss[j++]=o;
ss[j]=0;
return ss;
}
char* xeqs(char *p) {
if(!*p++) return 0;
while(*p) {
if(*p=='\\') { ++p; if(*p) ++p; }
else if(*p=='"') { ++p; break; }
else ++p;
}
return p;
}