File: bcpp.h

package info (click to toggle)
bcpp 0.0.20210108-1.1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 952 kB
  • sloc: cpp: 4,955; sh: 3,262; makefile: 237
file content (413 lines) | stat: -rw-r--r-- 12,878 bytes parent folder | download | duplicates (2)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/******************************************************************************
 * Copyright 1996-2009,2021 by Thomas E. Dickey                               *
 * All Rights Reserved.                                                       *
 *                                                                            *
 * Permission to use, copy, modify, and distribute this software and its      *
 * documentation for any purpose and without fee is hereby granted, provided  *
 * that the above copyright notice appear in all copies and that both that    *
 * copyright notice and this permission notice appear in supporting           *
 * documentation, and that the name of the above listed copyright holder(s)   *
 * not be used in advertising or publicity pertaining to distribution of the  *
 * software without specific, written prior permission. THE ABOVE LISTED      *
 * COPYRIGHT HOLDER(S) DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,  *
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO     *
 * EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY         *
 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER       *
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF       *
 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN        *
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.                   *
 ******************************************************************************/
// $Id: bcpp.h,v 1.49 2021/01/08 23:31:24 tom Exp $

#ifndef _BCPP_HEADER
#define _BCPP_HEADER

#include <string.h>
#include <sys/types.h>

#ifdef HAVE_CONFIG_H
#include "autoconf.h"
#endif

#include "config.h"
#include "anyobj.h"            // Use ANYOBJECT base class
#include "baseq.h"             // QueueList class to store Output structures
#include "stacklis.h"          // StackList class to store indentStruct

// Common/miscellaneous definitions for BCPP

#define TABLESIZE(n) (sizeof(n)/sizeof(n[0]))

#ifndef VERSION
#define VERSION ""
#endif

#ifdef DEBUG
#define TRACE(p) trace p
#else
#define TRACE(p) /*nothing*/
#endif

#if defined(sun) && !defined(__SVR4)    // SunOS
extern "C" {
    extern char *strcat(char *, const char *);
    extern char *strncpy(const char *, const char *, size_t);
    extern char *strstr(const char *, const char *);
    extern int strncmp(const char *, const char *, size_t);
};
#endif

#ifdef __GNUC__
#define HAVE_UNISTD_H 1
#else
#define bool int        // FIXME
#endif

//-----------------------------------------------------------------------------
// Character-state, chosen to allow storing as a null-terminated char-string
enum CharState {
    NullC   = 0,
    Blank   = ' ',      // whitespace not in other categories
    PreProc = '#',      // preprocessor (first chunk, with '#')
    Normal  = '.',      // code
    DQuoted = '"',      // "string"
    SQuoted = '\'',     // 'c'
    Comment = 'c',      // C comment
    Ignore  = 'C'       // C++ comment
    };

// ----------------------------------------------------------------------------
enum  DataTypes { NoType = 0, CCom = 1,   CppCom = 2, Code  = 3,
                  OBrace = 4, CBrace = 5, PreP = 6, ELine = 7
                };

// ----------------------------------------------------------------------------
enum IndentAttr { noIndent=0, oneLine=1, multiLine=2, blockLine=3 };

// ----------------------------------------------------------------------------
typedef struct {
    const char *name;
    IndentAttr code;
} IndentwordStruct;

extern const IndentwordStruct pIndentWords[];

// ----------------------------------------------------------------------------
// This structure is used to store line data that is de-constructed from the
// user's input file.

#define MY_DEFAULT \
        dataType(NoType), \
        comWcode(false), \
        offset(0), \
        pData(), \
        pState()

class InputStruct : public ANYOBJECT
{
    public:

        DataTypes dataType;   // i.e "/*" or "*/"                  (CCom)
                              //     "//"                          (CppCom)
                              //     Code (a = 5; , if (a == b) .. (Code)
                              //     "{"                           (OBrace)
                              //     "}"                           (CBrace)
                              //     #define                       (PreP)
                              //     SPACES (nothing, blank line)  (ELine)

