Skip to content

Commit ae49dd8

Browse files
committed
perf: read entire file and parse
1 parent fd008cd commit ae49dd8

File tree

1 file changed

+31
-39
lines changed

1 file changed

+31
-39
lines changed

src/mConfigFromFile.axs

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ MODULE_NAME='mConfigFromFile' (
55
(***********************************************************)
66
#DEFINE USING_NAV_MODULE_BASE_CALLBACKS
77
#DEFINE USING_NAV_MODULE_BASE_PROPERTY_EVENT_CALLBACK
8+
#include 'NAVFoundation.Core.axi'
89
#include 'NAVFoundation.ModuleBase.axi'
910
#include 'NAVFoundation.StringUtils.axi'
10-
#include 'NAVFoundation.ErrorLogUtils.axi'
1111
#include 'NAVFoundation.FileUtils.axi'
1212

1313
/*
@@ -51,6 +51,8 @@ DEFINE_DEVICE
5151
(***********************************************************)
5252
DEFINE_CONSTANT
5353

54+
constant integer MAX_FILE_SIZE = 2048
55+
5456
constant char EOF[] = '[END_OF_FILE]'
5557

5658
(***********************************************************)
@@ -82,67 +84,52 @@ DEFINE_MUTUALLY_EXCLUSIVE
8284
(* EXAMPLE: DEFINE_FUNCTION <RETURN_TYPE> <NAME> (<PARAMETERS>) *)
8385
(* EXAMPLE: DEFINE_CALL '<NAME>' (<PARAMETERS>) *)
8486

85-
define_function Read(char path[]) {
86-
stack_var long handle
87-
stack_var char buffer[NAV_MAX_BUFFER]
88-
stack_var integer line
87+
define_function GetConfig(char path[]) {
88+
stack_var char data[MAX_FILE_SIZE]
89+
stack_var char lines[1][255]
8990
stack_var slong result
90-
stack_var char data[255]
9191
stack_var long total
92+
stack_var integer x
9293

9394
if (!length_array(path)) {
9495
return
9596
}
9697

97-
result = NAVFileOpen(path, 'r')
98+
result = NAVFileRead(path, data)
9899

99100
if (result <= 0) {
100101
return
101102
}
102103

103-
handle = type_cast(result)
104+
total = type_cast(result)
105+
106+
NAVLog("'mConfigFromFile => Total Bytes Read: ', itoa(total)")
104107

105-
line = 0
106-
total = 0
107-
result = 1
108+
NAVSplitString(data, "NAV_LF", lines)
108109

109-
while (result > 0) {
110-
result = NAVFileReadLine(handle, buffer)
110+
for (x = 1; x <= length_array(lines); x++) {
111+
NAVLog("'mConfigFromFile => Line: ', lines[x]")
111112

112-
if (result <= 0) {
113+
if (NAVContains(lines[x], EOF)) {
114+
NAVLog("'mConfigFromFile => EOF Found'")
113115
break
114116
}
115117

116-
total = total + type_cast(result)
118+
{
119+
stack_var char value[255]
117120

118-
NAVErrorLog(NAV_LOG_LEVEL_DEBUG,
119-
"'mConfigFromFile => Line: ', buffer")
121+
value = NAVGetStringBetween(lines[x], '////', '////')
120122

121-
if (NAVContains(buffer, EOF)) {
122-
NAVErrorLog(NAV_LOG_LEVEL_DEBUG,
123-
"'mConfigFromFile => Found EOF'")
124-
break
125-
}
123+
if (!length_array(value) || !NAVContains(lines[x], '////////')) {
124+
continue
125+
}
126126

127-
data = NAVGetStringBetween(buffer, '////', '////')
127+
NAVLog("'mConfigFromFile => Line Value: ', value")
128128

129-
if (!length_array(data) && !NAVContains(buffer, '////////')) {
130-
NAVErrorLog(NAV_LOG_LEVEL_DEBUG,
131-
"'mConfigFromFile => Empty Line'")
132-
continue
129+
send_string vdvObject, "'LINE-', itoa(x), ',', value"
133130
}
134-
135-
NAVErrorLog(NAV_LOG_LEVEL_DEBUG,
136-
"'mConfigFromFile => Line Value: ', data")
137-
138-
line++
139-
send_string vdvObject, "'LINE-', itoa(line), ',', data"
140131
}
141132

142-
NAVFileClose(handle)
143-
NAVErrorLog(NAV_LOG_LEVEL_DEBUG,
144-
"'mConfigFromFile => Total Bytes Read: ', itoa(total)")
145-
146133
send_string vdvObject, "'DONE'"
147134
}
148135

@@ -181,8 +168,13 @@ data_event[vdvObject] {
181168
NAVParseSnapiMessage(data.text, message)
182169

183170
switch (message.Header) {
184-
case 'GET_TEXT': {
185-
Read(path)
171+
case 'GET_TEXT':
172+
case 'GET_CONFIG': {
173+
if (length_array(message.Parameter[1])) {
174+
path = NAVTrimString(message.Parameter[1])
175+
}
176+
177+
GetConfig(path)
186178
}
187179
}
188180
}

0 commit comments

Comments
 (0)