void swfdec_text_buffer_insert_text (SwfdecTextBuffer *buffer, gsize pos, const char *text) { gsize len; GSequenceIter *iter; SwfdecTextBufferFormat *format; g_return_if_fail (SWFDEC_IS_TEXT_BUFFER (buffer)); g_return_if_fail (pos <= buffer->text->len); g_return_if_fail (text != NULL); len = strlen (text); if (len == 0) return; if (pos == buffer->text->len) { g_string_insert_len (buffer->text, pos, text, len); format = swfdec_text_buffer_format_new (); format->start = pos; swfdec_text_attributes_copy (&format->attr, &buffer->default_attributes, SWFDEC_TEXT_ATTRIBUTES_MASK); iter = g_sequence_append (buffer->attributes, format); swfdec_text_buffer_remove_duplicates (g_sequence_iter_prev (iter), g_sequence_iter_next (iter)); } else { g_string_insert_len (buffer->text, pos, text, len); iter = g_sequence_get_end_iter (buffer->attributes); for (;;) { iter = g_sequence_iter_prev (iter); format = g_sequence_get (iter); if (format->start <= pos) break; format->start += len; } } CHECK_ATTRIBUTES (buffer); /* adapt cursor */ if (buffer->cursor_start >= pos) buffer->cursor_start += len; if (buffer->cursor_end >= pos) buffer->cursor_end += len; g_signal_emit (buffer, signals[TEXT_CHANGED], 0); g_signal_emit (buffer, signals[CURSOR_CHANGED], 0, (gulong) MIN (buffer->cursor_start, buffer->cursor_end), (gulong) MAX (buffer->cursor_start, buffer->cursor_end)); }
/** * Try to find an input module that can parse the given buffer. * * The buffer must contain enough of the beginning of the file for * the input modules to find a match. This is format-dependent, but * 128 bytes is normally enough. * * If an input module is found, an instance is created into *in. * Otherwise, *in contains NULL. * * If an instance is created, it has the given buffer used for scanning * already submitted to it, to be processed before more data is sent. * This allows a frontend to submit an initial chunk of a non-seekable * stream, such as stdin, without having to keep it around and submit * it again later. * */ SR_API int sr_input_scan_buffer(GString *buf, const struct sr_input **in) { const struct sr_input_module *imod; GHashTable *meta; unsigned int m, i; int ret; uint8_t mitem, avail_metadata[8]; /* No more metadata to be had from a buffer. */ avail_metadata[0] = SR_INPUT_META_HEADER; avail_metadata[1] = 0; *in = NULL; ret = SR_ERR; for (i = 0; input_module_list[i]; i++) { imod = input_module_list[i]; if (!imod->metadata[0]) { /* Module has no metadata for matching so will take * any input. No point in letting it try to match. */ continue; } if (!check_required_metadata(imod->metadata, avail_metadata)) /* Cannot satisfy this module's requirements. */ continue; meta = g_hash_table_new(NULL, NULL); for (m = 0; m < sizeof(imod->metadata); m++) { mitem = imod->metadata[m] & ~SR_INPUT_META_REQUIRED; if (mitem == SR_INPUT_META_HEADER) g_hash_table_insert(meta, GINT_TO_POINTER(mitem), buf); } if (g_hash_table_size(meta) == 0) { /* No metadata for this module, so nothing to match. */ g_hash_table_destroy(meta); continue; } sr_spew("Trying module %s.", imod->id); ret = imod->format_match(meta); g_hash_table_destroy(meta); if (ret == SR_ERR_DATA) { /* Module recognized this buffer, but cannot handle it. */ break; } else if (ret == SR_ERR) { /* Module didn't recognize this buffer. */ continue; } else if (ret != SR_OK) { /* Can be SR_ERR_NA. */ return ret; } /* Found a matching module. */ sr_spew("Module %s matched.", imod->id); *in = sr_input_new(imod, NULL); g_string_insert_len((*in)->buf, 0, buf->str, buf->len); break; } return ret; }
GString* g_string_prepend (GString *string, const gchar *val) { g_return_val_if_fail (string != NULL, NULL); g_return_val_if_fail (val != NULL, string); return g_string_insert_len (string, 0, val, -1); }
GString* g_string_append_len (GString *string, const gchar *val, gssize len) { g_return_val_if_fail (string != NULL, NULL); g_return_val_if_fail (val != NULL, string); return g_string_insert_len (string, -1, val, len); }
GString* g_string_insert (GString *string, gssize pos, const gchar *val) { g_return_val_if_fail (string != NULL, NULL); g_return_val_if_fail (val != NULL, string); if (pos >= 0) g_return_val_if_fail (pos <= string->len, string); return g_string_insert_len (string, pos, val, -1); }
/** * as_gstring_replace: * @string: The #GString to operate on * @search: The text to search for * @replace: The text to use for substitutions * * Performs multiple search and replace operations on the given string. * * Returns: the number of replacements done, or 0 if @search is not found. **/ guint as_gstring_replace (GString *string, const gchar *search, const gchar *replace) { gchar *tmp; guint count = 0; gsize search_idx = 0; gsize replace_len; gsize search_len; g_return_val_if_fail (string != NULL, 0); g_return_val_if_fail (search != NULL, 0); g_return_val_if_fail (replace != NULL, 0); /* nothing to do */ if (string->len == 0) return 0; search_len = strlen (search); replace_len = strlen (replace); do { tmp = g_strstr_len (string->str + search_idx, -1, search); if (tmp == NULL) break; /* advance the counter in case @replace contains @search */ search_idx = (gsize) (tmp - string->str); /* reallocate the string if required */ if (search_len > replace_len) { g_string_erase (string, (gssize) search_idx, (gssize) (search_len - replace_len)); memcpy (tmp, replace, replace_len); } else if (search_len < replace_len) { g_string_insert_len (string, (gssize) search_idx, replace, (gssize) (replace_len - search_len)); /* we have to treat this specially as it could have * been reallocated when the insertion happened */ memcpy (string->str + search_idx, replace, replace_len); } else { /* just memcmp in the new string */ memcpy (tmp, replace, replace_len); } search_idx += replace_len; count++; } while (TRUE); return count; }
static void fix_utf8_minus(GString *str) { static const gchar minus_utf8[] = { 0xe2, 0x88, 0x92 }; gchar *minus; guint lastpos = 0; while ((minus = strchr(str->str + lastpos, '-'))) { *minus = minus_utf8[0]; lastpos = minus - str->str + 1; g_string_insert_len(str, lastpos, minus_utf8 + 1, G_N_ELEMENTS(minus_utf8) - 1); lastpos += 2; } }
static void append_number (GString *res, double c, gboolean suppress1) { size_t prelen = res->len; g_string_append_printf (res, "%g", c); if (suppress1 && res->len == prelen + 1 && res->str[prelen] == '1') g_string_truncate (res, prelen); else { /* Handle the minuses as in -1.2222e-3. */ size_t i; for (i = prelen; i < res->len; i++) if (res->str[i] == '-') { res->str[i] = minus_utf8[0]; g_string_insert_len (res, i + 1, minus_utf8 + 1, minus_utf8_len - 1); i += minus_utf8_len - 1; } } }
/** * @brief Inserts text at given position * * @param text an AtkEditableText * @param string string to insert * @param length string length * @param [out] position at witch text is inserted. * After the call it points at the position after the newly inserted text. */ static void eail_multibuttonentry_insert_text(AtkEditableText *text, const gchar *string, gint length, gint *position) { Evas_Object *widget; Evas_Object *entry; GString *s; widget = eail_widget_get_widget(EAIL_WIDGET(text)); if (!widget || !elm_multibuttonentry_editable_get(widget)) return; entry = elm_multibuttonentry_entry_get(widget); s = g_string_new(elm_entry_entry_get(entry)); s = g_string_insert_len(s, *position, string, length); elm_entry_entry_set(entry, s->str); g_string_free(s, TRUE); *position += length; }
int ctf_sequence_read(struct bt_stream_pos *ppos, struct bt_definition *definition) { struct definition_sequence *sequence_definition = container_of(definition, struct definition_sequence, p); struct declaration_sequence *sequence_declaration = sequence_definition->declaration; struct bt_declaration *elem = sequence_declaration->elem; struct ctf_stream_pos *pos = ctf_pos(ppos); if (elem->id == CTF_TYPE_INTEGER) { struct declaration_integer *integer_declaration = container_of(elem, struct declaration_integer, p); if (integer_declaration->encoding == CTF_STRING_UTF8 || integer_declaration->encoding == CTF_STRING_ASCII) { if (integer_declaration->len == CHAR_BIT && integer_declaration->p.alignment == CHAR_BIT) { uint64_t len = bt_sequence_len(sequence_definition); if (!ctf_align_pos(pos, integer_declaration->p.alignment)) return -EFAULT; if (!ctf_pos_access_ok(pos, len * CHAR_BIT)) return -EFAULT; g_string_assign(sequence_definition->string, ""); g_string_insert_len(sequence_definition->string, (gssize)0, (const gchar *)ctf_get_pos_addr(pos), (gssize)len); if (!ctf_move_pos(pos, len * CHAR_BIT)) return -EFAULT; return 0; } } } return bt_sequence_rw(ppos, definition); }
guint gs_string_replace (GString *string, const gchar *search, const gchar *replace) { gchar *tmp; guint count = 0; guint replace_len; guint search_len; search_len = strlen (search); replace_len = strlen (replace); do { tmp = g_strstr_len (string->str, -1, search); if (tmp == NULL) goto out; /* reallocate the string if required */ if (search_len > replace_len) { g_string_erase (string, tmp - string->str, search_len - replace_len); } if (search_len < replace_len) { g_string_insert_len (string, tmp - string->str, search, replace_len - search_len); } /* just memcmp in the new string */ memcpy (tmp, replace, replace_len); count++; } while (TRUE); out: return count; }
int main (int argc, char *argv[]) { GStringChunk *string_chunk; gchar *tmp_string = NULL, *tmp_string_2; gint i; GString *string1, *string2; string_chunk = g_string_chunk_new (1024); for (i = 0; i < 100000; i ++) { tmp_string = g_string_chunk_insert (string_chunk, "hi pete"); if (strcmp ("hi pete", tmp_string) != 0) g_error ("string chunks are broken.\n"); } tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string); g_assert (tmp_string_2 != tmp_string && strcmp(tmp_string_2, tmp_string) == 0); tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string); g_assert (tmp_string_2 == tmp_string); g_string_chunk_free (string_chunk); string1 = g_string_new ("hi pete!"); string2 = g_string_new (""); g_assert (string1 != NULL); g_assert (string2 != NULL); g_assert (strlen (string1->str) == string1->len); g_assert (strlen (string2->str) == string2->len); g_assert (string2->len == 0); g_assert (strcmp ("hi pete!", string1->str) == 0); g_assert (strcmp ("", string2->str) == 0); for (i = 0; i < 10000; i++) g_string_append_c (string1, 'a'+(i%26)); g_assert((strlen("hi pete!") + 10000) == string1->len); g_assert((strlen("hi pete!") + 10000) == strlen(string1->str)); #ifndef G_OS_WIN32 /* MSVC and mingw32 use the same run-time C library, which doesn't like the %10000.10000f format... */ g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f", "this pete guy sure is a wuss, like he's the number ", 1, " wuss. everyone agrees.\n", string1->str, 10, 666, 15, 15, 666.666666666, 666.666666666); #else g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f", "this pete guy sure is a wuss, like he's the number ", 1, " wuss. everyone agrees.\n", string1->str, 10, 666, 15, 15, 666.666666666, 666.666666666); #endif g_string_free (string1, TRUE); g_string_free (string2, TRUE); /* append */ string1 = g_string_new ("firsthalf"); g_string_append (string1, "lasthalf"); g_assert (strcmp (string1->str, "firsthalflasthalf") == 0); g_string_free (string1, TRUE); /* append_len */ string1 = g_string_new ("firsthalf"); g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf")); g_assert (strcmp (string1->str, "firsthalflasthalf") == 0); g_string_free (string1, TRUE); /* prepend */ string1 = g_string_new ("lasthalf"); g_string_prepend (string1, "firsthalf"); g_assert (strcmp (string1->str, "firsthalflasthalf") == 0); g_string_free (string1, TRUE); /* prepend_len */ string1 = g_string_new ("lasthalf"); g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf")); g_assert (strcmp (string1->str, "firsthalflasthalf") == 0); g_string_free (string1, TRUE); /* insert */ string1 = g_string_new ("firstlast"); g_string_insert (string1, 5, "middle"); g_assert (strcmp (string1->str, "firstmiddlelast") == 0); g_string_free (string1, TRUE); /* insert with pos == end of the string */ string1 = g_string_new ("firstmiddle"); g_string_insert (string1, strlen ("firstmiddle"), "last"); g_assert (strcmp (string1->str, "firstmiddlelast") == 0); g_string_free (string1, TRUE); /* insert_len */ string1 = g_string_new ("firstlast"); g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle")); g_assert (strcmp (string1->str, "firstmiddlelast") == 0); g_string_free (string1, TRUE); /* insert_len with magic -1 pos for append */ string1 = g_string_new ("first"); g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last")); g_assert (strcmp (string1->str, "firstlast") == 0); g_string_free (string1, TRUE); /* insert_len with magic -1 len for strlen-the-string */ string1 = g_string_new ("first"); g_string_insert_len (string1, 5, "last", -1); g_assert (strcmp (string1->str, "firstlast") == 0); g_string_free (string1, TRUE); /* g_string_equal */ string1 = g_string_new ("test"); string2 = g_string_new ("te"); g_assert (! g_string_equal(string1, string2)); g_string_append (string2, "st"); g_assert (g_string_equal(string1, string2)); g_string_free (string1, TRUE); g_string_free (string2, TRUE); /* Check handling of embedded ASCII 0 (NUL) characters in GString. */ string1 = g_string_new ("fiddle"); string2 = g_string_new ("fiddle"); g_assert (g_string_equal(string1, string2)); g_string_append_c(string1, '\0'); g_assert (! g_string_equal(string1, string2)); g_string_append_c(string2, '\0'); g_assert (g_string_equal(string1, string2)); g_string_append_c(string1, 'x'); g_string_append_c(string2, 'y'); g_assert (! g_string_equal(string1, string2)); g_assert (string1->len == 8); g_string_append(string1, "yzzy"); g_assert (string1->len == 12); g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0); g_string_insert(string1, 1, "QED"); g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0); g_string_free (string1, TRUE); g_string_free (string2, TRUE); return 0; }
/** * spapr_drc_populate_dt * * @fdt: libfdt device tree * @path: path in the DT to generate properties * @owner: parent Object/DeviceState for which to generate DRC * descriptions for * @drc_type_mask: mask of sPAPRDRConnectorType values corresponding * to the types of DRCs to generate entries for * * generate OF properties to describe DRC topology/indices to guests * * as documented in PAPR+ v2.1, 13.5.2 */ int spapr_drc_populate_dt(void *fdt, int fdt_offset, Object *owner, uint32_t drc_type_mask) { Object *root_container; ObjectProperty *prop; ObjectPropertyIterator *iter; uint32_t drc_count = 0; GArray *drc_indexes, *drc_power_domains; GString *drc_names, *drc_types; int ret; /* the first entry of each properties is a 32-bit integer encoding * the number of elements in the array. we won't know this until * we complete the iteration through all the matching DRCs, but * reserve the space now and set the offsets accordingly so we * can fill them in later. */ drc_indexes = g_array_new(false, true, sizeof(uint32_t)); drc_indexes = g_array_set_size(drc_indexes, 1); drc_power_domains = g_array_new(false, true, sizeof(uint32_t)); drc_power_domains = g_array_set_size(drc_power_domains, 1); drc_names = g_string_set_size(g_string_new(NULL), sizeof(uint32_t)); drc_types = g_string_set_size(g_string_new(NULL), sizeof(uint32_t)); /* aliases for all DRConnector objects will be rooted in QOM * composition tree at DRC_CONTAINER_PATH */ root_container = container_get(object_get_root(), DRC_CONTAINER_PATH); iter = object_property_iter_init(root_container); while ((prop = object_property_iter_next(iter))) { Object *obj; sPAPRDRConnector *drc; sPAPRDRConnectorClass *drck; uint32_t drc_index, drc_power_domain; if (!strstart(prop->type, "link<", NULL)) { continue; } obj = object_property_get_link(root_container, prop->name, NULL); drc = SPAPR_DR_CONNECTOR(obj); drck = SPAPR_DR_CONNECTOR_GET_CLASS(drc); if (owner && (drc->owner != owner)) { continue; } if ((drc->type & drc_type_mask) == 0) { continue; } drc_count++; /* ibm,drc-indexes */ drc_index = cpu_to_be32(drck->get_index(drc)); g_array_append_val(drc_indexes, drc_index); /* ibm,drc-power-domains */ drc_power_domain = cpu_to_be32(-1); g_array_append_val(drc_power_domains, drc_power_domain); /* ibm,drc-names */ drc_names = g_string_append(drc_names, drck->get_name(drc)); drc_names = g_string_insert_len(drc_names, -1, "\0", 1); /* ibm,drc-types */ drc_types = g_string_append(drc_types, spapr_drc_get_type_str(drc->type)); drc_types = g_string_insert_len(drc_types, -1, "\0", 1); } object_property_iter_free(iter); /* now write the drc count into the space we reserved at the * beginning of the arrays previously */ *(uint32_t *)drc_indexes->data = cpu_to_be32(drc_count); *(uint32_t *)drc_power_domains->data = cpu_to_be32(drc_count); *(uint32_t *)drc_names->str = cpu_to_be32(drc_count); *(uint32_t *)drc_types->str = cpu_to_be32(drc_count); ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-indexes", drc_indexes->data, drc_indexes->len * sizeof(uint32_t)); if (ret) { fprintf(stderr, "Couldn't create ibm,drc-indexes property\n"); goto out; } ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-power-domains", drc_power_domains->data, drc_power_domains->len * sizeof(uint32_t)); if (ret) { fprintf(stderr, "Couldn't finalize ibm,drc-power-domains property\n"); goto out; } ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-names", drc_names->str, drc_names->len); if (ret) { fprintf(stderr, "Couldn't finalize ibm,drc-names property\n"); goto out; } ret = fdt_setprop(fdt, fdt_offset, "ibm,drc-types", drc_types->str, drc_types->len); if (ret) { fprintf(stderr, "Couldn't finalize ibm,drc-types property\n"); goto out; } out: g_array_free(drc_indexes, true); g_array_free(drc_power_domains, true); g_string_free(drc_names, true); g_string_free(drc_types, true); return ret; }
static gboolean project_import_generate_file (AnjutaPluginDescription *backend, ProjectImportDialog *import_dialog, GFile *project_file) { /* Of course we could do some more intelligent stuff here * and check which plugins are really needed */ GFile* source_file = NULL; gchar *backend_id = NULL; GError* error = NULL; if (!anjuta_plugin_description_get_string (backend, "Project", "Supported-Project-Types", &backend_id)) { if (!strcmp (backend_id, "automake")) source_file = g_file_new_for_path (AM_PROJECT_FILE); else if (!strcmp (backend_id, "make")) source_file = g_file_new_for_path (MKFILE_PROJECT_FILE); else if (!strcmp (backend_id, "directory")) source_file = g_file_new_for_path (DIRECTORY_PROJECT_FILE); } g_free (backend_id); if (source_file != NULL) { /* Use a default project file */ if (!g_file_copy (source_file, project_file, G_FILE_COPY_NONE, NULL, NULL, NULL, &error)) { if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_EXISTS) { gchar *prjfile = g_file_get_parse_name (project_file); if (anjuta_util_dialog_boolean_question (GTK_WINDOW (import_dialog), FALSE, _("A file named \"%s\" already exists. " "Do you want to replace it?"), prjfile)) { g_error_free (error); error = NULL; g_file_copy (source_file, project_file, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error); } g_free (prjfile); } } if (!error) { time_t ctime = time(NULL); GFileInfo* file_info = g_file_info_new(); g_file_info_set_attribute_uint64(file_info, "time::modified", ctime); g_file_info_set_attribute_uint64(file_info, "time::created", ctime); g_file_info_set_attribute_uint64(file_info, "time::access", ctime); g_file_set_attributes_from_info (project_file, file_info, G_FILE_QUERY_INFO_NONE, NULL, NULL); g_object_unref (G_OBJECT(file_info));; } } else { /* For unknown project backend we use the directory project file and * replace the backend plugin with the right one */ gchar *content; gsize length; source_file = g_file_new_for_path (DIRECTORY_PROJECT_FILE); if (g_file_load_contents (source_file, NULL, &content, &length, NULL, &error)) { GString *buffer; const gchar *pos; const gchar *plugin; const gchar *end_plugin; gssize len; buffer = g_string_new_len (content, length); pos = buffer->str; len = buffer->len; for (;;) { plugin = g_strstr_len (pos, len, "<plugin "); if (plugin == NULL) break; end_plugin = g_strstr_len (plugin, len - (plugin - pos), "</plugin>"); if (end_plugin == NULL) break; if (g_strstr_len (plugin, end_plugin - plugin, "\"IAnjutaProjectBackend\"") != NULL) break; pos = end_plugin + 9; len -= (end_plugin + 9 - pos); } if ((plugin == NULL) || (end_plugin == NULL)) { g_set_error (&error, ianjuta_project_backend_error_quark(),0, "Unable to find backend plugin"); } else { /* Replace directory backend with right one */ GString *str; GFileOutputStream *stream; gchar *name = NULL; gchar *plugin_id = NULL; anjuta_plugin_description_get_string (backend, "Anjuta Plugin", "Name", &name); anjuta_plugin_description_get_string (backend, "Anjuta Plugin", "Location", &plugin_id); str = g_string_new (NULL); g_string_printf (str, "<plugin name= \"%s\"\n" " mandatory=\"yes\">\n" " <require group=\"Anjuta Plugin\"\n" " attribute=\"Location\"\n" " value=\"%s\"/>\n" " <require group=\"Anjuta Plugin\"\n" " attribute=\"Interfaces\"\n" " value=\"IAnjutaProjectBackend\"/>\n" " ", name, plugin_id); g_string_erase (buffer, plugin - buffer->str, end_plugin - plugin); g_string_insert_len (buffer, plugin - buffer->str, str->str, str->len); g_string_free (str, TRUE); stream = g_file_create (project_file, G_FILE_CREATE_NONE, NULL, &error); if (stream == NULL && error->domain == G_IO_ERROR && error->code == G_IO_ERROR_EXISTS) { gchar *prjfile = g_file_get_parse_name (project_file); if (anjuta_util_dialog_boolean_question (GTK_WINDOW (import_dialog), FALSE, _("A file named \"%s\" already exists. " "Do you want to replace it?"), prjfile)) { g_error_free (error); error = NULL; stream = g_file_replace (project_file, NULL, FALSE, G_FILE_CREATE_REPLACE_DESTINATION, NULL, &error); } g_free (prjfile); } if (stream != NULL) { gsize written; g_output_stream_write_all (G_OUTPUT_STREAM (stream), buffer->str, buffer->len, &written, NULL, &error); g_output_stream_close (G_OUTPUT_STREAM (stream), NULL, NULL); } } g_string_free (buffer, TRUE); g_free (content); } } g_object_unref (source_file); if (error) { gchar *prjfile; prjfile = g_file_get_parse_name (project_file); /* show the dialog since it may be hidden */ gtk_widget_show (GTK_WIDGET (import_dialog)); anjuta_util_dialog_error (GTK_WINDOW (import_dialog), _("A file named \"%s\" cannot be written: %s. " "Check if you have write access to the project directory."), prjfile, error->message); g_free (prjfile); g_error_free (error); return FALSE; } return TRUE; }