Skip to content

Instantly share code, notes, and snippets.

@kylelemons
Forked from lamvak/makedict.go
Created May 11, 2012 00:25
Show Gist options
  • Save kylelemons/2656791 to your computer and use it in GitHub Desktop.
Save kylelemons/2656791 to your computer and use it in GitHub Desktop.
particularly disturbing fragment of my new poliqarp binding (in progress)
func MakeDict(prefix string, strings []string) (dict *Dict, err error) {
image_file_size := 0
dict, err = nil, nil
for _, s := range strings {
// accomodate the string itself, length counter and zero-byte marker
// for the reason of backwards debility
image_file_size += len(s) + 5
}
image_file, err := os.Create(prefix + `.image`)
defer image_file.Close()
if err != nil {
return
}
err = image_file.Truncate(int64(image_file_size))
if err != nil {
return
}
offset_file, err := os.Create(prefix + `.offset`)
defer offset_file.Close()
if err != nil {
return
}
err = offset_file.Truncate(int64(len(strings) * 4))
if err != nil {
return
}
var (
image []byte
raw_offset []byte
offset []uint32
image_tip uint32 = 0
)
image, err = MapFileFW(image_file)
if err != nil {
return
}
raw_offset, err = MapFileFW(offset_file)
if err != nil {
return
}
offset, err = Reslice32(raw_offset)
if err != nil {
return
}
for i, s := range strings {
err = Put32(image, uint64(image_tip), uint32(len(s)+1))
if err != nil {
Unmap(image)
Unmap(raw_offset)
return nil, err
}
image_tip += 4
offset[i] = image_tip
copy(image[image_tip:], s)
image_tip += uint32(len(s) + 1)
image[image_tip-1] = 0
}
SyncMap(image)
SyncMap(raw_offset)
return &Dict{Image: image, Offset: offset}, err //should be nil?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment