Repository navigation
Expand file tree
/
Copy pathcredoc.go
More file actions
382 lines (322 loc) · 7.84 KB
/
Copy pathcredoc.go
File metadata and controls
382 lines (322 loc) · 7.84 KB
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
//
// credoc
// create doc documentation
// usage: credoc file.go
//
// author: prr azul software
// date: 20 May 2022
// copyright prr azul software
//
package main
import (
"os"
"fmt"
"credoc/util"
)
func main() {
numArg := len(os.Args)
switch numArg {
case 0, 1:
fmt.Printf("no input file provided\n")
fmt.Printf("usage is: credoc file\n")
os.Exit(1)
case 2:
fmt.Printf("input file: %s\n", os.Args[1])
default:
fmt.Printf("in correct number of command line parameters: %d\n", numArg)
fmt.Printf("usage is: credoc file\n")
os.Exit(1)
}
err := credocfil(os.Args[1])
if err != nil {
fmt.Printf("error - credocfil: %v\n", err)
os.Exit(1)
}
fmt.Println("*** success ***")
}
func credocfil(inpfilnam string)(err error) {
// function that creates doc output file
var outfilnam string
// check whether input file is valid
inpfil, err := os.Open(os.Args[1])
defer inpfil.Close()
if err != nil {return fmt.Errorf("os.Open: %v\n", err)}
inpfilInfo,_ := inpfil.Stat()
inpSize := inpfilInfo.Size()
fmt.Printf("*** input file: %s Size: %d\n", inpfilnam, inpSize)
// create output file name
// found := false
for i:=0; i< len(inpfilnam); i++ {
if inpfilnam[i] == '.' {
// found = true
outfilnam = string(inpfilnam[:i])
break
}
}
fmt.Printf("*** output file: %s\n", outfilnam + ".md")
outfil, err := os.Create(outfilnam + ".md")
if err != nil { return fmt.Errorf("os.Create: %v\n", err)}
defer outfil.Close()
prefix := "<p style=\"font-size:18pt; text-align:center;\">"
suffix := "</p>\n\n"
outfil.WriteString(prefix + outfilnam + suffix)
bufp := make([]byte, inpSize)
// for iblock :=0; iblock < 10; iblock++ {
nb, _ := inpfil.Read(bufp)
// if err != nil {return fmt.Errorf("read: %d %v", nb, err)}
// top comments
outfil.WriteString("# Description\n")
linSt := 0
ilin := 1
endTop := 0
istate := 1
linEnd := 0
endTopLin := 1
for i:=0; i< nb; i++ {
if bufp[i] != '\n' {continue}
ilin++
switch istate {
case 1:
linEnd = i
// fmt.Printf("line %d: %s\n", ilin, string(bufp[linSt:linEnd]))
desc, res := IsComment(bufp[linSt: linEnd])
if res {
outfil.WriteString(" " + desc + " \n")
istate = 2
}
linSt = i+1
case 2:
linEnd = i
// fmt.Printf("line %d: %s\n", ilin, string(bufp[linSt:linEnd]))
desc, res := IsComment(bufp[linSt: linEnd])
if res {
outfil.WriteString(" " + desc + " \n")
istate = 2
linSt = i+1
endTop = i
endTopLin = ilin
}
}
}
if endTop == 0 {
outfil.WriteString("no description! \n")
}
outfil.WriteString("\n\n")
//type definitions
outfil.WriteString("# Types\n")
istate = 1
linEnd = 0
ilin = endTopLin
linSt = 0
found := false
//fmt.Println("nb: ", nb, " endTop: ", endTop)
for i:=endTop; i< nb; i++ {
if bufp[i] != '\n' {continue}
ilin++
linEnd = i
switch istate {
case 1:
typNam, res := IsTyp(bufp[linSt: linEnd])
if res {
outfil.WriteString("## " + typNam + " \n")
outfil.WriteString(string(bufp[linSt: linEnd-1]) + " \n\n")
istate = 2
found = true
}
linSt = i+1
case 2:
desc, res := IsComment(bufp[linSt: linEnd])
if res {
outfil.WriteString(desc + " \n")
}
linSt = i+1
if !res {istate = 1; break}
}
}
if !found {
outfil.WriteString("no types defined! \n")
}
// function
outfil.WriteString("\n# Functions\n")
istate = 1
linEnd = 0
ilin = endTopLin
linSt = 0
found = false
//fmt.Println("nb: ", nb, " endTop: ", endTop)
for i:=endTop; i< nb; i++ {
if bufp[i] != '\n' {continue}
ilin++
linEnd = i
switch istate {
case 1:
funcNam, res := IsFunc(bufp[linSt: linEnd])
if res {
outfil.WriteString("## " + funcNam + " \n")
outfil.WriteString(string(bufp[linSt: linEnd-1]) + " \n\n")
istate = 2
found = true
}
linSt = i+1
case 2:
desc, res := IsComment(bufp[linSt: linEnd])
if res {
outfil.WriteString(desc + " \n")
}
linSt = i+1
if !res {istate = 1; break}
}
}
if !found {
outfil.WriteString("no functions defined! \n")
}
// method
outfil.WriteString("\n# Methods\n")
istate = 1
linEnd = 0
ilin = endTopLin
linSt = 0
found = false
for i:=endTop; i< nb; i++ {
if bufp[i] != '\n' {continue}
ilin++
linEnd = i
switch istate {
case 1:
methNam, methTyp, res := IsMethod(bufp[linSt: linEnd])
if res {
outfil.WriteString("## " + methTyp +": " + methNam + " \n")
outfil.WriteString(string(bufp[linSt: linEnd-1]) + " \n\n")
istate = 2
found = true
}
linSt = i+1
case 2:
desc, res := IsComment(bufp[linSt: linEnd])
if res {
outfil.WriteString(desc + " \n")
}
linSt = i+1
if !res {istate = 1; break}
}
}
if !found {
outfil.WriteString("no methods defined! \n")
}
return nil
}
func IsTyp(buf []byte)(typNam string, res bool) {
// function that checks whether an input line is a type definition.
// It returns the type name if the input line is a typpe definition.
if len(buf) < 5 { return "", false }
if buf[0] != 't' {return "", false}
if buf[1] != 'y' {return "", false}
if buf[2] != 'p' {return "", false}
if buf[3] != 'e' {return "", false}
if buf[4] != ' ' {return "", false}
fnamSt := 0
for i:= 5; i<len(buf); i++ {
if buf[i] == ' ' {continue}
if utilLib.IsAlpha(buf[i]) {
fnamSt = i
break
}
}
if fnamSt == 0 {return "", false}
fnamEnd := 0
for i:= fnamSt; i<len(buf); i++ {
if (buf[i] == ' ') {
fnamEnd = i
break
}
}
if fnamEnd == 0 {return "", false}
typNam = string(buf[fnamSt:fnamEnd])
return typNam, true
}
func IsFunc(buf []byte)(funcNam string, res bool) {
// function that checks whether an input line is a function.
// It returns the function name if the input line is a function.
if len(buf) < 5 { return "", false }
if buf[0] != 'f' {return "", false}
if buf[1] != 'u' {return "", false}
if buf[2] != 'n' {return "", false}
if buf[3] != 'c' {return "", false}
if buf[4] != ' ' {return "", false}
fnamSt := 0
for i:= 5; i<len(buf); i++ {
if buf[i] == ' ' {continue}
if buf[i] == '(' {return "", false}
if utilLib.IsAlpha(buf[i]) {
fnamSt = i
break
}
}
if fnamSt == 0 {return "", false}
fnamEnd := 0
for i:= fnamSt; i<len(buf); i++ {
if (buf[i] == ' ') || (buf[i] == '(') {
fnamEnd = i
break
}
}
if fnamEnd == 0 {return "", false}
funcNam = string(buf[fnamSt:fnamEnd])
return funcNam, true
}
func IsMethod(buf []byte)(methNam string, typNam string, res bool) {
// function that detemines whether a input line is a method.
// if so, the function returns the method name and the name of the structure the method is associated with
if len(buf) < 5 { return "","", false }
if buf[0] != 'f' {return "", "", false}
if buf[1] != 'u' {return "", "", false}
if buf[2] != 'n' {return "", "", false}
if buf[3] != 'c' {return "", "", false}
if buf[4] != ' ' {return "", "", false}
typSt := 0
for i:= 5; i<len(buf); i++ {
if buf[i] == ' ' {continue}
if utilLib.IsAlpha(buf[i]) {return "", "", false}
if buf[i] == '(' {
typSt = i
break
}
}
if typSt == 0 {return "", "", false}
typEnd := 0
for i:= typSt; i<len(buf); i++ {
if buf[i] == ')' {
typEnd = i
break
}
}
if typEnd == 0 {return "", "", false}
typNam = string(buf[typSt+1:typEnd-1])
methSt := 0
for i:= typEnd + 1; i<len(buf); i++ {
if utilLib.IsAlpha(buf[i]) {
methSt = i
break
}
}
if methSt == 0 {return "", "", false}
methEnd := 0
for i:= methSt; i<len(buf); i++ {
if (buf[i] == ' ') || (buf[i] == '(') {
methEnd = i
break
}
}
if methEnd == 0 {return "", "", false}
methNam = string(buf[methSt:methEnd])
return methNam, typNam, true
}
func IsComment (buf []byte)(desc string, res bool) {
// function that determines whether the input line is a comment line.
// If so, it returns the comment in the desc.
if len(buf) < 2 {return "", false}
if buf[0] != '/' {return "", false}
if buf[1] != '/' {return "", false}
desc = string(buf[2:])
return desc, true
}