summaryrefslogtreecommitdiff
path: root/src/NCollection/templates2macros.sh
blob: 66c31f54b13995007f2950487613dfc155d7c276 (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
229
230
231
232
233
234
235
236
237
#!/bin/sh

FILES='
NCollection_Array1.hxx
NCollection_Array2.hxx
NCollection_List.hxx
NCollection_Map.hxx
NCollection_DataMap.hxx
NCollection_DoubleMap.hxx
NCollection_IndexedMap.hxx
NCollection_IndexedDataMap.hxx
NCollection_Queue.hxx
NCollection_SList.hxx
NCollection_Sequence.hxx
NCollection_Set.hxx
NCollection_Stack.hxx
NCollection_Vector.hxx
NCollection_BaseCollection.hxx
NCollection_TListIterator.hxx
NCollection_TListNode.hxx
NCollection_HArray1.hxx
NCollection_HArray2.hxx
NCollection_HSequence.hxx
NCollection_HSet.hxx'

#FILES=NCollection_Array2.hxx

for IN_FILE in $FILES; do
### for IN_FILE in $*; do
OUT_FILE=`echo $IN_FILE | sed 's/_/_Define/'` 
gawk '

# ------ Function: Print the backslash in the 79th position
    function PrintBSL(str) {
        str0 = "                                                                                "
        len = length (str)
        if (len > 78) $0 = str " \\"
        else $0 = str substr(str0, 1, 79-len) "\\"
    }
# -------------- Check and corect the file header -----------
/\/\/ *Copyright *:/ {
    print "//            Automatically created from " in_file " by GAWK"
    }
/\/\/ *File *:/ ||
/#(ifndef|define) *NCollection_/ {
    gsub ("NCollection_","NCollection_Define")
    }

# --------------- Modify the include statement for used templates ------
/^ *\#include <NCollection_BaseCollection.hxx>/ {
        print "\#include <NCollection_DefineBaseCollection.hxx>"
        next
    }
/^ *\#include <NCollection_T[A-Za-z]*\.hxx>/ {
    gsub ("NCollection_T","NCollection_DefineT")
    }
/^ *\#include <NCollection_Array[12]\.hxx>/ {
    gsub ("NCollection_Array","NCollection_DefineArray")
    }
/^ *\#include <NCollection_Sequence\.hxx>/ {
    gsub ("NCollection_Sequence","NCollection_DefineSequence")
    }
/^ *\#include <NCollection_Set\.hxx>/ {
    gsub ("NCollection_Set","NCollection_DefineSet")
    }

# -------------- Replace line #define NCOLLECTION_H... -----------------
/^ *#define NCOLLECTION_H.*\(.*\).*$/ {
      defh = $0
      do getline defcl; while (defcl ~ /^ *\\/)
      coll_type = gensub ("^.*public *NCollection_\([A-Za-z0-9_]*\) *<Type>.*$",
			  "\\1", 1, defcl)
      gsub (", *Type\\)",", _" coll_type "Type_\)", defh)
      gsub (" *\\\\$","", defh)
      PrintBSL(gensub ("NCOLLECTION_","DEFINE_", 1, defh))
      print
      PrintBSL("")
      print
      gsub ("NCollection_" coll_type " *<Type>", "_" coll_type "Type_", defcl)
      PrintBSL(gensub ("^\(.*\) *\\\\$","\\1", 1, defcl))
      print
      flag = 10
      next
    }

# -------------- Replace the line "\#define ...." for "template ...." --
/^ *template *< *class/,/^ *{ *$/ {
    if (flag == 0) {
        type = gensub ("^ *template.*NCollection_\([A-Za-z0-9]*\).*$","\\1","g")
        item_t = gensub("^ *template *< *class *\([A-Za-z0-9_]*\) *\(,|>\).*$",\
                         "\\1",1)
        item_tt= gensub("^ *template.*, *class *\([A-Za-z0-9_]*\) *\(,|>\).*$",\
                         "\\1",1)
        if (item_tt == $0) item_tt = ""
        if (type == "BaseCollection")
            PrintBSL("#define DEFINE_" toupper(type) "(_ClassName_, "item_t")")
        else if (item_tt == "")
            PrintBSL("#define DEFINE_" toupper(type) \
                     "(_ClassName_, _BaseCollection_, " item_t ")")
        else
            PrintBSL("#define DEFINE_" toupper(type) \
                     "(_ClassName_, _BaseCollection_, " item_t ", " item_tt ")")
        # Special addition for the Iterator
        if (type == "TListIterator")
        {
          print
          PrintBSL("DEFINE_TLISTNODE(TListNode, _BaseCollection_, " item_t ")")
        }
        flag = 1
    } else if (flag == 1) {
        gsub ("^ *", "class _ClassName_ ") 
        flag = 2
    }
    }

# --------------- Detect the final line of the template class definition ----
/^}; *$/{ flag = 0 }

