Commit c067bb0
Changed files (2)
question.go
@@ -2,19 +2,37 @@
package main
import (
+ "bufio"
"fmt"
+ "log"
+ "os"
+ "strings"
)
type Record struct {
question,
answer,
category string
+ isInitialized bool
}
// parseLine parses a csv line and returns a single record
func parseLine(line string) (Record, error) {
var record Record
- return record, fmt.Errorf("Unable to parse line for %s", line)
+ log.Print(line)
+ if strings.HasPrefix(line, "#") {
+ return record, nil
+ } else if strings.Compare(strings.TrimSpace(line), "") == 0 {
+ return record, nil
+ }
+ tokens := strings.Split(line, ",")
+ if len(tokens) != 3 {
+ return record, fmt.Errorf("Unable to parse line for %s", line)
+ }
+ record.question = strings.TrimSpace(tokens[0])
+ record.answer = strings.TrimSpace(tokens[1])
+ record.category = strings.TrimSpace(tokens[2])
+ return record, nil
}
// loadFile reads the lines of the question directory and returns an array of Records
@@ -22,7 +40,22 @@ func parseLine(line string) (Record, error) {
// question, answer, category
// Lines starting with '#' are ignored
func LoadFile(questionFile string) ([]Record, error) {
-
+ log.Printf("Loading file: %s", questionFile)
+ file, err := os.Open(questionFile)
+ if err != nil {
+ return nil, err
+ }
+ defer file.Close()
+ var records []Record
+ scanner := bufio.NewScanner(file)
+ for scanner.Scan() {
+ if record, err := parseLine(scanner.Text()); err != nil {
+ return nil, err
+ } else if record.question != "" {
+ records = append(records, record)
+ }
+ }
+ log.Printf("Loaded %d questions from %s.", 0, questionFile)
return nil, fmt.Errorf("Unable to finish readLines for %s", questionFile)
}
questioner.go
@@ -100,6 +100,6 @@ func main() {
flag.StringVar(&confFile, "f", "", "define a specific configuration file to read")
flag.Parse()
config := LoadConfiguration(confFile)
- log.Printf("%+v\t%s\n", config, confFile)
+ log.Printf("Configuration: %+v\t%s\n", config, confFile)
Listen(config)
}