        bool comWcode;
                              //       -1 : true,  comment with code (for comment dataType)
                              //        0 : false, comment with no Code

        int offset;           // offset within original line's text
        char* pData;          // pointer to queue data !
        char* pState;         // pointer to corresponding parse-state

        inline InputStruct (DataTypes theType, int theOffset)
            : MY_DEFAULT
        {
            dataType = theType;
            offset = theOffset;
            comWcode = false;
            pState = 0;       // only non-null for code
        }

        // use defaults here:
        InputStruct(const InputStruct&);
        InputStruct& operator=(const InputStruct&);
};

#undef MY_DEFAULT

#if defined(DEBUG) || defined(DEBUG2)
extern int   totalTokens;            // token count, for debugging
#endif

// ----------------------------------------------------------------------------
// The output structure is used to hold an entire output line. The structure is
// expanded with its real tabs/spaces within the output function of the program.

#if defined(DEBUG) || defined(DEBUG2)
#define DBG_DEFAULT , thisToken(totalTokens++)
#else
#define DBG_DEFAULT
#endif

#define MY_DEFAULT \
           pType(ELine), \
           offset(0), \
           bracesLevel(0), \
           preproLevel(0), \
           indentSpace(0), \
           indentHangs(0), \
           splitElseIf(false), \
           pCode(NULL), \
           pCFlag(NULL), \
           pBrace(NULL), \
           pBFlag(NULL), \
           filler(0), \
           pComment(NULL) DBG_DEFAULT

class OutputStruct : public ANYOBJECT
{
    public:
        DataTypes pType;
        int   offset;        // offset within original line's text
        int   bracesLevel;   // curly-brace level at beginning of line
        int   preproLevel;   // curly-brace level for preprocessor lines
        int   indentSpace;   // num of spaces
        int   indentHangs;   // num of indents for continuation
        bool  splitElseIf;   // special case for aligning else/if
        char* pCode;
        char* pCFlag;        // state-flags for pCode
        char* pBrace;        // "}" or "{", with possible code-fragment
        char* pBFlag;        // state-flags for pBrace
        int   filler;        // num of spaces
        char* pComment;
#if defined(DEBUG) || defined(DEBUG2)
        int   thisToken;     // current token number
#endif

    public:
        // Constructor
        // Automate initalisation
        inline OutputStruct (DataTypes theType)
            : MY_DEFAULT
        {
            pType          = theType;
        }

        inline OutputStruct (InputStruct* theIn)
            : MY_DEFAULT
        {
            pType          = theIn -> dataType;
            offset         = theIn -> offset;
        }

        inline OutputStruct (OutputStruct* theIn)
            : MY_DEFAULT
        {
            pType          = theIn -> pType;
            offset         = theIn -> offset;
        }

        // use defaults here
        OutputStruct(const OutputStruct &);
        OutputStruct& operator=(const OutputStruct&);

        // Destructor
        // Automate destruction
        inline ~OutputStruct (void)
        {
            delete[] pCode;
            delete[] pCFlag;
            delete[] pBrace;
            delete[] pBFlag;
            delete[] pComment;
        }
};

#undef MY_DEFAULT
#undef DBG_DEFAULT

// ----------------------------------------------------------------------------

#define MY_DEFAULT \
        stmt_level(0), \
        until_parn(0), \
        parn_level(0), \
        until_curl(0), \
        curl_level(0), \
        in_aggreg(0), \
        do_aggreg(false), \
        indent(0)

class HangStruct : public ANYOBJECT
{
    private:
        int  stmt_level;     // statement-level (from oneLine=0)

        int  until_parn;     // suppress hang until right-parenthesis
        int  parn_level;     // parentheses-level

        int  until_curl;     // suppress hang until R_CURL
        int  curl_level;     // curly-brace-level

        int  in_aggreg;      // in aggregate, curly-brace-level
        bool do_aggreg;

    public:
        int  indent;

        HangStruct()
            : MY_DEFAULT
        {
        }

        void IndentHanging (OutputStruct *pOut);

