Exemplo n.º 1
0
func Write(fd int, p []byte) (n int, errno int) {
	var mode uint32
	var done uint32
	if isConsole, _ := GetConsoleMode(int32(fd), &mode); UnicodeConsoleOutput && isConsole {
		// TODO: The number of TCHARs to write. If the total size of the
		// specified number of characters exceeds 64 KB, the function fails with ERROR_NOT_ENOUGH_MEMORY.
		buf16 := utf16.Encode([]int(string(p)))
		//for _, c := range buf16 { print(c," ") } ; println()
		if ok, e := WriteConsole(int32(fd), buf16, &done); !ok {
			return 0, e
		}
		// convert length of utf16 characters to number of bytes written
		if done == uint32(len(buf16)) {
			done = uint32(len(p))
		} else {
			done = 0
			for _, rune := range utf16.Decode(buf16[:done]) {
				done += uint32(utf8.RuneLen(rune))
			}
		}
	} else {
		// TODO: This might as well fail with large writes, only Microsoft doesn't say that, see
		// http://code.google.com/p/msysgit/issues/detail?id=409 for example
		if ok, e := syscall.WriteFile(int32(fd), p, &done, nil); !ok {
			return 0, e
		}
	}
	return int(done), 0
}
Exemplo n.º 2
0
func Getwd() (wd string, errno int) {
	b := make([]uint16, 300)
	n, e := GetCurrentDirectory(uint32(len(b)), &b[0])
	if e != 0 {
		return "", e
	}
	return string(utf16.Decode(b[0:n])), 0
}
Exemplo n.º 3
0
func ComputerName() (name string, errno int) {
	var n uint32 = MAX_COMPUTERNAME_LENGTH + 1
	b := make([]uint16, n)
	if ok, e := GetComputerName(&b[0], &n); !ok {
		return "", e
	}
	return string(utf16.Decode(b[0:n])), 0
}
Exemplo n.º 4
0
// UTF16ToString returns the UTF-8 encoding of the UTF-16 sequence s,
// with a terminating NUL removed.
func UTF16ToString(s []uint16) string {
	for i, v := range s {
		if v == 0 {
			s = s[0:i]
			break
		}
	}
	return string(utf16.Decode(s))
}
Exemplo n.º 5
0
func (h *DeviceHandle) GetStringDescriptor(index byte, langid uint16) (string, *UsbError) {
	buf := make([]uint16, 128)

	rlen, err := decodeUsbError(C.libusb_get_string_descriptor(h.handle, C.uint8_t(index), C.uint16_t(langid), (*C.uchar)(unsafe.Pointer(&buf[0])), 256))
	if err != nil {
		return "", err
	}
	return string(utf16.Decode(buf[1 : rlen/2])), nil
}
Exemplo n.º 6
0
func Errstr(errno int) string {
	if errno == EWINDOWS {
		return "not supported by windows"
	}
	b := make([]uint16, 300)
	n, err := FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_ARGUMENT_ARRAY, 0, uint32(errno), 0, b, nil)
	if err != 0 {
		return "error " + str(errno) + " (FormatMessage failed with err=" + str(err) + ")"
	}
	return string(utf16.Decode(b[0 : n-1]))
}
Exemplo n.º 7
0
// Full path of the current executable
func GetExecutableFileName() string {
	kernel32, _ := syscall.LoadLibrary("kernel32.dll")
	defer syscall.FreeLibrary(kernel32)
	b := make([]uint16, syscall.MAX_PATH)
	getModuleFileName, _ := syscall.GetProcAddress(kernel32, "GetModuleFileNameW")
	ret, _, callErr := syscall.Syscall(uintptr(getModuleFileName),
		3, 0,
		uintptr(unsafe.Pointer(&b[0])),
		uintptr(len(b)))
	if callErr != 0 {
		panic(fmt.Sprintf("GetModuleFileNameW : err %d", int(callErr)))
	}
	return string(utf16.Decode(b[:ret]))
}
// TempDir returns the default directory to use for temporary files.
func TempDir() string {
	const pathSep = '\\'
	dirw := make([]uint16, syscall.MAX_PATH)
	n, _ := syscall.GetTempPath(uint32(len(dirw)), &dirw[0])
	if n > uint32(len(dirw)) {
		dirw = make([]uint16, n)
		n, _ = syscall.GetTempPath(uint32(len(dirw)), &dirw[0])
		if n > uint32(len(dirw)) {
			n = 0
		}
	}
	if n > 0 && dirw[n-1] == pathSep {
		n--
	}
	return string(utf16.Decode(dirw[0:n]))
}
// Getenverror retrieves the value of the environment variable named by the key.
// It returns the value and an error, if any.
func Getenverror(key string) (value string, err Error) {
	b := make([]uint16, 100)
	n, e := syscall.GetEnvironmentVariable(syscall.StringToUTF16Ptr(key), &b[0], uint32(len(b)))
	if n == 0 && e == syscall.ERROR_ENVVAR_NOT_FOUND {
		return "", ENOENV
	}
	if n > uint32(len(b)) {
		b = make([]uint16, n)
		n, e = syscall.GetEnvironmentVariable(syscall.StringToUTF16Ptr(key), &b[0], uint32(len(b)))
		if n > uint32(len(b)) {
			n = 0
		}
	}
	if n == 0 {
		return "", NewSyscallError("GetEnvironmentVariable", e)
	}
	return string(utf16.Decode(b[0:n])), nil
}
Exemplo n.º 10
0
func Errstr(errno int) string {
	// deal with special go errors
	e := errno - APPLICATION_ERROR
	if 0 <= e && e < len(errors) {
		return errors[e]
	}
	// ask windows for the remaining errors
	var flags uint32 = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_IGNORE_INSERTS
	b := make([]uint16, 300)
	n, err := FormatMessage(flags, 0, uint32(errno), 0, b, nil)
	if err != 0 {
		return "error " + itoa(errno) + " (FormatMessage failed with err=" + itoa(err) + ")"
	}
	// trim terminating \r and \n
	for ; n > 0 && (b[n-1] == '\n' || b[n-1] == '\r'); n-- {
	}
	return string(utf16.Decode(b[:n]))
}
// Environ returns an array of strings representing the environment,
// in the form "key=value".
func Environ() []string {
	s, e := syscall.GetEnvironmentStrings()
	if e != 0 {
		return nil
	}
	defer syscall.FreeEnvironmentStrings(s)
	r := make([]string, 0, 50) // Empty with room to grow.
	for from, i, p := 0, 0, (*[1 << 24]uint16)(unsafe.Pointer(s)); true; i++ {
		if p[i] == 0 {
			// empty string marks the end
			if i <= from {
				break
			}
			r = append(r, string(utf16.Decode(p[from:i])))
			from = i + 1
		}
	}
	return r
}