aboutsummaryrefslogtreecommitdiff
path: root/main.odin
blob: 7887b67e108e752470d350366f6be1b27bb42631 (plain)
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package main

import "core:fmt"
import "core:os"
import "core:path/filepath"
import "core:sys/windows"
import "core:strings"

/*
TODO: Move info that stays the same throughout the table up to the info header
*/

VERBOSE :: false

PART_ONE :: #load("parts/start.html", string)
PART_TWO :: #load("parts/start2.html", string)
PART_END :: #load("parts/end.html", string)

Device :: enum {
	UNSET,
	ZOOM,
	SOUND_DEVICES,
}

Stages :: enum {
	TITLE,
	INFO,
	HEADER,
	BODY,
}

Info_Line :: struct {
	field : string,
	entry : string,
}

Report :: struct {
	title           : string,
	info_lines      : []Info_Line,
	header          : []string,
	table           : [][]string,
	column_count    : int,
	row_count       : int,
	tc_column_index : int,
}

main :: proc() {
	when ODIN_OS == .Windows {
		windows.SetConsoleOutputCP(windows.CODEPAGE.UTF8)
	}
	
	input_file_name : string
	if len(os.args) < 2 {
		fmt.println("ERROR: No file submitted.")
		return
	} else {
		fmt.printf("Input path: {}\n", os.args[1])
		input_file_name = os.args[1]
	}
	
	
	path_info, error := os.stat(input_file_name)
	
	if error == os.ERROR_NONE {
		
		file_list : [dynamic]string
		file_count := 1
		if(path_info.is_dir) {
			fmt.println("Directory submitted! Walking directory...\n")
			fmt.printf("šŸ“ {}\n", path_info.name)
			walk_directory(path_info.fullpath, &file_count, &file_list, 1)
			fmt.println("")
		} else {
			append(&file_list, strings.clone(path_info.fullpath))
		}
		
		for file, f in file_list {
			
			file_info, _ := os.stat(file)
			fmt.printf("\nšŸ“„ File {}: {}\n", f+1, file_info.name)
			parsed, ok_parse := parse(file_info.fullpath)
			if !ok_parse {
				fmt.printf("Parse failed: {}\n", file_info.fullpath)
				continue
			}
			output_name := fmt.aprintf("{}_Knekt.html", file_info.fullpath[:len(file_info.fullpath)-4], allocator=context.temp_allocator)
			render(parsed, output_name)
			free_all(context.temp_allocator)
		}
	} else {
		fmt.printf("ERROR could not get path info for: {}\n", input_file_name)
	}
}




parse :: proc(path : string, device : Device = .UNSET) -> (Report, bool) {
	device := device
	output : Report = {}
	data, ok := os.read_entire_file(path, context.temp_allocator)
	if !ok {
		fmt.printf("ERROR: Could not read file: {}\n", path)
		return {}, false
	}
	file_info, _ := os.lstat(path, context.temp_allocator)
	
	
	
	// STAGE 1 --------------------------------------------------------------
	// First, we detect what kind of sound report this is
	line_number := 0
	it := string(data)
	for line in strings.split_lines_iterator(&it) {
		if (device!=.UNSET) { break }
		if line == "\"SOUND REPORT\"," {
			device = .ZOOM
			if VERBOSE do fmt.printf("Detected ZOOM from quotes and comma on line index {}\n", line_number)
		}
		if line == "\"ZOOM F8\","      {
			device = .ZOOM
			if VERBOSE do fmt.printf("Detected ZOOM from \"ZOOM F8\" on line index {}\n", line_number)
		}
		if line == "SOUND REPORT"      {
			device = .SOUND_DEVICES
			if VERBOSE do fmt.printf("Detected SOUND_DEVICES from unquoted SOUND REPORT line index {}\n", line_number)
		}
		line_number += 1
	}
	
	if device == .UNSET {
		fmt.printf("ERROR: Unable to detect sound report type!\n")
		return {}, false
	}
	
	
	
	// STAGE 2 --------------------------------------------------------------
	// Measuring content for allocation
	if device == .ZOOM {
		output.column_count = 21 // Ugly magic number, could be fucked by firmware update
		output.info_lines = make([]Info_Line, 2, context.temp_allocator)
		
		output.row_count = strings.count(string(data), "\n") - 7 // Ugly magic number, could be fucked by firmware update
		output.table = make([][]string, output.row_count, context.temp_allocator)
		output.header = make([]string, output.column_count, context.temp_allocator)
		for &row in output.table {
			row = make([]string, output.column_count, context.temp_allocator)
		}
	}
	
	
	
	// STAGE 3 --------------------------------------------------------------
	// Filling with data
	
	if VERBOSE do fmt.printf("Struct before main parse:\n%#v\n", output)
	
	first_channel_index := -1
	last_channel_index  := -1
	
	stage : Stages = .TITLE
	#partial switch device {
		case .SOUND_DEVICES:
			fmt.printf("ERROR parsing {}: Sound Devices reports are not yet supported in this version.\n", path)
			return {}, false
			
			
		case .ZOOM:
			fmt.printf("Parsing [{}] as ZOOM report, ", file_info.name)
			
			// Getting title
			if        file_info.name[:8] == "F8n Pro_" {
				output.title = file_info.name[8:len(file_info.name)-4]
			} else if file_info.name[:4] == "F8n_"     { // I don't own one, so I don't know if this is what the F8n does
				output.title = file_info.name[4:len(file_info.name)-4]
			} else if file_info.name[:3] == "F8_"      { // TODO: Verify this is what the original F8 does
				output.title = file_info.name[4:len(file_info.name)-4]
			}
			fmt.printf("titled \"{}\".\n", output.title)
			
			line_index := 0
			info_line_index := 0
			body_line_index := 0
			it := string(data)
			for line in strings.split_lines_iterator(&it) {
				switch stage {
					case .TITLE:
						if line_index == 1 { // Ugly magic number, could be fucked by firmware update
							stage = .INFO
						}
						
					case .INFO:
						if line == "" {
							stage = .HEADER
							continue
						}
						line_elements := strings.split(line, ",")
						if VERBOSE do fmt.printf(".INFO {}: {}\n", line_index, line_elements)
						field_raw := line_elements[0]
						entry_raw := line_elements[1]
						field := line_elements[0][1:len(field_raw)-1]
						entry := line_elements[1][1:len(entry_raw)-1]
						output.info_lines[info_line_index].field = field
						output.info_lines[info_line_index].entry = entry
						info_line_index += 1
						
					case .HEADER:
						if VERBOSE do fmt.printf(".HEADER {}:", line_index)
						// to skip empty entry after trailing comma we do a silly slice
						for element, e in strings.split(line, ",")[:output.column_count] {
							if VERBOSE do fmt.printf(" {}", element)
							output.header[e] = element[1:len(element)-1]
							
							if element[:4] == "\"Tr " {
								if first_channel_index==-1 do first_channel_index = e
								last_channel_index = e
								output.header[e] = fmt.aprintf("Trk {}", e-first_channel_index+1, allocator=context.temp_allocator)
							}
							if element == "\"Start TC\"" {
								output.tc_column_index = e
							}
						}
						if VERBOSE do fmt.printf("\n")
						stage = .BODY
						
					case .BODY:
						if VERBOSE do fmt.printf("first_channel_index: {}\n", first_channel_index)
						if VERBOSE do fmt.printf("last_channel_index: {}\n", last_channel_index)
						if line == "" do break
						if VERBOSE do fmt.printf(".BODY {}:", line_index)
						// to skip empty entry after trailing comma we do a silly slice
						for element, e in strings.split(line, ",")[:output.column_count] {
							if VERBOSE do fmt.printf(" {}", element)
							output.table[body_line_index][e] = element[1:len(element)-1]
						}
						if VERBOSE do fmt.printf("\n")
						body_line_index += 1
				}
				line_index += 1
			}
	}
	
	
	
	
	// STAGE 4 --------------------------------------------------------------
	// Cleanup!
	if VERBOSE do fmt.printf("Struct before cleanup:\n%#v\n", output)
	
	// Stacking tracks to the left
	for &line, l in output.table {
		stacking_index := first_channel_index
		for &field, f in line[first_channel_index:last_channel_index+1] {
			if field != "" {
				line[stacking_index] = field
				stacking_index += 1
			}
		}
		for &field, f in line[stacking_index:last_channel_index+1] {
			field = ""
		}
	}
	
	// Cleaning out unused columns
	touched := make([]bool, output.column_count, context.temp_allocator)
	for line, l in output.table {
		for field, f in line {
			if touched[f] do continue
			if field != "" {
				touched[f] = true
			}
		}
	}
	for &line, l in output.table {
		stacking_index := 0
		for &field, f in line {
			if touched[f] {
				line[stacking_index] = field
				stacking_index += 1
			}
		}
		for &field, f in line[stacking_index:] {
			field = ""
		}
	}
	stacking_index := 0
	for &field, f in output.header {
		if touched[f] {
			output.header[stacking_index] = field
			stacking_index += 1
		}
	}
	for &field, f in output.header[stacking_index:] {
		field = ""
	}
	new_column_count := 0
	for b in touched {
		if b do new_column_count+=1
	}
	output.column_count = new_column_count
	
	if VERBOSE do fmt.printf("Struct before output:\n%#v\n", output)
	
	return output, true
}


render :: proc(report : Report, path : string) {
	// Now we output the HTML.
	
	builder := strings.builder_make(context.temp_allocator)
	
	strings.write_string(&builder, PART_ONE)
	strings.write_string(&builder, report.title)
	strings.write_string(&builder, " - Lydrapport")
	strings.write_string(&builder, PART_TWO)
	
	
	for line, l in report.info_lines {
		strings.write_string(&builder, "        <p><b>")
		strings.write_string(&builder, line.field)
		strings.write_string(&builder, "</b> ")
		strings.write_string(&builder, line.entry)
		strings.write_string(&builder, "</p>\n")
	}
	strings.write_string(&builder, "      </div>\n    </div>\n    <table>\n      <thead>\n        <tr class=\"header-tr\">\n")
	
	for field, f in report.header[:report.column_count] {
		if f != report.tc_column_index {
			strings.write_string(&builder, "          <th>")
		} else {
			strings.write_string(&builder, "          <th class=\"current-sort\">")
		}
		strings.write_string(&builder, field)
		strings.write_string(&builder, "</th>\n")
	}
	
	strings.write_string(&builder, "        </tr>\n      </thead>\n      <tbody>\n")
	
	for line, l in report.table {
		strings.write_string(&builder, "        <tr>\n")
		for field, f in line[:report.column_count] {
			strings.write_string(&builder, "          <td>")
			strings.write_string(&builder, field)
			strings.write_string(&builder, "</td>\n")
			
		}
		strings.write_string(&builder, "        </tr>\n")
	}
	
	strings.write_string(&builder, PART_END)
	
	output_text := strings.to_string(builder)
	os.write_entire_file(path, transmute([]u8)output_text)
	
	fmt.printf("Output: {}\n", path)
}




indent_by :: proc(i : int) {
	for x in 0..<i {
		fmt.printf("  ")
	}
}

walk_directory :: proc(path : string, file_number : ^int, file_list : ^[dynamic]string, depth : int = 0) {
	handle, ok := os.open(path)
	if ok != os.ERROR_NONE {
		indent_by(depth)
		fmt.printf("ERROR opening dir: %s\n", path)
		return
	}
	defer os.close(handle)
	
	files, okr := os.read_dir(handle, -1)
	if okr != os.ERROR_NONE {
		indent_by(depth)
		fmt.printf("ERROR reading dir: %s\n", path)
		return
	}
	defer delete(files)
	
	
	for file in files {
		
		full_path := file.fullpath
		defer delete(full_path)
		
		if file.is_dir {
			indent_by(depth)
			fmt.printf("šŸ“ %s\n", file.name)
			walk_directory(full_path, file_number, file_list, depth+1) // Recurse
			
		} else { // If file is actually a file
			
			extension := strings.to_lower(filepath.ext(file.name))
			defer delete(extension)
			if(extension == ".csv"){
				indent_by(depth)
				fmt.printf("šŸ“„ %d %s\n", file_number^, file.name)
				append(file_list, strings.clone(file.fullpath))
				file_number^ += 1
			}
		}
	}
}