summaryrefslogtreecommitdiff
path: root/support/chktex/Utility.h
blob: 7dc7c9a31788cf895d21d975161f22df4ce92bd3 (plain)
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
/*
 *  ChkTeX, utility functions -- header file.
 *  Copyright (C) 1995-96 Jens T. Berger Thielemann
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *  Contact the author at:
 *              Jens Berger
 *              Spektrumvn. 4
 *              N-0666 Oslo
 *              Norway
 *              E-mail: <jensthi@ifi.uio.no>
 *
 *
 */

#ifndef UTILITY_H
#define UTILITY_H 1

#include "ChkTeX.h"
#include "OpSys.h"

/*
 * How many indexes we'll allocate first time
 */
#define MINPUDDLE       256

/*
 * How many bytes we want in front/end of each char buffer. > 2
 */
#define WALLBYTES       4

#ifndef HASH_SIZE
#  define HASH_SIZE    1009     /* A reasonably large prime */
#endif

#define FORWL(ind, list)  for(ind = 0; ind < (list).Stack.Used; ind++)


enum Strip
{
    STRP_LFT = 0x01,
    STRP_RGT = 0x02,
    STRP_BTH = 0x03
};

struct HashEntry
{
    struct HashEntry *Next;
    char *Str;
};

struct Hash
{
    struct HashEntry **Index;
};

struct Stack
{
    void **Data;
    unsigned long Size, Used;
};

struct WordList
{
    unsigned long MaxLen;
    int NonEmpty;
    struct Stack Stack;
    struct Hash Hash;
};
#define WORDLIST_DEFINED

struct FileNode
{
    char *Name;
    FILE *fh;
    unsigned long Line;
};

/* Subtract 1 because sizeof includes the null terminator.
 * WARNING: To use this on a variable, the type should be char[]
 * rather than char*, since for some versions of gcc these give
 * different values. */
#define STRLEN(x)  (sizeof(x)/sizeof(x[0]) - 1)

int fexists(const char *Filename);

void *sfmemset(void *to, int c, long n);
void *saferealloc(void *old, size_t newsize);

int strafter(const char *Str, const char *Cmp);
void strrep(char *String, const char From, const char To);
void strxrep(char *Buf, const char *Prot, const char To);
char *strip(char *String, const enum Strip What);
void strwrite(char *To, const char *From, unsigned long Len);
int strinfront(const char *Str, const char *Cmp);
char *strdupx(const char *String, int Extra);
void strmove(char *a, const char *b);

void ClearHash(struct Hash *h);
void InsertHash(char *a, struct Hash *h);
char *HasHash(const char *a, const struct Hash *h);

int InsertWord(const char *Word, struct WordList *WL);
char *HasWord(const char *Word, struct WordList *WL);
void MakeLower(struct WordList *wl);
void ListRep(struct WordList *wl, const char From, const char To);
void ClearWord(struct WordList *WL);

int StkPush(void *Data, struct Stack *Stack);
void *StkPop(struct Stack *Stack);
void *StkTop(struct Stack *Stack);

FILE *CurStkFile(struct Stack *stack);
const char *CurStkName(struct Stack *stack);
unsigned long CurStkLine(struct Stack *stack);
long CurStkMode(struct Stack *stack);
long *PushMode(long mode, struct Stack *stack);
char *FGetsStk(char *Dest, unsigned long len, struct Stack *stack);
int PushFileName(const char *Name, struct Stack *stack);
int PushFile(const char *, FILE *, struct Stack *);


void FreeErrInfo(struct ErrInfo *ei);
struct ErrInfo *PushChar(const char c, const unsigned long Line,
                         const unsigned long Column, struct Stack *Stk,
                         const char *LineCpy);
struct ErrInfo *PushErr(const char *Data, const unsigned long Line,
                        const unsigned long Column,
                        const unsigned long ErrLen, const char *LineCpy,
                        struct Stack *Stk);
struct ErrInfo *TopChar(struct Stack *Stack);
struct ErrInfo *TopErr(struct Stack *Stack);
struct ErrInfo *PopErr(struct Stack *Stack);
struct ErrInfo *TopMatch(struct Stack *Stack, char *String);

long BrackIndex(const char c);
void AddBracket(const char c);
char MatchBracket(const char c);



short substring(const char *source, char *dest, unsigned long pos, long len);

#ifndef  HAVE_STRLWR
#  define  strlwr  mystrlwr
char *mystrlwr(char *String);
#endif

#ifndef  HAVE_STRDUP
#  define  strdup  mystrdup
char *mystrdup(const char *String);
#endif


#ifndef HAVE_STRCASECMP
int strcasecmp(char *a, char *b);
#endif

#if !(defined HAVE_DECL_STPCPY && HAVE_DECL_STPCPY)
static inline char * stpcpy(char *dest, const char *src)
{
    return strcpy(dest, src) + strlen(src);
}
#endif


#endif /* UTILITY_H */