aboutsummaryrefslogtreecommitdiff
path: root/project/dclib/dclib-regex.h
blob: 0a319984c2631d86317497e76c0564bf87a8b40e (plain) (blame)
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

/***************************************************************************
 *                                                                         *
 *                     _____     ____                                      *
 *                    |  __ \   / __ \   _     _ _____                     *
 *                    | |  \ \ / /  \_\ | |   | |  _  \                    *
 *                    | |   \ \| |      | |   | | |_| |                    *
 *                    | |   | || |      | |   | |  ___/                    *
 *                    | |   / /| |   __ | |   | |  _  \                    *
 *                    | |__/ / \ \__/ / | |___| | |_| |                    *
 *                    |_____/   \____/  |_____|_|_____/                    *
 *                                                                         *
 *                       Wiimms source code library                        *
 *                                                                         *
 ***************************************************************************
 *                                                                         *
 *        Copyright (c) 2012-2021 by Dirk Clemens <wiimm@wiimm.de>         *
 *                                                                         *
 ***************************************************************************
 *                                                                         *
 *   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.                          *
 *                                                                         *
 *   See file gpl-2.0.txt or http://www.gnu.org/licenses/gpl-2.0.txt       *
 *                                                                         *
 ***************************************************************************/

#ifndef DCLIB_REGEX_H
#define DCLIB_REGEX_H 1

#if DCLIB_USE_PCRE
  #include <pcre.h>
  #define DC_REGEX_TYPE pcre
#else
  #ifndef DCLIB_USE_REGEX
    #define DCLIB_USE_REGEX 1
  #endif
  #if DCLIB_USE_REGEX
    #include <regex.h>
    #define DC_REGEX_TYPE regex_t
  #else
    #error Nn Regex Support!
  #endif
#endif

#include "dclib-types.h"
#include "dclib-debug.h"
#include "dclib-basics.h"

//
///////////////////////////////////////////////////////////////////////////////
///////////////			struct Regex_t			///////////////
///////////////////////////////////////////////////////////////////////////////
// [[RegexReplace_t]]

typedef struct RegexReplace_t
{
    mem_t	str;	// first:  string to replace (maybe 0 bytes)
    int		ref;	// second: back reference to copy (if >=0)
}
RegexReplace_t;

///////////////////////////////////////////////////////////////////////////////
// [[RegexElem_t]]

typedef struct RegexElem_t
{
    bool		valid;		// true: successfull compiled
    bool		global;		// true: replace all ('g')
    bool		icase;		// true: ignore case ('i')

 #if DCLIB_USE_PCRE
    DC_REGEX_TYPE	*regex;		// not NULL: compiled regular expression
 #else
    DC_REGEX_TYPE	regex;		// compiled regular expression
 #endif

    int			opt;		// compile options
    ccp			pattern;	// pattern string, alloced
    mem_t		replace;	// replace string, alloced

    RegexReplace_t	*repl;		// replace data, use 'replace' as reference
    uint		repl_used;	// number of used elements in 'repl'
    uint		repl_size;	// number of available elements in 'repl'
}
RegexElem_t;

///////////////////////////////////////////////////////////////////////////////
// [[RegexMain_t]]

typedef struct Regex_t
{
    bool		valid;		// true: succesfull initialized

    RegexElem_t		*re_list;	// list; either 're_pool' or alloced
    RegexElem_t		re_pool[3];	// pool for fast access
    uint		re_used;	// number of used elements in 're_list'
    uint		re_size;	// number of available elements in 're_list'
}
Regex_t;

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

void InitializeRegex ( Regex_t *re );
void ResetRegex ( Regex_t *re );

enumError ScanRegex ( Regex_t *re, bool init_re, ccp regex );

int ReplaceRegex
(
    Regex_t	*re,		// valid Regex_t
    FastBuf_t	*res,		// return buffer, cleared
    ccp		src,
    int		src_len		// -1: use strlen()
);

//
///////////////////////////////////////////////////////////////////////////////
///////////////			    E N D			///////////////
///////////////////////////////////////////////////////////////////////////////

#endif // DCLIB_REGEX_H