-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPKINSCRT.c
195 lines (173 loc) · 6.37 KB
/
PKINSCRT.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
#ifdef INDEPENDENT
#include <MacHeaders>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int Ltest=0; /*PKIN common*/
char alertstr[256]; /*PKIN common*/
void DoReportDialog(void); /*PKIN common*/
typedef struct{
char* begin;
char* end;
char* next;
char* cursor;
} textblock;
/*prototypes*/
void inittextblock(textblock*);
void rewindtextblock(textblock*);
void buildtextblock(textblock*, size_t);
void disposetextblock(textblock*);
void putonetextblockline(textblock*, char*);
void getonetextblockline(textblock*, char*); /*corrected 070210*/
#endif /*INDEPENDENT*/
#include "PKIN.h"
/***countpoints()*************************************************************/
int countpoints(char cntl[5], int Lout) /*0 accummulates, 1 reports*/
{/*counter of incidents of scratch points with particular 4-char-flags*/
/*returns number accummulated in specific counter, or 0 when count fails*/
/*initialize ntypes = 0, this routine will initialize cntp,cntn as it goes*/
int j=0,Lmatch=0,ireturn=0;
for(j=0; j<ntypes; j++)
{/*check 4 characters of stored string against scratch-flag string*/
if( cntp[j][0]==cntl[0]
&& cntp[j][1]==cntl[1]
&& cntp[j][2]==cntl[2]
&& cntp[j][3]==cntl[3] )
{/*match exists, augment counter*/
cntn[j]++;
ireturn = cntn[j];
Lmatch = 1;
break;
}
}
if(!Lmatch && !Lout && ntypes < MAXTYPES)
{/*this set of 4 characters not previously part of a count to be stored*/
j = ntypes; /*next actual index above present set*/
cntp[j][0] = cntl[0];
cntp[j][1] = cntl[1];
cntp[j][2] = cntl[2];
cntp[j][3] = cntl[3];
cntp[j][4] = '\0'; /*real string, could be printed*/
cntn[j] = 1; /*count this first point*/
ireturn = cntn[j]; /*i.e. 1 */
ntypes = j + 1; /*new working total for 0--<ntypes scan*/
}/*this set of 4 characters not previously part of a count to be stored*/
return ireturn;
}
/*___countpoints()___________________________________________________________*/
/****inittextblock()**********************************************************/
void inittextblock(textblock* tb)
{
tb->begin = tb->end = tb->next = tb->cursor = NULL;
}
/*___inittextblock()_________________________________________________________*/
/****rewindtextblock()********************************************************/
void rewindtextblock(textblock* tb)
{
tb->cursor = tb->begin; /*just move cursor back to beginning*/
}
/*___rewindtextblock()_______________________________________________________*/
/*3456789_123456789_123456789_123456789_123456789_123456789_123456789_12345678*/
/****disposetextblock()*******************************************************/
void disposetextblock(textblock* tb)
{
if (tb->begin)
{
free(tb->begin); /*actually releases any allocated space*/
inittextblock(tb); /*just sets member pointers==0*/
}
}
/*___disposetextblock()______________________________________________________*/
/*3456789_123456789_123456789_123456789_123456789_123456789_123456789_12345678*/
/****putonetextblockline()****************************************************/
void putonetextblockline(textblock* tb, char* thestring)
{
static char stringtemp[256];
/*store directly in giant character array, textblock */
if( (unsigned)(tb->end - tb->next) < strlen(thestring)+8 ) /*give some slack*/
{/*build more space*/
strcpy(stringtemp,thestring);/*leaving this routine forgets contents*/
buildtextblock(tb, 10000); /*PKINSCRT.c*/ /*try for (n) more */
strcpy(thestring,stringtemp);
}
if( (unsigned)(tb->end - tb->next) > strlen(thestring) ) /*insurance */
{
strcpy(tb->next,thestring);
tb->next += strlen(tb->next) + 1;
}
}
/*___putonetextblockline()___________________________________________________*/
/****getonetextblockline()****************************************************/
void getonetextblockline(textblock* tb, char* thestring)
{
/*get one character string from textblock, giant character array*/
if(tb->cursor < tb->next) /*next available open space*/
{
strcpy(thestring,tb->cursor);
tb->cursor += strlen(tb->cursor) + 1;
}
else thestring[0] = '\0'; /*NULL string signel for end of data*/
return;
}
/*___getonetextblockline()___________________________________________________*/
/****buildtextblock()*********************************************************/
void buildtextblock(textblock* tb, size_t more)
{
textblock tbtemp;
/*members: char *begin, *end, *next, *cursor */
size_t length,nextoffset;
int ifail, recycle, moretry;
tbtemp.begin = NULL;
tbtemp.end = NULL;
tbtemp.next = NULL;
tbtemp.cursor = NULL;
moretry = more;
recycle = 1;
while(recycle==1)
{/*recycle allocation trials*/
ifail = 0;
length = tb->end - tb->begin;
nextoffset = tb->next - tb->begin;
if(tb->begin)
{/*exists: realloc more space*/
length = tb->end - tb->begin;
tbtemp.begin=(char *)realloc(tb->begin,sizeof(char)*(length+more));
if(tbtemp.begin==NULL) ifail = 1;
}
else
{/*not previously built, malloc initial "more" space*/
length = 0;
nextoffset = 0;
tbtemp.begin = (char *)malloc(sizeof(char)*more);
if(tbtemp.begin==NULL) ifail = 1;
}
if(ifail)
{/*failed to do a reallocation, reduce request and try again*/
more = more/2;
if(more > 3) recycle = 1;
else recycle = 0;
}
else recycle = 0;
}/*recycle allocation trials*/
if(ifail==0)
{
tb->begin = tbtemp.begin; /*reassign pointer*/
tb->next = tb->begin + nextoffset; /*reset*/
tb->cursor = tb->next; /*current position*/
tb->end = tb->begin + sizeof(char)*(length+more);
}
if(Ltestprint||ifail)
{
if(ifail)
sprintf(alertstr,CRLF"+%d text block reallocation failed"CRLF
", remains: %ld"
,moretry,(tb->end - tb->begin));
else
sprintf(alertstr,CRLF"text block allocation now == %ld"CRLF
,(tb->end - tb->begin));
pkintextinsert(alertstr);
adjusttext(1);
/*DoReportDialog(); */ /*___MAIN.C*/
}
}
/*___buildtextblock()________________________________________________________*/