    private:
        void ScanState(const char *code, const char *state);
};

#undef MY_DEFAULT

// ----------------------------------------------------------------------------
class HtmlStruct : public ANYOBJECT
{
        int state;
    public:
        HtmlStruct(void)
            : state(0)
        {
        }
        bool Active(const char *pLineData);
};

// ----------------------------------------------------------------------------

#define MY_DEFAULT \
        state(NotSQL), \
        level(0), \
        matched()

enum SqlState {
    NotSQL = 0,
    DeclSQL = 1,
    BeginSQL = 2,
    MoreSQL = 3
};

class SqlStruct : public ANYOBJECT
{
        SqlState state;
        int level;
        char matched[80];

    public:
        SqlStruct(void)
            : MY_DEFAULT
        {
            matched[0] = 0;
        }
        void IndentSQL(OutputStruct *pOut);

    private:
        int NextWord(int start, OutputStruct *pOut);
        int SkipWord(int start, OutputStruct *pOut);
        bool SqlVerb(const char *code);
};

#undef MY_DEFAULT

// ----------------------------------------------------------------------------
// This structure is used to hold indent data on non-brace code.
// This includes case statements, single line if's, while's, for statements...

#define MY_DEFAULT \
           attrib(noIndent), \
           pos(), \
           singleIndentLen()

class IndentStruct : public ANYOBJECT
{
    public:
           // attribute values ...
           // value 1 = indent code one position, until a ';' is found !
           //       2 = end on close brace, and at a position "pos"
           IndentAttr    attrib;
           int           pos;

           // Indent double the amount for multiline single if, while ...
           // statements.
           int           singleIndentLen;

    // constructor
    IndentStruct (void)
        : MY_DEFAULT
    {
        attrib          = noIndent;
        pos             = 0;
        singleIndentLen = 0; // number of spaces to indent !
    }
};

#undef MY_DEFAULT

//-----------------------------------------------------------------------------
// debug.cpp
extern void trace (const char *format, ...);
#ifdef DEBUG
extern void traceInput(const char *file, int line, InputStruct *pIn);
extern void traceIndent(const char *file, int line, IndentStruct *pIndent);
extern void traceOutput(const char *file, int line, OutputStruct *pOut);
#define TRACE_INPUT(pOut)  traceInput(__FILE__, __LINE__, pOut);
#define TRACE_INDENT(pOut) traceIndent(__FILE__, __LINE__, pOut);
#define TRACE_OUTPUT(pOut) traceOutput(__FILE__, __LINE__, pOut);
#else
#define TRACE_INPUT(pOut)  /* nothing */
#define TRACE_INDENT(pOut) /* nothing */
#define TRACE_OUTPUT(pOut) /* nothing */
#endif

inline bool emptyString(const char *s)
{
    return s == 0 || *s == 0;
}

//-----------------------------------------------------------------------------
// backup.cpp
extern int BackupFile (char*& oldFilename, char*& newFilename);
extern void RestoreIfUnchanged(char *oldFilename, char *newFilename);

// exec_sql.cpp
extern void IndentSQL (OutputStruct *pOut, int& state);

// hanging.cpp
extern void IndentHanging (OutputStruct *pOut, HangStruct& state);

// FIXME
extern int LookupKeyword(const char *tst);
extern bool ContinuedQuote(OutputStruct *pOut);

// strings.cpp
extern bool isName(char c);
extern bool CompareKeyword(const char *tst, const char *ref);
extern char *NewString (const char *src);
extern char *NewSubstring (const char *src, size_t len);
extern const char *SkipBlanks(const char *s);

// tabs.cpp
extern void ExpandTabs (char* &pString,
    int tabLen,
    int deleteChars,
    bool quoteChars,
    CharState &curState, char * &lineState, bool &codeOnLine);
extern char* TabSpacing (int mode, int col, int len, int spaceIndent);

// verbose.cpp
extern bool prompt (const char *format, ...);
extern void verbose (const char *format, ...);
extern void warning (const char *format, ...);

#endif // _BCPP_HEADER