Skip to content

Commit

Permalink
HW08 is completed. work on mistakes
Browse files Browse the repository at this point in the history
  • Loading branch information
DimVlas committed Jul 23, 2024
1 parent 11aaec9 commit 196c9d1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
Empty file removed hw08_envdir_tool/.sync
Empty file.
43 changes: 23 additions & 20 deletions hw08_envdir_tool/env_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,13 @@ func ReadDir(dir string) (Environment, error) {
continue
}

fName := fmt.Sprintf("%s%v%s", dir, string(os.PathSeparator), de.Name())
f, err := os.Open(fName)
fName := filepath.Join(dir, de.Name())
data, err := readData(fName)
if err != nil {
log.Printf("open file '%s': %s", fName, err)
log.Printf("ReadFile: %s", err)
continue
}

rd := bufio.NewReader(f)
data, flg, err := rd.ReadLine()
if err != nil {
log.Printf("ReadLine: %s", err)
continue
}

var d []byte
for flg {
d, flg, err = rd.ReadLine()
if err != nil {
log.Printf("ReadLine: %s", err)
continue
}
data = append(data, d...)
}

data = bytes.ReplaceAll(data, []byte("\000"), []byte("\n"))

env[de.Name()] = EnvValue{Value: strings.TrimRight(string(data), " \t"), NeedRemove: false}
Expand All @@ -78,6 +61,26 @@ func ReadDir(dir string) (Environment, error) {
return env, nil
}

func readData(fileName string) ([]byte, error) {
f, err := os.Open(fileName)
if err != nil {
return nil, fmt.Errorf("open file '%s': %w", fileName, err)
}
defer f.Close()

scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanLines)

if !scanner.Scan() {
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("scan: %w", err)
}
return make([]byte, 0), nil
}

return scanner.Bytes(), nil
}

func SetEnv(env Environment) error {
for envName, envVal := range env {
if envVal.NeedRemove {
Expand Down

0 comments on commit 196c9d1

Please sign in to comment.