| 1 |
// =========================================================================== |
| 2 |
// |
| 3 |
// PUBLIC DOMAIN NOTICE |
| 4 |
// National Center for Biotechnology Information (NCBI) |
| 5 |
// |
| 6 |
// This software/database is a "United States Government Work" under the |
| 7 |
// terms of the United States Copyright Act. It was written as part of |
| 8 |
// the author's official duties as a United States Government employee and |
| 9 |
// thus cannot be copyrighted. This software/database is freely available |
| 10 |
// to the public for use. The National Library of Medicine and the U.S. |
| 11 |
// Government do not place any restriction on its use or reproduction. |
| 12 |
// We would, however, appreciate having the NCBI and the author cited in |
| 13 |
// any work or product based on this material. |
| 14 |
// |
| 15 |
// Although all reasonable efforts have been taken to ensure the accuracy |
| 16 |
// and reliability of the software and data, the NLM and the U.S. |
| 17 |
// Government do not and cannot warrant the performance or results that |
| 18 |
// may be obtained by using this software or data. The NLM and the U.S. |
| 19 |
// Government disclaim all warranties, express or implied, including |
| 20 |
// warranties of performance, merchantability or fitness for any particular |
| 21 |
// purpose. |
| 22 |
// |
| 23 |
// =========================================================================== |
| 24 |
// |
| 25 |
// File Name: ini.go |
| 26 |
// |
| 27 |
// Author: Jonathan Kans |
| 28 |
// |
| 29 |
// ========================================================================== |
| 30 |
|
| 31 |
package eutils |
| 32 |
|
| 33 |
import ( |
| 34 |
"bufio" |
| 35 |
"fmt" |
| 36 |
"html" |
| 37 |
"io" |
| 38 |
"os" |
| 39 |
"runtime" |
| 40 |
"strings" |
| 41 |
) |
| 42 |
|
| 43 |
// INIConverter parses INI files into XML object stream |
| 44 |
func INIConverter(inp io.Reader) <-chan string { |
| 45 |
|
| 46 |
if inp == nil { |
| 47 |
return nil |
| 48 |
} |
| 49 |
|
| 50 |
out := make(chan string, chanDepth) |
| 51 |
if out == nil { |
| 52 |
DisplayError("Unable to create INI converter channel") |
| 53 |
os.Exit(1) |
| 54 |
} |
| 55 |
|
| 56 |
convertINI := func(inp io.Reader, out chan<- string) { |
| 57 |
|
| 58 |
// close channel when all records have been sent |
| 59 |
defer close(out) |
| 60 |
|
| 61 |
okay := false |
| 62 |
row := 0 |
| 63 |
|
| 64 |
var buffer strings.Builder |
| 65 |
|
| 66 |
scanr := bufio.NewScanner(inp) |
| 67 |
|
| 68 |
buffer.WriteString("<ConfigFile>\n") |
| 69 |
|
| 70 |
var currSect []string |
| 71 |
|
| 72 |
// array to speed up indentation |
| 73 |
indentSpaces := []string{ |
| 74 |
"", |
| 75 |
" ", |
| 76 |
" ", |
| 77 |
" ", |
| 78 |
" ", |
| 79 |
" ", |
| 80 |
" ", |
| 81 |
" ", |
| 82 |
" ", |
| 83 |
" ", |
| 84 |
} |
| 85 |
|
| 86 |
// indent a specified number of spaces |
| 87 |
doIndent := func(indt int) { |
| 88 |
i := indt |
| 89 |
for i > 9 { |
| 90 |
buffer.WriteString(" ") |
| 91 |
i -= 10 |
| 92 |
} |
| 93 |
if i < 0 { |
| 94 |
return |
| 95 |
} |
| 96 |
buffer.WriteString(indentSpaces[i]) |
| 97 |
} |
| 98 |
|
| 99 |
indent := 0 |
| 100 |
|
| 101 |
changeSection := func(sect string, curr []string) ([]string, int) { |
| 102 |
|
| 103 |
splt := strings.Split(sect, ".") |
| 104 |
|
| 105 |
slen, clen := len(splt), len(curr) |
| 106 |
mlen := min(slen, clen) |
| 107 |
|
| 108 |
// find last common subsection |
| 109 |
i := 0 |
| 110 |
for range mlen { |
| 111 |
if splt[i] != curr[i] { |
| 112 |
break |
| 113 |
} |
| 114 |
i++ |
| 115 |
} |
| 116 |
|
| 117 |
// descend down to common point, closing old levels |
| 118 |
for j := clen - 1; j >= i; j-- { |
| 119 |
if curr[j] == "" { |
| 120 |
continue |
| 121 |
} |
| 122 |
doIndent(j) |
| 123 |
buffer.WriteString(" </" + curr[j] + ">\n") |
| 124 |
} |
| 125 |
|
| 126 |
// add new levels from common point |
| 127 |
k := 0 |
| 128 |
for k = i; k < slen; k++ { |
| 129 |
if splt[k] == "" { |
| 130 |
continue |
| 131 |
} |
| 132 |
doIndent(k) |
| 133 |
buffer.WriteString(" <" + splt[k] + ">\n") |
| 134 |
} |
| 135 |
|
| 136 |
return splt, k |
| 137 |
} |
| 138 |
|
| 139 |
for scanr.Scan() { |
| 140 |
|
| 141 |
line := scanr.Text() |
| 142 |
|
| 143 |
row++ |
| 144 |
|
| 145 |
line = strings.TrimSpace(line) |
| 146 |
|
| 147 |
// ignore comment lines |
| 148 |
if line == "" || strings.HasPrefix(line, ";") || strings.HasPrefix(line, "#") { |
| 149 |
continue |
| 150 |
} |
| 151 |
|
| 152 |
if strings.HasPrefix(line, "[") { |
| 153 |
if !strings.HasSuffix(line, "]") { |
| 154 |
fmt.Fprintf(os.Stderr, "Improper Section '%s'\n", line) |
| 155 |
continue |
| 156 |
} |
| 157 |
|
| 158 |
line = strings.TrimPrefix(line, "[") |
| 159 |
line = strings.TrimSuffix(line, "]") |
| 160 |
line = strings.TrimSpace(line) |
| 161 |
|
| 162 |
// check for relative section |
| 163 |
if strings.HasPrefix(line, ".") { |
| 164 |
if len(currSect) < 1 || len(line) < 2 { |
| 165 |
fmt.Fprintf(os.Stderr, "Improper Relative Section '%s'\n", line) |
| 166 |
continue |
| 167 |
} |
| 168 |
line = strings.Join(currSect, ".") + line |
| 169 |
} |
| 170 |
|
| 171 |
currSect, indent = changeSection(line, currSect) |
| 172 |
|
| 173 |
continue |
| 174 |
} |
| 175 |
|
| 176 |
lft, rgt, found := strings.Cut(line, "=") |
| 177 |
if !found || lft == "" || rgt == "" { |
| 178 |
fmt.Fprintf(os.Stderr, "Improper Item '%s'\n", line) |
| 179 |
continue |
| 180 |
} |
| 181 |
lft = strings.TrimSpace(lft) |
| 182 |
rgt = strings.TrimSpace(rgt) |
| 183 |
rgt = strings.TrimPrefix(rgt, "\"") |
| 184 |
rgt = strings.TrimSuffix(rgt, "\"") |
| 185 |
rgt = strings.TrimSpace(rgt) |
| 186 |
rgt = html.EscapeString(rgt) |
| 187 |
|
| 188 |
doIndent(indent) |
| 189 |
buffer.WriteString(" <" + lft + ">" + rgt + "</" + lft + ">\n") |
| 190 |
|
| 191 |
okay = true |
| 192 |
} |
| 193 |
|
| 194 |
currSect, indent = changeSection("", currSect) |
| 195 |
|
| 196 |
buffer.WriteString("</ConfigFile>\n") |
| 197 |
|
| 198 |
if okay { |
| 199 |
txt := buffer.String() |
| 200 |
if txt != "" { |
| 201 |
// send remaining result through output channel |
| 202 |
out <- txt |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
buffer.Reset() |
| 207 |
|
| 208 |
runtime.Gosched() |
| 209 |
} |
| 210 |
|
| 211 |
go convertINI(inp, out) |
| 212 |
|
| 213 |
return out |
| 214 |
} |
| 215 |
|
| 216 |
// INItoXML converts INI to an XML string |
| 217 |
func INItoXML(ini string) string { |
| 218 |
|
| 219 |
return StringToXML(ini, INIConverter) |
| 220 |
} |