File: enum_c-source.tmpl

package info (click to toggle)
easygen 5.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 848 kB
  • sloc: sh: 14; makefile: 13
file content (35 lines) | stat: -rw-r--r-- 945 bytes parent folder | download | duplicates (3)
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
{{/*
   Randomly pick the first element if no EnumDefault is set.
*/}}
{{ with $DEFAULT := or (.EnumDefault) (index .Values 0 "Name") -}}
/** Try to extract an enum {{$.EnumName}} value from @str; fall back to "{{$DEFAULT}}". */
const enum {{$.EnumName}}
str_to_{{$.EnumName}}(const char const *str)
{
{{- range $i, $val := $.Values }}
	{{ if gt $i 0 -}}} else {{end -}}if (strcasecmp(str, "{{ if .String }}{{.String}}{{else}}{{.Name}}{{end}}") == 0) {
		return {{ .Name }};
{{- range .AltString }}
	} else if (strcasecmp(str, "{{.}}") == 0) {
		return {{ $val.Name }};
{{- end }}
{{- if eq (len $.Values|minus1) ($i)}}{{/* Sentinel */}}
	}
{{- end -}}
{{- end }}
	return {{$DEFAULT}};
}
{{- end }}

/** Stringer function for {{.EnumName}}. */
const char *
{{.EnumName}}_to_str(const enum {{.EnumName}} val)
{
	switch (val) {
{{- range .Values }}
	case {{.Name}}:
		return "{{ if .String }}{{.String}}{{else}}{{.Name}}{{end}}";
{{- end }}
	}
}