aboutsummaryrefslogtreecommitdiff
path: root/src/gnunet/enums/generate.go
blob: 0e028508f4f0f237c7b44936e42ad051ee169d85 (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
132
133
134
// This file is part of gnunet-go, a GNUnet-implementation in Golang.
// Copyright (C) 2019-2022 Bernd Fix  >Y<
//
// gnunet-go is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// gnunet-go 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
// Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: AGPL3.0-or-later

//go:build ignore

package main

import (
	"bufio"
	"flag"
	"fmt"
	"io"
	"log"
	"os"
	"strings"
	"text/template"
)

// Record in the GANA registry (for a given type)
type Record struct {
	Number     string
	Name       string
	Comment    string
	References string
}

// String returns a readable record string
func (rec *Record) String() string {
	return fmt.Sprintf("[%s:%s]\n", rec.Number, rec.Name)
}

// go:generate generator to read recfiles and fill templates (not exactly
// build on recutils but on recfiles).
func main() {
	// handle command-line arguments
	flag.Parse()
	args := flag.Args()
	if len(args) != 3 {
		log.Fatal("not enough arguments")
	}

	// read template
	tpl, err := template.ParseFiles(args[1])
	if err != nil {
		log.Fatal(err)
	}

	// parse recfile
	in, err := os.Open(args[0])
	if err != nil {
		log.Fatal(err)
	}
	defer in.Close()

	rdr := bufio.NewReader(in)
	state := 0
	var recs []*Record
	var rec *Record
	for {
		// read next line from recfile
		buf, _, err := rdr.ReadLine()
		if err != nil {
			if err == io.EOF {
				break
			}
		}
		line := strings.TrimSpace(string(buf))
		log.Printf("[%d] %s\n", state, line)

		// perform state machine:
		switch state {

		// wait for record to start
		case 0:
			if len(line) == 0 || strings.Index("%#", string(line[0])) != -1 {
				continue
			}
			// new record starts here
			rec = new(Record)
			state = 1
			fallthrough

		// read record data
		case 1:
			if len(line) == 0 {
				// record done; add to list
				log.Println("Record: " + rec.String())
				recs = append(recs, rec)
				rec = nil
				state = 0
				continue
			}
			// set attribute
			kv := strings.SplitN(line, ":", 2)
			switch kv[0] {
			case "Number":
				rec.Number = strings.TrimSpace(kv[1])
			case "Name":
				rec.Name = strings.TrimSpace(kv[1])
			case "Comment":
				rec.Comment = strings.TrimSpace(kv[1])
			case "References":
				rec.References = strings.TrimSpace(kv[1])
			}
		}
	}

	// open output file
	out, err := os.Create(args[2])
	if err != nil {
		log.Fatal(err)
	}
	defer out.Close()

	// Exeute template on data
	if err := tpl.Execute(out, recs); err != nil {
		log.Fatal(err)
	}
}