summaryrefslogtreecommitdiff
path: root/tex/context/base/m-datastrc.tex
blob: 1fd5a9e42c42a8384453da4f2ca0d1b5b6c434df (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
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
%D \module
%D   [       file=m-datastrc, % was: core-dat % was core-02a
%D        version=1999.08.10,               % 1997.03.31,
%D          title=\CONTEXT\ Modules,
%D       subtitle=Database Support,         % 2A
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\writestatus{loading}{ConTeXt Core Macros / Database Support}

\unprotect

%D This module is a (limited) rewrite of the original \type
%D {core-02a} module, the module that dealt with managing a
%D database of addresses. The principles and methods have not
%D changed; they are only generalized.
%D
%D A database file |<|in most cases such a base is generated
%D from another one|>| is structured as follows:
%D
%D \starttyping
%D \startrecord{tag}
%D   \memberofgroup{grouplist}
%D   \setrecordentry{name}{...}
%D   ....
%D \stoprecord
%D \stoptyping
%D
%D The interface to such a database is defined as follows:
%D
%D \starttyping
%D \definerecord[class][settings]
%D \setuprecord[class][settings]
%D \definerecordentry[class][name]
%D \stoptyping
%D
%D and processed by
%D
%D \starttyping
%D \processrecords[file list][tag and/or group list]
%D \stoptyping
%D
%D The actual processing is done by a macro assigned to \type
%D {command}:
%D
%D \starttyping
%D \setuprecord[class][command=\DoWithRecord]
%D \stoptyping
%D
%D Given that one can ask for a field with
%D
%D \starttyping
%D \getrecordentry{name}
%D \stoptyping
%D
%D such a command can look like:
%D
%D \starttyping
%D \def\DoWithRecord#1%
%D   {\startpacked
%D    \let\\=\quad
%D    name:  \getrecordentry{name}~\getrecordentry{family name}\par
%D    address: \getrecordentry{postal address}\par
%D    \stoppacked}
%D \stoptyping
%D
%D The argument passed is the tag. The database can look like:
%D
%D \starttyping
%D \startrecord{hagenj}
%D   \memberofgroup{a,b}
%D   \setrecordentry{naam}{Hans}
%D   \setrecordentry{family name}{Hagen}
%D   \setrecordentry{postal address}{J. Hagen\\Ridderstraat 29\\Hasselt NL}
%D \stoprecord
%D
%D \startrecord{ottenaf}
%D   \memberofgroup{a}
%D   \setrecordentry{name}{Ton}
%D   \setrecordentry{family name}{Otten}
%D   \setrecordentry{postal address}{A.F. Otten\\Prinsengracht 17\\Hasselt NL}
%D \stoprecord
%D \stoptyping
%D
%D The definition of this database looks like:
%D
%D \starttyping
%D \definerecord[address][command=\DoWithRecord]
%D
%D \definerecordentry[address][name]
%D \definerecordentry[address][family name]
%D \definerecordentry[address][postal address]
%D \stoptyping
%D
%D The actual processing is now done by (for instance):
%D
%D \starttyping
%D \processrecords[datafile][hagenj]
%D \processrecords[datafile][hagenj,offenaf]
%D \processrecords[datafile][all]
%D \processrecords[datafile][a]
%D \processrecords[datafile][b]
%D \stoptyping
%D
%D Of course one can reassign the command used to handle the
%D records in between.

% \??kt ->
% \??kw ->

\def\??db    {@@db}
\def\c!velden{velden}

%\newevery \everyrecord \EveryRecord

\def\definerecord
  {\dodoubleempty\dodefinerecord}

\def\dodefinerecord[#1][#2]%
  {\getparameters
     [\??db#1]
     [\c!velden=,
      \c!command=\gobbleoneargument,
      #2]}

\def\setuprecord
  {\dodoubleargument\dosetuprecord}

\def\dosetuprecord[#1][#2]%
  {\getparameters[\??db#1][#2]}%

\def\definerecordentry[#1][#2]%
  {\edef\recordentries{\getvalue{\??db#1\c!velden}}%
   \addtocommalist{#2}\recordentries
   \letvalue{\??db#1\c!velden}\recordentries}

%D Watch out: the entries are defined global! While
%D processing a record, no grouping is applied.

\def\getrecordentry   #1{\getvalue      {\??db:#1}}
\def\resetrecordentry #1{\letgvalueempty{\??db:#1}}
\def\assignrecordentry#1{\setgvalue     {\??db:#1}}

\long\def\skiprecord#1\stoprecord
  {\egroup}

\newif\ifrecordok

\newtoks\resetrecordlist

\def\processrecords
  {\dotripleargument\doprocessrecords}

\def\doprocessrecords[#1][#2][#3]%
  {\bgroup
   \ifx\\\undefined\let\\\relax\fi
   \def\docommand##1%
     {\resetrecordentry{##1}%
      \appendtoks\resetrecordentry{##1}\to\resetrecordlist}%
   \processcommacommand[\getvalue{\??db#1\c!velden}]\docommand
   \let\setrecordentry\skiprecord
   \the\resetrecordlist
   \doifelse{#2}\v!all   % 't Is nu eenmaal alles
     \recordoktrue
     {\doifelsenothing{#2} % of niets
        \recordoktrue
        \recordokfalse}%   % zullen we maar zeggen.
   \ifrecordok
     \let\askedrecords\v!all
   \else
     \makerawcommalist[#2]\askedrecords
   \fi
   \def\checkrecord##1%
     {\rawdoifinsetelse{##1}{\askedrecords}{\recordoktrue}{}}%
   \def\presetrecord##1%
     {\let\setrecordentry\assignrecordentry
      \let\memberofgroup\gobbleoneargument
      \the\resetrecordlist
      \def\stoprecord{\dostoprecord{##1}}}%
   \def\memberofgroup##1%
     {\doifsomething{##1}
        {\rawprocesscommalist[##1]\checkrecord}%
      \ifrecordok
        \presetrecord{##1}%
      \else
        \expandafter\skiprecord
      \fi}%
   \def\startrecord##1%
     {\bgroup
      \ifrecordok
        \presetrecord{##1}%
      \else
        \checkrecord{##1}%
        \ifrecordok
          \presetrecord{##1}%
        \fi
      \fi}%
   \def\dostoprecord##1%
     {\relax
      \egroup
     %\the\everyrecord
      \getvalue{\??db#1\c!command}{##1}}%
   \showmessage\m!databases1\askedrecords
   \def\doprocessrecords##1%
     {\readjobfile{##1}
        {\showmessage\m!databases2{(job)}}
        {\readsysfile{##1}
           {\showmessage\m!databases3{(sys)}}
           {\showmessage\m!databases4{}}}}%
   \processcommalist[#3]\doprocessrecords
   \egroup}

%D While writing the original implementation, I did some
%D experiments with \type {%} before each entry and changing
%D the category code of the comment char. Because \TEX\ scans
%D the line anyway |<|this is needed because the end of line
%D character can be non standard|>| this is not faster.
%D
%D Although this mechanism could have been combined with the
%D block moving mechanism, the current implementation is
%D prefered out of speed reasons.

\protect \endinput