Skip to content

Commit

Permalink
Merge pull request #98 from Valentin-Kaiser/complexity
Browse files Browse the repository at this point in the history
Decreased complexity of openTable in io_unix
  • Loading branch information
Valentin-Kaiser authored Oct 8, 2023
2 parents 4e40f55 + 1e52da1 commit 954f2f5
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions dbase/io_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,30 +78,41 @@ func (u UnixIO) OpenTable(config *Config) (*File, error) {
if config.ValidateCodePage && file.header.CodePage != file.config.Converter.CodePage() {
return nil, newError("dbase-io-unix-opentable-8", fmt.Errorf("code page mark mismatch: %d != %d", file.header.CodePage, file.config.Converter.CodePage()))
}
// Check if there is an FPT according to the header.
// If there is we will try to open it in the same dir (using the same filename and case).
// If the FPT file does not exist an error is returned.

err = u.openMemo(file, fileName, mode, fileExtension == DBC)
if err != nil {
return nil, newError("dbase-io-unix-opentable-9", err)
}

return file, nil
}

// Check if there is an FPT according to the header.
// If there is we will try to open it in the same dir (using the same filename and case).
// If the FPT file does not exist an error is returned.
func (u UnixIO) openMemo(file *File, filename string, mode int, container bool) error {
if MemoFlag.Defined(file.header.TableFlags) {
ext := FPT
if fileExtension == DBC {
if container {
ext = DCT
}
relatedFile, err := _findFile(strings.TrimSuffix(fileName, path.Ext(fileName)) + string(ext))
relatedFile, err := _findFile(strings.TrimSuffix(filename, path.Ext(filename)) + string(ext))
if err != nil {
return nil, newError("dbase-io-unix-opentable-9", err)
return newError("dbase-io-unix-opentable-9", err)
}
debugf("Opening related file: %s\n", relatedFile)
relatedHandle, err := os.OpenFile(relatedFile, mode, 0600)
if err != nil {
return nil, newError("dbase-io-unix-opentable-10", fmt.Errorf("opening FPT file failed with error: %w", err))
return newError("dbase-io-unix-opentable-10", fmt.Errorf("opening FPT file failed with error: %w", err))
}
file.relatedHandle = relatedHandle
err = file.ReadMemoHeader()
if err != nil {
return nil, newError("dbase-io-unix-opentable-11", err)
return newError("dbase-io-unix-opentable-11", err)
}
}
return file, nil

return nil
}

func (u UnixIO) Close(file *File) error {
Expand Down

0 comments on commit 954f2f5

Please sign in to comment.