コード例 #1
0
ファイル: keyfile.go プロジェクト: sqp/godock
// Get gets a value from the keyfile. Must be used with a pointer to value.
//
func (kf *KeyFile) Get(group string, key string, val interface{}) error {
	switch ptr := val.(type) {
	case *[]bool:
		cast, e := kf.ListBool(group, key)
		*ptr = cast
		return e

	case *[]int:
		cast, e := kf.ListInt(group, key)
		*ptr = cast
		return e

	case *[]float64:
		cast, e := kf.ListFloat(group, key)
		*ptr = cast
		return e

	case *[]string:
		cast, e := kf.ListString(group, key)
		*ptr = cast
		return e
	}

	cGroup := (*C.gchar)(C.CString(group))
	defer C.g_free(C.gpointer(cGroup))
	cKey := (*C.gchar)(C.CString(key))
	defer C.g_free(C.gpointer(cKey))
	var cErr *C.GError

	switch ptr := val.(type) {
	case *bool:
		*ptr = goBool(C.g_key_file_get_boolean(kf.cKey, cGroup, cKey, &cErr))

	case *int:
		*ptr = int(C.g_key_file_get_integer(kf.cKey, cGroup, cKey, &cErr))

	case *float64:
		*ptr = float64(C.g_key_file_get_double(kf.cKey, cGroup, cKey, &cErr))

	case *string:
		cstr := C.g_key_file_get_string(kf.cKey, cGroup, cKey, &cErr)
		*ptr = C.GoString((*C.char)(cstr))
		C.g_free(C.gpointer(cstr))

	}
	return goError(cErr)
}
コード例 #2
0
ファイル: keyfile.go プロジェクト: sqp/godock
// GetOne returns a key value as interface.
//
// valid types are:
//   bool, int, float64, string, comment
//   listbool, listint, listfloat64, liststring,
//
func (kf *KeyFile) GetOne(group string, key string, typ string) (interface{}, error) {
	switch typ {
	case "listbool":
		return kf.ListBool(group, key)

	case "listint":
		return kf.ListInt(group, key)

	case "listfloat64":
		return kf.ListFloat(group, key)

	case "liststring":
		return kf.ListString(group, key)
	}

	cGroup := (*C.gchar)(C.CString(group))
	defer C.g_free(C.gpointer(cGroup))
	cKey := (*C.gchar)(C.CString(key))
	defer C.g_free(C.gpointer(cKey))
	var cErr *C.GError

	var c interface{}
	switch typ {
	case "bool":
		c = goBool(C.g_key_file_get_boolean(kf.cKey, cGroup, cKey, &cErr))

	case "int":
		c = int(C.g_key_file_get_integer(kf.cKey, cGroup, cKey, &cErr))

	case "float64":
		c = float64(C.g_key_file_get_double(kf.cKey, cGroup, cKey, &cErr))

	case "comment":
		cstr := C.g_key_file_get_comment(kf.cKey, cGroup, cKey, &cErr)
		c = C.GoString((*C.char)(cstr))
		C.g_free(C.gpointer(cstr))

	case "string":
		cstr := C.g_key_file_get_string(kf.cKey, cGroup, cKey, &cErr)
		c = C.GoString((*C.char)(cstr))
		C.g_free(C.gpointer(cstr))

	}
	return c, goError(cErr)
}