# --------------- Replace C-style comments for C++-style ones ---------------
/\/\//  { if (flag == 2) $0=gensub ("// *\(.*\) *$","/\* \\1 \*/", "g") }

# --------------- Replace "Raise_if.." for "#ifdef DEB ..::Raise.. #endif" --
/^\# *ifdef *DEB/ {
    nline = 0
    if (flag == 0) {
	while (1) {
	    getline debl
	    if (debl ~ /^\# *endif/) break;
	    print debl
	}
    } else {
        getline debl;
        if (debl !~ /^ *if *\(/) {
	    print "ERROR: in " in_file " cannot find if after \#ifdef DEB" \
		> "/dev/stderr"
	    exit 1
	}
	if (debl ~ /^ *if *\(.*\) *$/)
	    condl = gensub ("^ *if *\\( *(.*) *\\) *$", "\\1", 1, debl)
	else
	    condl = gensub ("^ *if *\\( *(.*) *$", "\\1", 1, debl)
	while(1) {
	    getline debl;
	    if (debl ~ /^\# *endif/) break;
	    if (debl !~ /Standard.*::Raise/)
		condl = gensub("^ *(.*[^\\)]) *($|\\) *$)", condl "\\1", 1, debl)
	    else {
		PrintBSL(gensub ("^  \(.*\):: *Raise *\\(.*$",
			         "\\1_Raise_if", 1, debl))
		print
		PrintBSL(gensub ("^  ( *)Standard.*Raise *\\( *\"NCollection_" \
				    type "(.*)\" *\\) *\; *$",
			         "\\1  (" condl ", #_ClassName_ \"\\2\")\;",
				 1, debl))
		print
	    }
	}
    }
    next
}

# --------------- Remove #pragma warning (default:4291) from the output -----
/^\# *ifdef WNT/ {
    defh = $0
    has_printed = 0
    while (1) {
	getline defcl
	if (defcl ~ /^[ \t]*$/) continue;
	if (defcl ~ /^\# *pragma +warning *\(default *: *4291 *\)/) continue;
	if (defcl ~ /^\# *endif/) {
	    if (has_printed) print defcl
	    break
	}
	if (has_printed == 0) {
	    has_printed = 1
	    print defh
	}
	print defcl
    }
    next
}

# --------------- Replace "DEFINE_T.*..." for typedef ... -------------------
#                 in classes List, Queue, Set, Stack
/^ *typedef NCollection_T[A-Za-z]*<TheItemType>/ {
      if (flag > 1) {
        tsup = gensub("^ *typedef.*NCollection_\([A-Za-z0-9]*\).*$","\\1",1)
        tdef = gensub("^ *typedef.*<[A-Za-z0-9]*> *([A-Za-z0-9]*)\;.*$","\\1",1)
        gsub ("typedef.*$", "DEFINE_" toupper(tsup) "(" tdef \
              ",_BaseCollection_," item_t ")")
      }
    }

# --------------- Replace public T* with DEFINE... --------------------------
/^ *class.*: *public *NCollection_T[A-Za-z]*<.*>/ {
      if (flag > 1) {
        ttempl = gensub("^ *class.*: *public *(NCollection_T[A-Za-z0-9]*<.*>).*$","\\1",1)
        tshort = gensub("^ *class.*: *public *NCollection_(T[A-Za-z0-9]*)<.*$","\\1",1)
        spub = gensub("(^ *class.*: *public *)NCollection_(T[A-Za-z0-9]*)<.*($)",
                      "\\1\\2\\3",1)
        tpara = gensub("^.*<(.*)>","\\1",1)
        PrintBSL("  DEFINE_" toupper(tshort) "(" tshort ",_BaseCollection_," tpara ")")
        print
        PrintBSL(spub)
        print
        next
      }
    }

# ------- General case: replace the macro parameter for class name and print -
        { if (flag == 10) {
	    nsub = gsub ("NCollection_" coll_type " *<Type>",
		         "_" coll_type "Type_")
	    if (nsub) {
		gsub (" *\\\\$","")
		PrintBSL($0)
	    }
	  } else if (flag > 1) {
            gsub ("NCollection_BaseCollection *< *\("item_t"|"item_tt"\) *>", \
                  "_BaseCollection_")
            gsub ("NCollection_TListNode *< *\("item_t"|"item_tt"\) *>", \
                  "TListNode")
            gsub ("NCollection_TListIterator *< *\("item_t"|"item_tt"\) *>", \
                  "TListIterator")
            gsub (ttempl, tshort)
	    gsub (":: *Raise *\\( *\"NCollection_" type,
		  "::Raise (#_ClassName_ \"")
            PrintBSL(gensub ("NCollection_" type \
                             "\(<"item_t">|<"item_tt">|\)\(\[^A-Za-z\]|$\)", \
                             "_ClassName_\\2","g"))
        }
        print
        }

' in_file=$IN_FILE out_file=$OUT_FILE $IN_FILE > $OUT_FILE
echo "\t$IN_FILE converted to $OUT_FILE"
done