summaryrefslogtreecommitdiffstats
path: root/lib/qs_pcsub.h
blob: 8ea241c8a529f0b40e4c23ec4f814d02180cd2b5 (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
/* Substitution of percent escapes in qstring -- header
 * Copyright (C) 2009 Chris Hall (GMCH), Highwayman
 *
 * This file is part of GNU Zebra.
 *
 * GNU Zebra 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, or (at your
 * option) any later version.
 *
 * GNU Zebra 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 GNU Zebra; see the file COPYING.  If not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifndef _ZEBRA_QS_PCSUB_H
#define _ZEBRA_QS_PCSUB_H

#include "misc.h"
#include "qstring.h"

/*==============================================================================
 * Fairly general purpose engine to substitute "%...x" escapes in a qstring.
 *
 * A "%...x" escape is up to 22 characters starting '%' and ending 'a'..'z' or
 * 'A'..'Z'.  The (up to 20) characters between may be anything ' '..'\x7E',
 * except for '%', 'a'..'z' and 'A'..'Z' and are escape specific modifiers.
 * A pair of "%%" is also an escape.
 *
 * Anything which is not an escape according to the above is ignored, including
 * escapes which are too long.
 *
 * The substitution process is driven by a table, whose entries take the
 * form:
 *
 *   'x'    -- the escape "action" char (alphabetic)
 *   parser -- address of escape parsing function
 *   int    -- extra parameter for parser
 *
 * The process is also given an "argument array", which is an array of void*
 * pointers, also passed to the parser.
 *
 * The parser function verifies the escape, and returns either a qstring
 * containing the substitution text, or NULL indicating that the escape is
 * not to be substituted (perhaps because is invalid).
 *
 * There is a default parser which expects the escape to have no modifiers, and
 * for the extra parameter to be the index into the argument vector, which is
 * assumed to give the address of a '\0' terminated string.
 *
 * The table is terminated by the required '%' entry -- the parser and extra
 * fields of which are ignored.
 */

typedef struct
{
  uint  count ;
  void* args[] ;

} qs_pcsub_args_t ;

typedef qs_pcsub_args_t* qs_pcsub_args ;

enum { qs_pcsub_esc_max = 20 } ;
typedef char qs_pcsub_esc_t[qs_pcsub_esc_max + 1] ;     /* '\0' terminated */

typedef qstring qs_pcsub_parser_f(qstring qs, char action, qs_pcsub_esc_t esc,
                                                qs_pcsub_args args, int extra) ;
typedef struct
{
  char  action ;
  qs_pcsub_parser_f*
        parser ;
  int   extra ;

} qs_pcsub_entry_t ;

typedef qs_pcsub_entry_t  qs_pcsub_table_t[] ;
typedef qs_pcsub_entry_t* qs_pcsub_table ;

/*==============================================================================
 * Functions
 */

extern void qs_pcsub(qstring qs, const qs_pcsub_table table, ...) ;
extern qs_pcsub_parser_f qs_pcsub_default ;
extern void* qs_pcsub_arg(qs_pcsub_args args, int index) ;

#endif /* _ZEBRA_QS_PCSUB_H */