Skip to content

Instantly share code, notes, and snippets.

@bsiegert
Created March 14, 2012 11:27
Show Gist options
  • Save bsiegert/2035877 to your computer and use it in GitHub Desktop.
Save bsiegert/2035877 to your computer and use it in GitHub Desktop.
"Native" Glob function for Windows. Very old code (2009 or 2010).
package w32glob
import (
"container/vector"
"os"
"strings"
"syscall"
)
func Glob(patterns []string) ([]string, os.Error) {
var path, name string
var i, errno int
var handle int32
var f syscall.Win32finddata
files := make(vector.StringVector, 0)
for _, p := range patterns {
i = strings.LastIndexFunc(p, func(r int) bool {
return r == '\\' || r == ':' || r == '/'
})
if i != -1 {
path = p[0 : i+1]
} else {
path = ""
}
handle, errno = syscall.FindFirstFile(syscall.StringToUTF16Ptr(p), &f)
if handle == -1 { // INVALID_HANDLE_VALUE
return nil, os.NewError(syscall.Errstr(errno))
}
defer syscall.FindClose(handle)
for errno == 0 {
name = syscall.UTF16ToString(f.FileName[0:])
if name != "." && name != ".." {
files.Push(path + name)
}
_, errno = syscall.FindNextFile(handle, &f)
}
if errno != syscall.ERROR_NO_MORE_FILES {
return nil, os.NewError(syscall.Errstr(errno))
}
}
return files, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment