Example #1
0
// HasKey is a wrapper around g_key_file_has_key().
func (kf *KeyFile) HasKey(group string, key string) bool {
	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))
	return goBool(C.g_key_file_has_key(kf.cKey, cGroup, cKey, nil))
}
Example #2
0
func GetInfoByName(namespace, symbol string) *GiInfo {
	_namespace := GlibString(namespace)
	defer C.g_free((C.gpointer)(_namespace))
	_symbol := GlibString(symbol)
	defer C.g_free((C.gpointer)(_symbol))
	ptr := (*C.GIBaseInfo)(C.g_irepository_find_by_name(nil, _namespace, _symbol))
	if ptr == nil {
		return nil
	}
	return NewGiInfo(ptr)
}
Example #3
0
// GetKeys is a wrapper around g_key_file_get_keys().
func (kf *KeyFile) GetKeys(group string) (uint64, []string, error) {
	cGroup := (*C.gchar)(C.CString(group))
	defer C.g_free(C.gpointer(cGroup))
	var length C.gsize
	var cErr *C.GError
	c := C.g_key_file_get_keys(kf.cKey, cGroup, &length, &cErr)
	list := make([]string, uint64(length))
	for i := range list {
		list[i] = C.GoString((*(*[999999]*C.char)(unsafe.Pointer(c)))[i])
		C.g_free(C.gpointer((*(*[999999]*C.char)(unsafe.Pointer(c)))[i]))
	}
	return uint64(length), list, goError(cErr)
}
Example #4
0
func LoadNamespace(namespace string) bool {
	_namespace := GlibString(namespace)
	defer C.g_free((C.gpointer)(_namespace))
	success := GoBool(C.load_namespace(_namespace))
	if success {
		cExports = make(map[string]bool)
		cNamespace = namespace

		prefixes = make(map[string]string)
		blacklist = make(map[string]bool)
		content, err := ioutil.ReadFile(filepath.Join("blacklist", namespace))
		if err != nil {
			println("error reading blacklist:", err.Error())
		} else {
			lines := strings.Split(string(content), "\n")
			for _, line := range lines {
				line = strings.TrimSpace(line)
				if len(line) == 0 || strings.HasPrefix(line, "#") {
					continue
				}

				blacklist[line] = true
			}
		}
	}
	return success
}
Example #5
0
// Return input with pattern replaced by sub.
func reSub(pattern []byte, sub []byte, input []byte) []byte {
	var err *C.GError = nil
	re := C.g_regex_new(
		(*C.gchar)(data(append(pattern, 0))),
		C.G_REGEX_CASELESS|
			C.G_REGEX_RAW|
			C.G_REGEX_NO_AUTO_CAPTURE|
			C.G_REGEX_OPTIMIZE,
		0,
		&err)
	if err != nil {
		panic("g_regex_new")
	}
	defer C.g_regex_unref(re)
	subd := C.g_regex_replace_literal(re, (*C.gchar)(data(input)),
		C.gssize(len(input)), 0, (*C.gchar)(data(sub)), 0, &err)
	if err != nil {
		panic("g_regex_replace_literal")
	}
	defer C.g_free(C.gpointer(subd))
	l := C.strlen((*C.char)(subd))
	rv := make([]byte, l)
	C.memcpy(data(rv), unsafe.Pointer(subd), l)
	return rv
}
Example #6
0
// ToData is a wrapper around g_key_file_to_data().
func (kf *KeyFile) ToData() (uint64, string, error) {
	var clength C.gsize
	var cerr *C.GError
	c := C.g_key_file_to_data(kf.cKey, &clength, &cerr)
	defer C.g_free(C.gpointer(c))
	return uint64(clength), C.GoString((*C.char)(c)), goError(cerr)
}
Example #7
0
// LoadFromFile is a wrapper around g_key_file_load_from_file().
func (kf *KeyFile) LoadFromFile(file string, flags Flags) (bool, error) {
	var cstr = C.CString(file)
	defer C.g_free(C.gpointer(cstr))
	var cerr *C.GError
	c := C.g_key_file_load_from_file(kf.cKey, (*C.gchar)(cstr), C.GKeyFileFlags(flags), &cerr)
	return c != 0, goError(cerr)
}
Example #8
0
// 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)
}
Example #9
0
// MT safe.
func (o *GstObj) GetName() string {
	s := C.gst_object_get_name(o.g())
	if s == nil {
		return ""
	}
	defer C.g_free(C.gpointer(s))
	return C.GoString((*C.char)(s))
}
Example #10
0
// 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)
}
Example #11
0
// ListString is a wrapper around g_key_file_get_string_list().
func (kf *KeyFile) ListString(group string, key string) ([]string, error) {
	length, c, e := kf.getList(group, key, "string")
	defer C.g_free(c)
	list := make([]string, length)
	for i := range list {
		list[i] = C.GoString((*(*[999999]*C.char)(c))[i])
	}
	return list, e
}
Example #12
0
// ListInt is a wrapper around g_key_file_get_integer_list().
func (kf *KeyFile) ListInt(group string, key string) ([]int, error) {
	length, c, e := kf.getList(group, key, "int")
	defer C.g_free(c)
	list := make([]int, length)
	for i := range list {
		list[i] = int((*(*[999999]C.int)(c))[i])
	}
	return list, e
}
Example #13
0
// ListFloat is a wrapper around g_key_file_get_double_list().
func (kf *KeyFile) ListFloat(group string, key string) ([]float64, error) {
	// var c C.gpointer
	length, c, e := kf.getList(group, key, "float64")
	defer C.g_free(c)
	list := make([]float64, length)
	for i := range list {
		list[i] = float64((*(*[999999]C.double)(c))[i])
	}
	return list, e
}
Example #14
0
// ListBool is a wrapper around g_key_file_get_boolean_list().
func (kf *KeyFile) ListBool(group string, key string) ([]bool, error) {
	// var c C.gpointer
	length, c, e := kf.getList(group, key, "bool")
	defer C.g_free(c)
	list := make([]bool, length)
	for i := range list {
		list[i] = (*(*[999999]C.int)(c))[i] != 0
	}
	return list, e
}
Example #15
0
// ParseWarning() is a wrapper around gst_message_parse_warning().
func (v *Message) ParseWarning() (err error, debug string) {
	var e *C.GError
	var d *C.gchar
	C.gst_message_parse_warning(v.native(), &e, &d)
	eMessage := C.GoString((*C.char)(e.message))
	defer C.g_error_free(e)
	debug = C.GoString((*C.char)(d))
	defer C.g_free((C.gpointer)(d))
	return errors.New(eMessage), debug
}
Example #16
0
File: GFile.go Project: gotk3/gio
//Loads the content of the file into memory. The data is always zero-terminated, but this is not included in the resultant length . The returned content should be freed with g_free() when no longer needed.
//If cancellable is not NULL, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error G_IO_ERROR_CANCELLED will be returned.
func (v *File) LoadContents(cancellable *Cancellable) (text string, ok bool) {
	contents := new(*C.char)
	length := new(C.gsize)
	c := C.g_file_load_contents(v.native(), cancellable.native(), contents, length, nil, nil)

	ok = gobool(c)
	text = C.GoStringN((*C.char)(*contents), (C.int)(*length))
	C.g_free((C.gpointer)(contents))
	return
}
Example #17
0
//Opens the given files.
//In essence, this results in the “open” signal being emitted in the primary instance.
//n_files must be greater than zero.
//hint is simply passed through to the ::open signal. It is intended to be used by applications that have multiple modes for opening files (eg: "view" vs "edit", etc). Unless you have a need for this functionality, you should use "".
//The application must be registered before calling this function and it must have the G_APPLICATION_HANDLES_OPEN flag set.
func (v *Application) Open(files []*File, hint string) {
	gfiles := C.alloc_files(C.int(len(files)))
	for n, val := range files {
		C.set_file(gfiles, C.int(n), val.native())
	}
	defer C.g_free(C.gpointer(gfiles))
	cstr_hint := C.CString(hint)
	defer C.free(unsafe.Pointer(cstr_hint))
	C.g_application_open(v.native(), gfiles, C.gint(len(files)), (*C.gchar)(cstr_hint))
}
Example #18
0
func allocateContentEncoder() *contentEncoding {
	ptr := C.alloc_gmime_encoding()
	obj := &contentEncoding{
		Pointer: ptr,
	}
	runtime.SetFinalizer(obj, func(o *contentEncoding) {
		C.g_free((C.gpointer)(unsafe.Pointer(o.Pointer)))
	})
	return obj
}
Example #19
0
func GetInfos(namespace string) []*GiInfo {
	_namespace := GlibString(namespace)
	defer C.g_free((C.gpointer)(_namespace))
	raw_list := GListToGo(C.get_infos(_namespace))
	results := make([]*GiInfo, raw_list.Len())
	for i, e := 0, raw_list.Front(); e != nil; i, e = i+1, e.Next() {
		ptr := (*C.GIBaseInfo)(e.Value.(C.gpointer))
		results[i] = NewGiInfo(ptr)
	}
	return results
}
Example #20
0
File: vips.go Project: greut/bimg
func vipsSave(image *C.VipsImage, o vipsSaveOptions) ([]byte, error) {
	defer C.g_object_unref(C.gpointer(image))

	tmpImage, err := vipsPreSave(image, &o)
	if err != nil {
		return nil, err
	}

	// When an image has an unsupported color space, vipsPreSave
	// returns the pointer of the image passed to it unmodified.
	// When this occurs, we must take care to not dereference the
	// original image a second time; we may otherwise erroneously
	// free the object twice.
	if tmpImage != image {
		defer C.g_object_unref(C.gpointer(tmpImage))
	}

	length := C.size_t(0)
	saveErr := C.int(0)
	interlace := C.int(boolToInt(o.Interlace))
	quality := C.int(o.Quality)

	var ptr unsafe.Pointer
	switch o.Type {
	case WEBP:
		saveErr = C.vips_webpsave_bridge(tmpImage, &ptr, &length, 1, quality)
		break
	case PNG:
		saveErr = C.vips_pngsave_bridge(tmpImage, &ptr, &length, 1, C.int(o.Compression), quality, interlace)
		break
	case GIF:
		return nil, errors.New("VIPS cannot save to GIF")
	case PDF:
		return nil, errors.New("VIPS cannot save to PDF")
	case SVG:
		return nil, errors.New("VIPS cannot save to SVG")
	default:
		saveErr = C.vips_jpegsave_bridge(tmpImage, &ptr, &length, 1, quality, interlace)
		break
	}

	if int(saveErr) != 0 {
		return nil, catchVipsError()
	}

	buf := C.GoBytes(ptr, C.int(length))

	// Clean up
	C.g_free(C.gpointer(ptr))
	C.vips_error_clear()

	return buf, nil
}
Example #21
0
func (t *textbox) Text() string {
	var start, end C.GtkTextIter

	buf := C.gtk_text_view_get_buffer(t.textview)
	C.gtk_text_buffer_get_bounds(buf, &start, &end)
	// include hidden chars even though there can't be one since Textbox is explicitly unformatted just to be safe
	// don't worry about embedded pixbufs or widgets; those aren't allowed either
	ctext := C.gtk_text_buffer_get_text(buf, &start, &end, C.TRUE)
	// not explicitly documented: have to manually free this (thanks ste in irc.gimp.net/#gtk+)
	defer C.g_free(C.gpointer(unsafe.Pointer(ctext)))
	return fromgstr(ctext)
}
Example #22
0
func (kf *KeyFile) getList(group, key, typ string) (uint64, C.gpointer, error) {
	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 length C.gsize
	var cErr *C.GError
	var c C.gpointer
	switch typ {
	case "bool":
		c = C.gpointer(C.g_key_file_get_boolean_list(kf.cKey, cGroup, cKey, &length, &cErr))
	case "int":
		c = C.gpointer(C.g_key_file_get_integer_list(kf.cKey, cGroup, cKey, &length, &cErr))
	case "float64":
		c = C.gpointer(C.g_key_file_get_double_list(kf.cKey, cGroup, cKey, &length, &cErr))
	case "string":
		c = C.gpointer(C.g_key_file_get_string_list(kf.cKey, cGroup, cKey, &length, &cErr))

	}
	return uint64(length), c, goError(cErr)
}
Example #23
0
// saveError is a convenience wrapper around vipsError() for funcs that
// call a vips function passing in an output C buffer and length and returning
// ([]byte, error).
func saveError(ptr unsafe.Pointer, length C.size_t, e C.int) ([]byte, error) {
	err := vipsError(e)

	if ptr == nil {
		return nil, err
	}

	buf := C.GoBytes(ptr, C.int(length))
	C.g_free(C.gpointer(ptr))

	return buf, err
}
Example #24
0
// Set is a generic wrapper around g_key_file_set_xxx() with type assertion.
func (kf *KeyFile) Set(group string, key string, uncasted interface{}) error {
	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))

	switch value := uncasted.(type) {
	case bool:
		C.g_key_file_set_boolean(kf.cKey, cGroup, cKey, cBool(value))

	case int:
		C.g_key_file_set_integer(kf.cKey, cGroup, cKey, C.gint(value))

	case float64:
		C.g_key_file_set_double(kf.cKey, cGroup, cKey, C.gdouble(value))

	case string:
		cstr := (*C.gchar)(C.CString(value))
		defer C.g_free(C.gpointer(cstr))
		C.g_key_file_set_string(kf.cKey, cGroup, cKey, cstr)

	case []bool:
		clist := cListBool(value)
		defer C.g_free(C.gpointer(clist))
		C.g_key_file_set_boolean_list(kf.cKey, cGroup, cKey, clist, C.gsize(len(value)))

	case []int:
		clist := cListInt(value)
		defer C.g_free(C.gpointer(clist))
		C.g_key_file_set_integer_list(kf.cKey, cGroup, cKey, clist, C.gsize(len(value)))

	case []float64:
		clist := cListDouble(value)
		defer C.g_free(C.gpointer(clist))
		C.g_key_file_set_double_list(kf.cKey, cGroup, cKey, clist, C.gsize(len(value)))

	case []string:
		clist := cListString(value)
		C.g_key_file_set_string_list(kf.cKey, cGroup, cKey, clist, C.gsize(len(value)))
		for i := range value {
			C.g_free(C.gpointer((*(*[999999]*C.gchar)(unsafe.Pointer(clist)))[i]))
		}
		C.g_free(C.gpointer(clist))

	default:
		return errors.New("type unknown")
	}
	return nil
}
Example #25
0
func GetDependencies(namespace string) []string {
	if namespace == "GLib" {
		return make([]string, 0)
	}
	_namespace := GlibString(namespace)
	defer C.g_free((C.gpointer)(_namespace))
	raw_list := GListToGo(C.get_dependencies(_namespace))
	results := make([]string, raw_list.Len())
	for i, e := 0, raw_list.Front(); e != nil; i, e = i+1, e.Next() {
		results[i] = C.GoString((*C.char)(e.Value.(C.gpointer)))
	}
	return results
}
Example #26
0
// GetStrv returns a slice of strings from this variant.  It wraps
// g_variant_get_strv, but returns copies of the strings instead.
func (v *Variant) GetStrv() []string {
	gstrv := C.g_variant_get_strv(v.native(), nil)
	// we do not own the memory for these strings, so we must not use strfreev
	// but we must free the actual pointer we receive.
	c := gstrv
	defer C.g_free(C.gpointer(gstrv))
	var strs []string

	for *c != nil {
		strs = append(strs, C.GoString((*C.char)(*c)))
		c = C.next_gcharptr(c)
	}
	return strs
}
Example #27
0
// GetGroups is a wrapper around g_key_file_get_groups().
func (kf *KeyFile) GetGroups() (uint64, []string) {
	var length C.gsize
	c := C.g_key_file_get_groups(kf.cKey, &length)
	var list []string
	for i := 0; i < int(length); i++ {
		if str := C.GoString((*(*[999999]*C.char)(unsafe.Pointer(c)))[i]); str != "" {
			list = append(list, str)
		} else {
			println("-----------------------------------------------dropped empty group")
		}
		C.g_free(C.gpointer((*(*[999999]*C.char)(unsafe.Pointer(c)))[i]))
	}
	return uint64(length), list
}
Example #28
0
// Returns all the icons in the theme, including inherited and hicolor icons.
func (t GTKTheme) GetAllIcons() []string {
	// Get the list of all icons in this theme.
	out := make([]string, 0)
	list := C.gtk_icon_theme_list_icons(t.theme, nil)
	defer C.g_list_free(list)

	// Convert the list into a slice, freeing used elements as we go.
	for ptr := list; ptr != nil; ptr = ptr.next {
		out = append(out, C.GoString((*C.char)(ptr.data)))
		C.g_free(ptr.data)
	}

	return out
}
Example #29
0
// Return a list of available fonts
func ListFontFamilies() []string {
	var names []string
	var families **C.PangoFontFamily
	var nfam C.int
	var fontmap *C.PangoFontMap
	fontmap = C.pango_cairo_font_map_get_default()
	C.pango_font_map_list_families(fontmap, &families, &nfam)
	for i := 0; i < int(nfam); i++ {
		family := C.indexFamily(families, C.int(i))
		familyname := C.pango_font_family_get_name(family)
		names = append(names, C.GoString(familyname))
	}
	C.g_free(C.gpointer(families))
	return names
}
Example #30
0
//export our_openfile_response_callback
func our_openfile_response_callback(dialog *C.GtkDialog, response C.gint, data C.gpointer) {
	f := (*func(string))(unsafe.Pointer(data))
	if response != C.GTK_RESPONSE_ACCEPT {
		(*f)("")
		C.gtk_widget_destroy((*C.GtkWidget)(unsafe.Pointer(dialog)))
		return
	}
	filename := C.gtk_file_chooser_get_filename((*C.GtkFileChooser)(unsafe.Pointer(dialog)))
	if filename == nil {
		panic("chosen filename NULL in OpenFile()")
	}
	realfilename := fromgstr(filename)
	C.g_free(C.gpointer(unsafe.Pointer(filename)))
	C.gtk_widget_destroy((*C.GtkWidget)(unsafe.Pointer(dialog)))
	(*f)(realfilename)
}