-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfqdaf.c
211 lines (188 loc) · 3.5 KB
/
fqdaf.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
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
/* Fredlin Quick and Dirty Additional Functions */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "fqdaf.h"
long getmin(long l1, long l2)
{
if (l1<l2) return l1; else return l2;
}
long getmax(long l1, long l2)
{
if (l1>l2) return l1; else return l2;
}
int strltrim(char *outstring, char *instring)
{
int charptr=0;
int ocharptr=0;
while (instring[charptr]==32) charptr++;
while (instring[charptr]!=0)
{
outstring[ocharptr]=instring[charptr];
charptr++;
ocharptr++;
}
outstring[ocharptr]=0;
return 0;
}
int isfredlinlndigit(int dig)
{
if (isdigit(dig)) return 1;
if (dig==35) return 1;
if (dig == 46) return 1;
return 0;
}
unsigned long sstrlen(char *astr)
{
if (!astr) return 0;
return strlen(astr);
}
int streq_(char *str1, char *str2)
{
unsigned long l1=sstrlen(str1), l2=sstrlen(str2);
if (l1 != l2) return 0;
if (memcmp(str1, str2, l1)==0) return 1;
return 0;
}
int nstreq(char *str1, char *str2)
{
/*char str2a[256]="";
strcpy(str2a,str2);
strcat(str2a,"\n");
if ((strcmp(str1,str2)==0) || (strcmp(str1,str2a)==0))
{
return 1;
}
return 0;*/ /* This method was limited to 256 characters! */
if (streq_(str1,str2)) return 1;
unsigned long l1 = sstrlen(str1), l2 = sstrlen(str2);
if ((l1 == (l2 + 1) || l1 == (l2 + 2)) &&
(str1[l1-1] == '\n' || str1[l1-1] == '\r') &&
memcmp(str1, str2, l2) == 0)
return 1;
return 0;
}
int instr(char *targstring, char *findstring, int startoffset)
{
int i=0;
int j=0;
int ans=-1;
i+=startoffset;
while (j>-1)
{
j=instrch(targstring, findstring[0], i);
if (j>-1)
{
if (strncmp(targstring+j+i, findstring,strlen(findstring))==0)
{
ans = j+i;
return ans;
}
i+=j+1;
}
}
return ans;
}
int replacestronce(char *outstring, char *targstring, char *findstring, char *repstring, int maxlen, int startoffset)
{
int i=0;
int j=0;
int ans=0;
int a=0;
if (strlen(targstring)-strlen(findstring)+strlen(repstring) > maxlen) return -2;
i = instr(targstring,findstring,startoffset);
if (i>-1)
{
for (j=0;j<i;j++)
{
outstring[j]=targstring[j];
}
for (j=0;j<strlen(repstring);j++)
{
outstring[j+i]=repstring[j];
}
a=i;
a+=strlen(findstring);
i+=strlen(repstring);
ans=i;
for (j=a;j<=strlen(targstring);j++)
{
outstring[j-a+i]=targstring[j];
}
return ans;
}
else
{
return -1;
}
}
int firstletterpos(char *cmdstring)
{
int i=0;
while (cmdstring[i] != 0)
{
if (isalpha(cmdstring[i])) return i;
i++;
}
return -1;
}
long getfredlinlnnum(char *astr, int startoffset)
{
/*
Returns:
0+ = Number
-1 = Current line
-2 = Line after last
-3 = No number
-10 = Invalid number
*/
int i=0;
char lnnum[256]="\x00";
int j=0;
i+=startoffset;
while (astr[i] != 0)
{
if (isfredlinlndigit(astr[i]))
{
lnnum[j]=astr[i];
j++;
}
else
{
break;
}
i++;
}
if (lnnum[0]==46)
{
return FLN_CURRENT;
}
if (lnnum[0]==35)
{
return FLN_AFTERLAST;
}
if (lnnum[0]==0)
{
return FLN_NONUMBER;
}
if (isdigit(lnnum[0]))
{
return atol(lnnum);
}
return FLN_INVALIDNUMBER;
}
void printdebug(char *astr)
{
int i;
printf(">>");
for (i=0;astr[i]!=0;i++)
{
if (astr[i]<32 || astr[i]>126) printf("%03d ",astr[i]);
else printf("'%c' ",astr[i]);
}
printf("\n");
return;
}