Skip to content

Commit 0c0168f

Browse files
digama0wlammen
andauthored
add types for temp_(v/nmbr/pntr)String, use const (#37)
* add types for temp_(v/nmbr/pntr)String, use const * improve docs * make len const * fix signed char dependence in ascii_ Co-authored-by: Wolf Lammen <[email protected]>
1 parent 8b442fa commit 0c0168f

File tree

12 files changed

+424
-467
lines changed

12 files changed

+424
-467
lines changed

src/mmdata.c

Lines changed: 127 additions & 185 deletions
Large diffs are not rendered by default.

src/mmdata.h

Lines changed: 64 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ extern flag g_toolsMode; /* In metamath mode: 0 = metamath, 1 = tools */
2222
typedef long nmbrString; /* String of numbers */
2323
typedef void* pntrString; /* String of pointers */
2424

25+
/* A nmbrString allocated in temporary storage. These strings will be deallocated
26+
after the next call to `nmbrLet`.
27+
See also `temp_vstring` for information on how temporaries are handled. */
28+
typedef nmbrString temp_nmbrString;
2529

30+
/* A pntrString allocated in temporary storage. These strings will be deallocated
31+
after the next call to `pntrLet`.
32+
See also `temp_vstring` for information on how temporaries are handled. */
33+
typedef pntrString temp_pntrString;
2634

2735
enum mTokenType { var_, con_ };
2836
#define lb_ '{' /* ${ */
@@ -186,7 +194,7 @@ void initBigArrays(void);
186194
long getFreeSpace(long max);
187195

188196
/* Fatal memory allocation error */
189-
void outOfMemory(vstring msg);
197+
void outOfMemory(const char *msg);
190198

191199
/* Bug check error */
192200
void bug(int bugNum);
@@ -213,13 +221,13 @@ extern struct nullPntrStruct g_PntrNull;
213221

214222
/* This function returns a 1 if any entry in a comma-separated list
215223
matches using the matches() function. */
216-
flag matchesList(vstring testString, vstring pattern, char wildCard,
224+
flag matchesList(const char *testString, const char *pattern, char wildCard,
217225
char oneCharWildCard);
218226

219227
/* This function returns a 1 if the first argument matches the pattern of
220228
the second argument. The second argument may have 0-or-more and
221229
exactly-1 character match wildcards, typically '*' and '?'.*/
222-
flag matches(vstring testString, vstring pattern, char wildCard,
230+
flag matches(const char *testString, const char *pattern, char wildCard,
223231
char oneCharWildCard);
224232

225233

@@ -247,117 +255,114 @@ void nmbrMakeTempAlloc(nmbrString *s);
247255

248256

249257
/* String assignment - MUST be used to assign vstrings */
250-
void nmbrLet(nmbrString **target,nmbrString *source);
258+
void nmbrLet(nmbrString **target, const nmbrString *source);
251259

252260
/* String concatenation - last argument MUST be NULL */
253-
nmbrString *nmbrCat(nmbrString *string1,...);
261+
temp_nmbrString *nmbrCat(const nmbrString *string1,...);
254262

255263
/* Emulation of nmbrString functions similar to BASIC string functions */
256-
nmbrString *nmbrSeg(nmbrString *sin, long p1, long p2);
257-
nmbrString *nmbrMid(nmbrString *sin, long p, long l);
258-
nmbrString *nmbrLeft(nmbrString *sin, long n);
259-
nmbrString *nmbrRight(nmbrString *sin, long n);
264+
temp_nmbrString *nmbrSeg(const nmbrString *sin, long p1, long p2);
265+
temp_nmbrString *nmbrMid(const nmbrString *sin, long p, long l);
266+
temp_nmbrString *nmbrLeft(const nmbrString *sin, long n);
267+
temp_nmbrString *nmbrRight(const nmbrString *sin, long n);
260268

261269
/* Allocate and return an "empty" string n "characters" long */
262-
nmbrString *nmbrSpace(long n);
270+
temp_nmbrString *nmbrSpace(long n);
263271

264-
long nmbrLen(nmbrString *s);
265-
long nmbrAllocLen(nmbrString *s);
272+
long nmbrLen(const nmbrString *s);
273+
long nmbrAllocLen(const nmbrString *s);
266274
void nmbrZapLen(nmbrString *s, long length);
267275

268276
/* Search for string2 in string 1 starting at start_position */
269-
long nmbrInstr(long start, nmbrString *sin, nmbrString *s);
277+
long nmbrInstr(long start, const nmbrString *sin, const nmbrString *s);
270278

271279
/* Search for string2 in string 1 in reverse starting at start_position */
272280
/* (Reverse nmbrInstr) */
273-
long nmbrRevInstr(long start_position,nmbrString *string1,
274-
nmbrString *string2);
281+
long nmbrRevInstr(long start_position, const nmbrString *string1,
282+
const nmbrString *string2);
275283

276284
/* 1 if strings are equal, 0 otherwise */
277-
int nmbrEq(nmbrString *sout,nmbrString *sin);
285+
flag nmbrEq(const nmbrString *s, const nmbrString *t);
278286

279287
/* Converts mString to a vstring with one space between tokens */
280-
vstring nmbrCvtMToVString(nmbrString *s);
288+
temp_vstring nmbrCvtMToVString(const nmbrString *s);
281289

282290
/* Converts rString to a vstring with one space between tokens */
283-
vstring nmbrCvtRToVString(nmbrString *s,
291+
temp_vstring nmbrCvtRToVString(const nmbrString *s,
284292
flag explicitTargets,
285293
long statemNum);
286294

287295
/* Get step numbers in an rString - needed by cvtRToVString & elsewhere */
288-
nmbrString *nmbrGetProofStepNumbs(nmbrString *reason);
296+
nmbrString *nmbrGetProofStepNumbs(const nmbrString *reason);
289297

290298
/* Converts any nmbrString to an ASCII string of numbers corresponding
291299
to the .tokenNum field -- used for debugging only. */
292-
vstring nmbrCvtAnyToVString(nmbrString *s);
300+
temp_vstring nmbrCvtAnyToVString(const nmbrString *s);
293301

294302
/* Extract variables from a math token string */
295-
nmbrString *nmbrExtractVars(nmbrString *m);
303+
temp_nmbrString *nmbrExtractVars(const nmbrString *m);
296304

297305
/* Determine if an element is in a nmbrString; return position if it is */
298-
long nmbrElementIn(long start, nmbrString *g, long element);
306+
long nmbrElementIn(long start, const nmbrString *g, long element);
299307

300308
/* Add a single number to end of a nmbrString - faster than nmbrCat */
301-
nmbrString *nmbrAddElement(nmbrString *g, long element);
309+
temp_nmbrString *nmbrAddElement(const nmbrString *g, long element);
302310

303311
/* Get the set union of two math token strings (presumably
304312
variable lists) */
305-
nmbrString *nmbrUnion(nmbrString *m1,nmbrString *m2);
313+
temp_nmbrString *nmbrUnion(const nmbrString *m1, const nmbrString *m2);
306314

307315
/* Get the set intersection of two math token strings (presumably
308316
variable lists) */
309-
nmbrString *nmbrIntersection(nmbrString *m1,nmbrString *m2);
317+
temp_nmbrString *nmbrIntersection(const nmbrString *m1, const nmbrString *m2);
310318

311319
/* Get the set difference m1-m2 of two math token strings (presumably
312320
variable lists) */
313-
nmbrString *nmbrSetMinus(nmbrString *m1,nmbrString *m2);
321+
temp_nmbrString *nmbrSetMinus(const nmbrString *m1,const nmbrString *m2);
314322

315323

316324

317325
/* This is a utility function that returns the length of a subproof that
318326
ends at step */
319-
long nmbrGetSubproofLen(nmbrString *proof, long step);
327+
long nmbrGetSubproofLen(const nmbrString *proof, long step);
320328

321329
/* This function returns a "squished" proof, putting in {} references
322330
to previous subproofs. */
323-
nmbrString *nmbrSquishProof(nmbrString *proof);
331+
temp_nmbrString *nmbrSquishProof(const nmbrString *proof);
324332

325333
/* This function unsquishes a "squished" proof, replacing {} references
326-
to previous subproofs by the subproofs themselvs. The returned nmbrString
327-
must be deallocated by the caller. */
328-
nmbrString *nmbrUnsquishProof(nmbrString *proof);
334+
to previous subproofs by the subproofs themselves. */
335+
temp_nmbrString *nmbrUnsquishProof(const nmbrString *proof);
329336

330337
/* This function returns the indentation level vs. step number of a proof
331338
string. This information is used for formatting proof displays. The
332339
function calls itself recursively, but the first call should be with
333-
startingLevel = 0. The caller is responsible for deallocating the
334-
result. */
335-
nmbrString *nmbrGetIndentation(nmbrString *proof,
340+
startingLevel = 0. */
341+
temp_nmbrString *nmbrGetIndentation(const nmbrString *proof,
336342
long startingLevel);
337343

338344
/* This function returns essential (1) or floating (0) vs. step number of a
339345
proof string. This information is used for formatting proof displays. The
340346
function calls itself recursively, but the first call should be with
341-
startingLevel = 0. The caller is responsible for deallocating the
342-
result. */
343-
nmbrString *nmbrGetEssential(nmbrString *proof);
347+
startingLevel = 0. */
348+
temp_nmbrString *nmbrGetEssential(const nmbrString *proof);
344349

345350
/* This function returns the target hypothesis vs. step number of a proof
346351
string. This information is used for formatting proof displays. The
347-
function calls itself recursively. The caller is responsible for
348-
deallocating the result. statemNum is the statement being proved. */
349-
nmbrString *nmbrGetTargetHyp(nmbrString *proof, long statemNum);
352+
function calls itself recursively.
353+
statemNum is the statement being proved. */
354+
temp_nmbrString *nmbrGetTargetHyp(const nmbrString *proof, long statemNum);
350355

351356
/* Converts a proof string to a compressed-proof-format ASCII string.
352357
Normally, the proof string would be compacted with squishProof first,
353358
although it's not a requirement. */
354359
/* The statement number is needed because required hypotheses are
355360
implicit in the compressed proof. */
356-
vstring compressProof(nmbrString *proof, long statemNum,
361+
temp_vstring compressProof(const nmbrString *proof, long statemNum,
357362
flag oldCompressionAlgorithm);
358363

359364
/* Gets length of the ASCII form of a compressed proof */
360-
long compressedProofSize(nmbrString *proof, long statemNum);
365+
long compressedProofSize(const nmbrString *proof, long statemNum);
361366

362367

363368
/*******************************************************************/
@@ -383,48 +388,48 @@ void pntrMakeTempAlloc(pntrString *s);
383388

384389

385390
/* String assignment - MUST be used to assign vstrings */
386-
void pntrLet(pntrString **target,pntrString *source);
391+
void pntrLet(pntrString **target, const pntrString *source);
387392

388393
/* String concatenation - last argument MUST be NULL */
389-
pntrString *pntrCat(pntrString *string1,...);
394+
temp_pntrString *pntrCat(const pntrString *string1,...);
390395

391396
/* Emulation of pntrString functions similar to BASIC string functions */
392-
pntrString *pntrSeg(pntrString *sin, long p1, long p2);
393-
pntrString *pntrMid(pntrString *sin, long p, long length);
394-
pntrString *pntrLeft(pntrString *sin, long n);
395-
pntrString *pntrRight(pntrString *sin, long n);
397+
temp_pntrString *pntrSeg(const pntrString *sin, long p1, long p2);
398+
temp_pntrString *pntrMid(const pntrString *sin, long p, long length);
399+
temp_pntrString *pntrLeft(const pntrString *sin, long n);
400+
temp_pntrString *pntrRight(const pntrString *sin, long n);
396401

397402
/* Allocate and return an "empty" string n "characters" long */
398-
pntrString *pntrSpace(long n);
403+
temp_pntrString *pntrSpace(long n);
399404

400405
/* Allocate and return an "empty" string n "characters" long
401406
initialized to nmbrStrings instead of vStrings */
402-
pntrString *pntrNSpace(long n);
407+
temp_pntrString *pntrNSpace(long n);
403408

404409
/* Allocate and return an "empty" string n "characters" long
405410
initialized to pntrStrings instead of vStrings */
406-
pntrString *pntrPSpace(long n);
411+
temp_pntrString *pntrPSpace(long n);
407412

408-
long pntrLen(pntrString *s);
409-
long pntrAllocLen(pntrString *s);
413+
long pntrLen(const pntrString *s);
414+
long pntrAllocLen(const pntrString *s);
410415
void pntrZapLen(pntrString *s, long length);
411416

412417
/* Search for string2 in string 1 starting at start_position */
413-
long pntrInstr(long start, pntrString *sin, pntrString *s);
418+
long pntrInstr(long start, const pntrString *sin, const pntrString *s);
414419

415420
/* Search for string2 in string 1 in reverse starting at start_position */
416421
/* (Reverse pntrInstr) */
417-
long pntrRevInstr(long start_position,pntrString *string1,
418-
pntrString *string2);
422+
long pntrRevInstr(long start_position, const pntrString *string1,
423+
const pntrString *string2);
419424

420425
/* 1 if strings are equal, 0 otherwise */
421-
int pntrEq(pntrString *sout,pntrString *sin);
426+
flag pntrEq(const pntrString *sout, const pntrString *sin);
422427

423428
/* Add a single null string element to a pntrString - faster than pntrCat */
424-
pntrString *pntrAddElement(pntrString *g);
429+
temp_pntrString *pntrAddElement(const pntrString *g);
425430

426431
/* Add a single null pntrString element to a pntrString - faster than pntrCat */
427-
pntrString *pntrAddGElement(pntrString *g);
432+
temp_pntrString *pntrAddGElement(const pntrString *g);
428433

429434
/* Utility functions */
430435

src/mminou.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ flag backFromCmdInput = 0; /* User typed "B" at main prompt */
6666
/* Returns 0 if user typed "q" during scroll prompt; this lets a procedure
6767
interrupt it's output for speedup (rest of output will be suppressed anyway
6868
until next command line prompt) */
69-
flag print2(char* fmt, ...) {
69+
flag print2(const char* fmt, ...) {
7070
/* This performs the same operations as printf, except that if a log file is
7171
open, the characters will also be printed to the log file. */
7272
/* Also, scrolling is paused at each page if in scroll-prompted mode. */
@@ -356,8 +356,7 @@ flag print2(char* fmt, ...) {
356356
/* Special: if breakMatch is " (quote), treat as if space but don't break
357357
quotes, and also let lines grow long - use this call for all HTML
358358
code */
359-
void printLongLine(vstring line, vstring startNextLine, vstring breakMatch)
360-
{
359+
void printLongLine(const char *line, const char *startNextLine, const char *breakMatch) {
361360
vstring longLine = "";
362361
vstring multiLine = "";
363362
vstring prefix = "";
@@ -606,7 +605,7 @@ void printLongLine(vstring line, vstring startNextLine, vstring breakMatch)
606605
} /* printLongLine */
607606

608607

609-
vstring cmdInput(FILE *stream, vstring ask) {
608+
vstring cmdInput(FILE *stream, const char *ask) {
610609
/* This function prints a prompt (if 'ask' is not NULL) and gets a line from
611610
the input stream. NULL is returned when end-of-file is encountered.
612611
New memory is allocated each time linput is called. This space must
@@ -733,8 +732,7 @@ vstring cmdInput(FILE *stream, vstring ask) {
733732
return g;
734733
} /* cmdInput */
735734

736-
vstring cmdInput1(vstring ask)
737-
{
735+
vstring cmdInput1(const char *ask) {
738736
/* This function gets a line from either the terminal or the command file
739737
stream depending on g_commandFileNestingLevel > 0. It calls cmdInput(). */
740738
/* Warning: the calling program must deallocate the returned string. */
@@ -956,7 +954,7 @@ void errorMessage(vstring line, long lineNum, long column, long tokenLength,
956954

957955
/* Opens files with error message; opens output files with
958956
backup of previous version. Mode must be "r" or "w" or "d" (delete). */
959-
FILE *fSafeOpen(vstring fileName, vstring mode, flag noVersioningFlag) {
957+
FILE *fSafeOpen(const char *fileName, const char *mode, flag noVersioningFlag) {
960958
FILE *fp;
961959
vstring prefix = "";
962960
vstring postfix = "";
@@ -1103,8 +1101,7 @@ FILE *fSafeOpen(vstring fileName, vstring mode, flag noVersioningFlag) {
11031101

11041102
/* Renames a file with backup of previous version. If non-zero
11051103
is returned, there was an error. */
1106-
int fSafeRename(vstring oldFileName, vstring newFileName)
1107-
{
1104+
int fSafeRename(const char *oldFileName, const char *newFileName) {
11081105
int error = 0;
11091106
int i;
11101107
FILE *fp;
@@ -1154,8 +1151,7 @@ int fSafeRename(vstring oldFileName, vstring newFileName)
11541151
str1 = fTmpName("zz~list"); ]
11551152
The file whose name is the returned string is not left open;
11561153
the caller must separately open the file. */
1157-
vstring fGetTmpName(vstring filePrefix)
1158-
{
1154+
vstring fGetTmpName(const char *filePrefix) {
11591155
FILE *fp;
11601156
vstring fname = "";
11611157
static long counter = 0;
@@ -1182,7 +1178,7 @@ vstring fGetTmpName(vstring filePrefix)
11821178
must deallocate the returned string. If a NULL is returned, the file
11831179
could not be opened or had a non-ASCII Unicode character or some other
11841180
problem. If verbose is 0, error and warning messages are suppressed. */
1185-
vstring readFileToString(vstring fileName, char verbose, long *charCount) {
1181+
vstring readFileToString(const char *fileName, char verbose, long *charCount) {
11861182
FILE *inputFp;
11871183
long fileBufSize;
11881184
char *fileBuf;

src/mminou.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extern vstring g_input_fn, g_output_fn; /* File names */
3434

3535
/* Warning: never call print2 with string longer than PRINTBUFFERSIZE - 1 */
3636
/* print2 returns 0 if the user has quit the printout. */
37-
flag print2(char* fmt,...);
37+
flag print2(const char* fmt,...);
3838
extern long g_screenHeight; /* Height of screen */
3939
extern long g_screenWidth; /* Width of screen */
4040
#define MAX_LEN 79 /* Default width of screen */
@@ -43,34 +43,35 @@ extern flag g_scrollMode; /* Flag for continuous or prompted scroll */
4343
extern flag g_quitPrint; /* Flag that user typed 'q' to last scrolling prompt */
4444

4545
/* printLongLine automatically puts a newline \n in the output line. */
46-
void printLongLine(vstring line, vstring startNextLine, vstring breakMatch);
47-
vstring cmdInput(FILE *stream,vstring ask);
48-
vstring cmdInput1(vstring ask);
46+
void printLongLine(const char *line, const char *startNextLine, const char *breakMatch);
47+
vstring cmdInput(FILE *stream, const char *ask);
48+
vstring cmdInput1(const char *ask);
49+
flag cmdInputIsY(const char *ask);
4950

5051
enum severity {notice_,warning_,error_,fatal_};
5152
void errorMessage(vstring line, long lineNum, long column, long tokenLength,
5253
vstring error, vstring fileName, long statementNum, flag warnFlag);
5354

5455
/* Opens files with error message; opens output files with
5556
backup of previous version. Mode must be "r" or "w". */
56-
FILE *fSafeOpen(vstring fileName, vstring mode, flag noVersioningFlag);
57+
FILE *fSafeOpen(const char *fileName, const char *mode, flag noVersioningFlag);
5758

5859
/* Renames a file with backup of previous version. If non-zero
5960
is returned, there was an error. */
60-
int fSafeRename(vstring oldFileName, vstring newFileName);
61+
int fSafeRename(const char *oldFileName, const char *newFileName);
6162

6263
/* Finds the name of the first file of the form filePrefix +
6364
nnn + ".tmp" that does not exist. THE CALLER MUST DEALLOCATE
6465
THE RETURNED STRING. */
65-
vstring fGetTmpName(vstring filePrefix);
66+
vstring fGetTmpName(const char *filePrefix);
6667

6768
/* This function returns a character string containing the entire contents of
6869
an ASCII file, or Unicode file with only ASCII characters. On some
6970
systems it is faster than reading the file line by line. THE CALLER
7071
MUST DEALLOCATE THE RETURNED STRING. If a NULL is returned, the file
7172
could not be opened or had a non-ASCII Unicode character or some other
7273
problem. If verbose is 0, error and warning messages are suppressed. */
73-
vstring readFileToString(vstring fileName, char verbose, long *charCount);
74+
vstring readFileToString(const char *fileName, char verbose, long *charCount);
7475

7576
/* Returns total elapsed time in seconds since starting session (for the
7677
lcc compiler) or the CPU time used (for the gcc compiler). The

0 commit comments

Comments
 (0)