예제 #1
0
/**
 * na_core_utils_dir_split_ext:
 * @string: the input path or URI to be splitted.
 * @first: a pointer to a buffer which will contain the first part of the split.
 * @ext: a pointer to a buffer which will contain the extension part of the path.
 *
 * Split the given @string, returning the first part and the extension in newly
 * allocated buffers which should be g_free() by the caller.
 *
 * The extension is set to an empty string if no extension is detected.
 *
 * Since: 2.30
 */
void
na_core_utils_dir_split_ext( const gchar *string, gchar **first, gchar **ext )
{
	gchar *dupped;
	gchar **array, **iter;

	dupped = g_strreverse( g_strdup( string ));
	array = g_strsplit( dupped, ".", 2 );

	if( g_strv_length( array ) == 1 ){
		if( ext ){
			*ext = g_strdup( "" );
		}
		if( first ){
			*first = g_strreverse( g_strdup(( const gchar * ) *array ));
		}
	} else {
		if( ext ){
			*ext = g_strreverse( g_strdup(( const gchar * ) *array ));
		}
		iter = array;
		++iter;
		if( first ){
			*first = g_strreverse( g_strdup(( const gchar * ) *iter ));
		}
	}

	g_strfreev( array );
	g_free( dupped );
}
예제 #2
0
/* Resolves '..' and '.' references in a path. If the path provided is relative,
 * it will be relative to the current directory */
gchar *
mono_path_canonicalize (const char *path)
{
	gchar *abspath, *pos, *lastpos, *dest;
	int backc;

	if (g_path_is_absolute (path)) {
		abspath = g_strdup (path);
	} else {
		gchar *tmpdir = g_get_current_dir ();
		abspath = g_build_filename (tmpdir, path, NULL);
		g_free (tmpdir);
	}

#ifdef PLATFORM_WIN32
	g_strdelimit (abspath, "/", '\\');
#endif
	abspath = g_strreverse (abspath);

	backc = 0;
	dest = lastpos = abspath;
	pos = strchr (lastpos, G_DIR_SEPARATOR);

	while (pos != NULL) {
		int len = pos - lastpos;
		if (len == 1 && lastpos [0] == '.') {
			// nop
		} else if (len == 2 && lastpos [0] == '.' && lastpos [1] == '.') {
			backc++;
		} else if (len > 0) {
			if (backc > 0) {
				backc--;
			} else {
				if (dest != lastpos) 
					/* The two strings can overlap */
					memmove (dest, lastpos, len + 1);
				dest += len + 1;
			}
		}
		lastpos = pos + 1;
		pos = strchr (lastpos, G_DIR_SEPARATOR);
	}

#ifdef PLATFORM_WIN32 /* For UNC paths the first '\' is removed. */
	if (*(lastpos-1) == G_DIR_SEPARATOR && *(lastpos-2) == G_DIR_SEPARATOR)
		lastpos = lastpos-1;
#endif
	
	if (dest != lastpos) strcpy (dest, lastpos);
	return g_strreverse (abspath);
}
예제 #3
0
static void *
init_header (iPodBuffer *buffer, gchar _header_id[4], guint header_len)
{
	MHeader *mh;
	int padded_size;
	gchar *header_id;

	padded_size = get_padded_header_size (_header_id);
	if (padded_size != 0) {
		header_len = padded_size;
	}
	g_assert (header_len > sizeof (MHeader));
	ipod_buffer_maybe_grow (buffer, header_len);

	mh = (MHeader*)ipod_buffer_get_pointer (buffer);
	if (mh == NULL) {
		return NULL;
	}
	memset (mh, 0, header_len);

	header_id = g_strndup (_header_id, 4);
	if (buffer->byte_order == G_BIG_ENDIAN) {
		g_strreverse (header_id);
	}
	strncpy ((char *)mh->header_id, header_id, 4);
	mh->header_len = get_gint32 (header_len, buffer->byte_order);

	g_free (header_id);
	return mh;
}
예제 #4
0
/**
 * @brief convert an integer into a gchar based string
 *
 *
 * @param integer   integer to convert
 *
 * @return  a newly allocated string
 *
 * @caveats You have to unallocate the returned string when you no more use it to save memory
 *
 * @todo: check usage of this function which a cause of memory leak
 *
 * */
gchar *utils_str_itoa ( gint integer )
{
    div_t result_div;
    gchar *chaine;
    gint i = 0;
    gint num;

    chaine = g_malloc0 ( 11*sizeof (gchar) );

    num = abs(integer);

    /* Construct the result in the reverse order from right to left, then reverse it. */
    do
    {
	result_div = div ( num, 10 );
	chaine[i] = result_div.rem + '0';
	i++;
    }
    while ( ( num = result_div.quot ));

    /* Add the sign at the end of the string just before to reverse it to avoid
     to have to insert it at the begin just after... */
    if (integer < 0)
    {
        chaine[i++] = '-';
    }

    chaine[i] = 0;

    g_strreverse ( chaine );

    return ( chaine );
}
예제 #5
0
char *formataResultado(double valor) {
    char str[256];
    int i;
    int count=0;
    sprintf(str,"\n%f",valor);
    g_strreverse(str);
    for(i=0; i<strlen(str); i++) {
        if(str[i]=='0') {
            str[i]=' ';    // eliminado os `zeros` desnecessarios
            if(str[i+1]==',')
                str[i+1]=' ';
        } else {
            break;
        }
    }
    g_strreverse(str);
    return  str;
}
예제 #6
0
void
sp_selected_path_reverse(SPDesktop *desktop)
{
    Inkscape::Selection *selection = sp_desktop_selection(desktop);
    GSList *items = (GSList *) selection->itemList();

    if (!items) {
        sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to reverse."));
        return;
    }


    // set "busy" cursor
    desktop->setWaitingCursor();

    bool did = false;
    desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Reversing paths..."));

    for (GSList *i = items; i != NULL; i = i->next) {

        if (!SP_IS_PATH(i->data))
            continue;

        did = true;
        SPPath *path = SP_PATH(i->data);

        SPCurve *rcurve = sp_path_get_curve_reference(path)->create_reverse();

        gchar *str = sp_svg_write_path(rcurve->get_pathvector());
        if ( sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path)) ) {
            SP_OBJECT_REPR(path)->setAttribute("inkscape:original-d", str);
        } else {
            SP_OBJECT_REPR(path)->setAttribute("d", str);
        }
        g_free(str);

        rcurve->unref();

        // reverse nodetypes order (Bug #179866)
        gchar *nodetypes = g_strdup(SP_OBJECT_REPR(path)->attribute("sodipodi:nodetypes"));
        if ( nodetypes ) {
            SP_OBJECT_REPR(path)->setAttribute("sodipodi:nodetypes", g_strreverse(nodetypes));
            g_free(nodetypes);
        }
    }

    desktop->clearWaitingCursor();

    if (did) {
        sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_REVERSE,
                         _("Reverse path"));
    } else {
        sp_desktop_message_stack(desktop)->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to reverse in the selection."));
    }
}
예제 #7
0
static DirMatchString*
dir_match_string_new (gchar *string)
{
	DirMatchString *str = NULL;

	str = g_slice_new0(DirMatchString);
	str->length = strlen (string);
	str->string = string;
	str->reverse = g_strreverse(g_strdup (string));

	return str;
}
예제 #8
0
파일: util.c 프로젝트: fadec/sqlpilot
/* out must be a buffer of at least length BUF_TIME */
void format_time(const char *input, char *out, char separator)
{
    if (strlen(input)) {
        if (strstr(input, ".")) {
            float hours;
            sscanf(input, "%f", &hours);
            m_to_strtime(hours * 60, out, BUF_TIME, separator);
        } else {
            char str[BUF_TIME], *ptr;
            char str2[BUF_TIME];
            int i = 0;
            strncpy(str, input, BUF_TIME);
            str[BUF_TIME-1] = '\0';
            ptr = g_strreverse(str);
            while (i < BUF_TIME - 1) {
                if (i == 2) {		/* BUF_TIME must be >= 4 */
                    str2[i] = separator;
                    i++;
                }
                if (*ptr >= '0' && *ptr <= '9') {
                    str2[i] = *ptr;
                    i++;
                }
                if (*ptr == '\0') {
                    /* format like (separator == ':') ? "%02d:%02d" : "%d:%02d */
                    str2[i] = (i < 4 || separator == ':') ? '0' : '\0';
                    i++;
                } else {
                    ptr++;
                }
            }
            str2[i] = '\0';
            g_strreverse(str2);
            m_to_strtime(strtime_to_m(str2), out, BUF_TIME, separator);
        }
    }
    else {
        out[0] = '\0';
    }
}
예제 #9
0
void list_files (state_t *self)
{
    DIR *dir = opendir (self->config->data_dir);
    if (!dir) {
        dbg (DBG_INFO, "warning: data dir %s does not exist.", self->config->data_dir);
        return;
    }

    struct dirent *dt;
    self->hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);

    while ((dt = readdir (dir))!=NULL) {
        // must end in .log
        if (strncmp (g_strreverse (dt->d_name), "gol.",4)!=0) continue;
        g_strreverse (dt->d_name);

        char *name = g_strconcat (dt->d_name, NULL);
        char *desc = search_index_file (self, dt->d_name);
        printf ("%s %s\n", dt->d_name, desc);
        g_hash_table_insert (self->hash, name, desc);
    }

    closedir (dir);
}
예제 #10
0
static GList *
lookup_rules (const gchar *mimetype)
{
	GList *mimetype_rules = NULL;
	RuleInfo *info;
	gchar *reversed;
	gint len, i;

	if (!rules) {
		return NULL;
	}

	if (mimetype_map) {
		mimetype_rules = g_hash_table_lookup (mimetype_map, mimetype);

		if (mimetype_rules) {
			return mimetype_rules;
		}
	}

	reversed = g_strdup (mimetype);
	g_strreverse (reversed);
	len = strlen (mimetype);

	/* Apply the rules! */
	for (i = 0; i < rules->len; i++) {
		GList *l;

		info = &g_array_index (rules, RuleInfo, i);

		for (l = info->patterns; l; l = l->next) {
			if (g_pattern_match (l->data, len, mimetype, reversed)) {
				/* Match, store for future queries and return */
				mimetype_rules = g_list_prepend (mimetype_rules, info);
			}
		}
	}

	if (mimetype_rules) {
		mimetype_rules = g_list_reverse (mimetype_rules);
		g_hash_table_insert (mimetype_map, g_strdup (mimetype), mimetype_rules);
	}

	g_free (reversed);

	return mimetype_rules;
}
예제 #11
0
/* Add a gwgetdata to the main window */
void 
gwget_data_add_download(GwgetData *gwgetdata)
{
	gint response;
	gchar *reverse_filename;
	GtkWidget *radio, *recursive_window;
	
	if (check_url_already_exists(gwgetdata->url)) {
		run_dialog_information(_("Unable to add this download"),
				       _("This download is already added"));
		return;
	}

	/* if the url it's not a file drop a dialog to recurse into the url */
	reverse_filename = g_strdup(gwgetdata->filename);
	reverse_filename = g_strreverse(reverse_filename);
	if (!strcmp(gwgetdata->filename,"") || !strcmp(gwgetdata->filename,gwgetdata->url) ||
		!strncmp(reverse_filename,"lmth",4) || !strncmp(reverse_filename,"mth",3) || 
		!strncmp(reverse_filename,"php",3)  || !strncmp(reverse_filename,"psa",3)) {
		recursive_window=GTK_WIDGET (gtk_builder_get_object(builder,"dialog2"));
		response=gtk_dialog_run(GTK_DIALOG(recursive_window));
		gtk_widget_hide(GTK_WIDGET(recursive_window));
		if (response==GTK_RESPONSE_OK) {
			radio=GTK_WIDGET (gtk_builder_get_object(builder,"radio_index"));
			if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radio))) {
				gwgetdata->recursive=FALSE;
			}
			radio=GTK_WIDGET (gtk_builder_get_object(builder,"radio_multimedia"));
			if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radio))) {
				gwgetdata->recursive=TRUE;
				gwgetdata->multimedia=TRUE;
			}
			radio=GTK_WIDGET (gtk_builder_get_object(builder,"radio_mirror"));
			if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radio))) {
				gwgetdata->recursive=TRUE;
				gwgetdata->mirror=TRUE;
			}
			radio=GTK_WIDGET (gtk_builder_get_object(builder,"radio_recursive"));
			if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radio))) {
				gwgetdata->recursive=TRUE;
			}
		}
	}
	downloads = g_list_append(downloads,gwgetdata);
	new_download(gwgetdata);
	gwget_data_set_state(gwgetdata,DL_NOT_CONNECTED);
}
예제 #12
0
void *
db_parse_context_get_m_header_internal (DBParseContext *ctx, const char *id, off_t size) 
{
	MHeader *h;
	char *header_id;
	
	if (!ctx) {
		return NULL;
	}

	if (db_parse_context_get_remaining_length (ctx) < 8) {
		return NULL;
	}

	h = (MHeader *)ctx->cur_pos;
	if (!h) {
		return NULL;
	}
	
	header_id = g_strndup ((char *)h->header_id, 4);
	if (ctx->byte_order == G_BIG_ENDIAN) {
		g_strreverse (header_id);
	}
	if (strncmp (id, header_id, 4) != 0) {
	        g_free (header_id);
		return NULL;
	}

	g_free (header_id);

	/* FIXME: this test sucks for compat: if a field is smaller than 
	 * expected, we probably should create a buffer of the appropriate 
	 * size inited to 0, copy the data that is available in it and use
	 * that buffer in the rest of the code (maybe it's harmful to have
	 * some fields at 0 in some headers though...)
	 */
	if (get_gint32 (h->header_len, ctx->byte_order) < size) {
		return NULL;
	}

	db_parse_context_set_header_len (ctx, get_gint32 (h->header_len, 
							  ctx->byte_order));

	return h;
}
예제 #13
0
static void
dissect_wow_pdu(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
	proto_item *ti;
	proto_tree *wow_tree, *wow_realms_tree;

	gchar *string, *realm_name;
	guint8 cmd, srp_i_len, srp_g_len, srp_n_len;
	guint16 num_realms;
	guint32 offset = 0;
	gint len, i;

	col_set_str(pinfo->cinfo, COL_PROTOCOL, "WOW");

	col_clear(pinfo->cinfo, COL_INFO);

	cmd = tvb_get_guint8(tvb, offset);

	if(check_col(pinfo->cinfo, COL_INFO)) {
		col_set_str(pinfo->cinfo, COL_INFO,
			    val_to_str_const(cmd, cmd_vs,
				       "Unrecognized packet type"));
	}

	if(tree) {
		ti = proto_tree_add_item(tree, proto_wow, tvb, 0, -1, ENC_NA);
		wow_tree = proto_item_add_subtree(ti, ett_wow);

		proto_tree_add_item(wow_tree, hf_wow_command, tvb, offset, 1,
				    ENC_LITTLE_ENDIAN);
		offset += 1;

		switch(cmd) {

		case AUTH_LOGON_CHALLENGE :

			if(WOW_CLIENT_TO_SERVER) {
				proto_tree_add_item(wow_tree, hf_wow_error, tvb,
						    offset, 1, ENC_LITTLE_ENDIAN);
				offset += 1;

				proto_tree_add_item(wow_tree, hf_wow_pkt_size,
						    tvb, offset, 2, ENC_LITTLE_ENDIAN);
				offset += 2;

				string = g_strreverse(tvb_get_ephemeral_string(tvb, offset, 4));
				proto_tree_add_string(wow_tree, hf_wow_gamename,
						      tvb, offset, 4, string);
				offset += 4;

				proto_tree_add_item(wow_tree, hf_wow_version1,
						    tvb, offset, 1, ENC_LITTLE_ENDIAN);
				offset += 1;

				proto_tree_add_item(wow_tree, hf_wow_version2,
						    tvb, offset, 1, ENC_LITTLE_ENDIAN);
				offset += 1;

				proto_tree_add_item(wow_tree, hf_wow_version3,
						    tvb, offset, 1, ENC_LITTLE_ENDIAN);
				offset += 1;

				proto_tree_add_item(wow_tree, hf_wow_build, tvb,
						    offset, 2, ENC_LITTLE_ENDIAN);
				offset += 2;

				string = g_strreverse(tvb_get_ephemeral_string(tvb, offset, 4));
				proto_tree_add_string(wow_tree, hf_wow_platform,
						      tvb, offset, 4, string);
				offset += 4;

				string = g_strreverse(tvb_get_ephemeral_string(tvb, offset, 4));
				proto_tree_add_string(wow_tree, hf_wow_os, tvb,
						      offset, 4, string);
				offset += 4;

				string = g_strreverse(tvb_get_ephemeral_string(tvb, offset, 4));
				proto_tree_add_string(wow_tree, hf_wow_country,
						      tvb, offset, 4, string);
				offset += 4;

				proto_tree_add_item(wow_tree,
						    hf_wow_timezone_bias,
						    tvb, offset, 4, ENC_LITTLE_ENDIAN);
				offset += 4;

				proto_tree_add_item(wow_tree, hf_wow_ip, tvb,
						    offset, 4, ENC_BIG_ENDIAN);
				offset += 4;

				proto_tree_add_item(wow_tree,
						    hf_wow_srp_i_len,
						    tvb, offset, 1, ENC_LITTLE_ENDIAN);
				srp_i_len = tvb_get_guint8(tvb, offset);
				offset += 1;

				proto_tree_add_item(wow_tree,
						    hf_wow_srp_i, tvb,
						    offset, srp_i_len,
						    ENC_ASCII|ENC_NA);
				offset += srp_i_len;


			} else if(WOW_SERVER_TO_CLIENT) {
				proto_tree_add_item(wow_tree, hf_wow_error, tvb,
						    offset, 1, ENC_LITTLE_ENDIAN);
				offset += 1;

				offset += 1; /* Unknown field */

				proto_tree_add_item(wow_tree, hf_wow_srp_b, tvb,
						    offset, 32, ENC_NA);
				offset += 32;

				proto_tree_add_item(wow_tree, hf_wow_srp_g_len,
						    tvb, offset, 1, ENC_LITTLE_ENDIAN);
				srp_g_len = tvb_get_guint8(tvb, offset);
				offset += 1;

				proto_tree_add_item(wow_tree, hf_wow_srp_g, tvb,
						    offset, srp_g_len, ENC_NA);
				offset += srp_g_len;

				proto_tree_add_item(wow_tree, hf_wow_srp_n_len,
						    tvb, offset, 1, ENC_LITTLE_ENDIAN);
				srp_n_len = tvb_get_guint8(tvb, offset);
				offset += 1;

				proto_tree_add_item(wow_tree, hf_wow_srp_n, tvb,
						    offset, srp_n_len, ENC_NA);
				offset += srp_n_len;

				proto_tree_add_item(wow_tree, hf_wow_srp_s, tvb,
						    offset, 32, ENC_NA);
				offset += 32;

				offset += 16; /* Unknown field */
			}

			break;

		case AUTH_LOGON_PROOF :

			if(WOW_CLIENT_TO_SERVER) {
				proto_tree_add_item(wow_tree, hf_wow_srp_a, tvb,
						    offset, 32, ENC_NA);
				offset += 32;

				proto_tree_add_item(wow_tree, hf_wow_srp_m1,
						    tvb, offset, 20, ENC_NA);
				offset += 20;

				proto_tree_add_item(wow_tree, hf_wow_crc_hash,
						    tvb, offset, 20, ENC_NA);
				offset += 20;

				proto_tree_add_item(wow_tree, hf_wow_num_keys,
						    tvb, offset, 1, ENC_LITTLE_ENDIAN);
				offset += 1;

				offset += 1; /* Unknown field */

			} else if(WOW_SERVER_TO_CLIENT) {
				proto_tree_add_item(wow_tree, hf_wow_error, tvb,
						    offset, 1, ENC_LITTLE_ENDIAN);
				offset += 1;

				proto_tree_add_item(wow_tree, hf_wow_srp_m2,
						    tvb, offset, 20, ENC_NA);
				offset += 20;

				offset += 4; /* Unknown field */

				offset += 2; /* Unknown field */
			}

			break;

		case REALM_LIST :

			if(WOW_CLIENT_TO_SERVER) {


			} else if(WOW_SERVER_TO_CLIENT) {

				proto_tree_add_item(wow_tree, hf_wow_pkt_size,
						    tvb, offset, 2, ENC_LITTLE_ENDIAN);
				offset += 2;

				offset += 4; /* Unknown field; always 0 */

				proto_tree_add_item(wow_tree, hf_wow_num_realms,
						    tvb, offset, 2, ENC_LITTLE_ENDIAN);
				num_realms = tvb_get_letohs(tvb, offset);
				offset += 2;

				for(i = 1; i <= num_realms; i++) {
					realm_name = tvb_get_ephemeral_stringz(tvb,
								     offset + 3,
								     &len);

					ti = proto_tree_add_text(wow_tree, tvb,
								 offset, 0,
								 "%s",
								 realm_name);

					wow_realms_tree = proto_item_add_subtree(ti, ett_wow_realms);
					proto_tree_add_item(wow_realms_tree, hf_wow_realm_type, tvb, offset, 1, ENC_LITTLE_ENDIAN);
					offset += 1;

					proto_tree_add_item(wow_realms_tree, hf_wow_realm_status, tvb, offset, 1, ENC_LITTLE_ENDIAN);
					offset += 1;

					proto_tree_add_item(wow_realms_tree, hf_wow_realm_color, tvb, offset, 1, ENC_LITTLE_ENDIAN);
					offset += 1;

					proto_tree_add_string(wow_realms_tree, hf_wow_realm_name, tvb, offset, len, realm_name);
					offset += len;

					string = tvb_get_ephemeral_stringz(tvb, offset,
								 &len);
					proto_tree_add_string(wow_realms_tree, hf_wow_realm_socket, tvb, offset, len, string);
					offset += len;

					proto_tree_add_item(wow_realms_tree, hf_wow_realm_population_level, tvb, offset, 4, ENC_LITTLE_ENDIAN);
					offset += 4;

					proto_tree_add_item(wow_realms_tree, hf_wow_realm_num_characters, tvb, offset, 1, ENC_LITTLE_ENDIAN);
					offset += 1;

					proto_tree_add_item(wow_realms_tree, hf_wow_realm_timezone, tvb, offset, 1, ENC_LITTLE_ENDIAN);
					offset += 1;

					offset += 1; /* Unknown field */
				}

				break;
			}
		}
	}
}
예제 #14
0
파일: mono-path.c 프로젝트: LogosBible/mono
/* There are a couple of tests for this method in mono/test/mono-path.cs */
gchar *
mono_path_canonicalize (const char *path)
{
	gchar *abspath, *pos, *lastpos, *dest;
	int backc;

	if (g_path_is_absolute (path)) {
		abspath = g_strdup (path);
	} else {
		gchar *tmpdir = g_get_current_dir ();
		abspath = g_build_filename (tmpdir, path, NULL);
		g_free (tmpdir);
	}

#ifdef HOST_WIN32
	g_strdelimit (abspath, '/', '\\');
#endif
	abspath = g_strreverse (abspath);

	backc = 0;
	dest = lastpos = abspath;
	pos = strchr (lastpos, G_DIR_SEPARATOR);

	while (pos != NULL) {
		int len = pos - lastpos;
		if (len == 1 && lastpos [0] == '.') {
			// nop
		} else if (len == 2 && lastpos [0] == '.' && lastpos [1] == '.') {
			backc++;
		} else if (len > 0) {
			if (backc > 0) {
				backc--;
			} else {
				if (dest != lastpos) 
					/* The two strings can overlap */
					memmove (dest, lastpos, len + 1);
				dest += len + 1;
			}
		}
		lastpos = pos + 1;
		pos = strchr (lastpos, G_DIR_SEPARATOR);
	}

#ifdef HOST_WIN32
	/* Avoid removing the first '\' for UNC paths. We must make sure that it's indeed an UNC path
	by checking if the \\ pair happens exactly at the end of the string.
	*/
	if (*(lastpos-1) == G_DIR_SEPARATOR && *(lastpos-2) == G_DIR_SEPARATOR && *lastpos == 0)
		lastpos = lastpos-1;
#endif
	
	if (dest != lastpos) strcpy (dest, lastpos);
	
	g_strreverse (abspath);

	/* We strip away all trailing dir separators. This is not correct for the root directory,
	 * since we'll return an empty string, so re-append a dir separator if there is none in the
	 * result */
	if (strchr (abspath, G_DIR_SEPARATOR) == NULL) {
		int len = strlen (abspath);
		abspath = (gchar *) g_realloc (abspath, len + 2);
		abspath [len] = G_DIR_SEPARATOR;
		abspath [len+1] = 0;
	}

	return abspath;
}
예제 #15
0
/**
 * ajoute le séparateur des milliers passé en paramètre
 *
 * \param string to modify WARNING string is free
 *
 * \return a new allocated sring
 * */
gchar *gsb_real_add_thousands_sep ( gchar *str_number, const gchar *thousands_sep )
{
    gchar *mon_thousands_sep;
    gchar *result = NULL;
    gchar *ptr;
    gchar *dest;
    gchar *tmp_ptr;
    gchar **tab_str = NULL;
    gint nbre_char;
    gint i = 0;
    gint j = 0;
    gint sep = 0;
    gint longueur;

    if ( thousands_sep == NULL )
        return str_number;

    nbre_char = strlen ( str_number );
    str_number = g_strreverse ( str_number );
    ptr = str_number;

    if ( ( longueur = strlen ( thousands_sep ) ) == 1 )
        mon_thousands_sep = g_strndup ( thousands_sep, 1 );
    else
        mon_thousands_sep = g_strndup ( "&", 1 );

    dest = g_malloc0 ( 128 * sizeof ( gchar ) );
    tmp_ptr = dest;

    while ( i < nbre_char )
    {
        dest[i+sep] = ptr[0];

        ptr++;
        tmp_ptr++;
        i++;
        j++;
        if ( i < nbre_char && j == 3 )
        {
            tmp_ptr = g_stpcpy ( tmp_ptr, mon_thousands_sep );
            j = 0;
            sep++;
        }
    };

    result = g_strndup ( dest, nbre_char + sep );
    result = g_strreverse ( result );

    /* on met le bon séparateur si necessaire */
    if ( longueur > 1 )
    {
        tab_str = g_strsplit ( result, "&", 0 );
        g_free ( result );

        result = g_strjoinv ( thousands_sep, tab_str );
        g_strfreev ( tab_str );
    }

    g_free ( mon_thousands_sep );
    g_free ( str_number );
    g_free ( dest );

    return result;
}
예제 #16
0
/*
 * text_reformat
 * @pos: an inout parameter to keep track of a position
 *
 * Retunrs: a new string
 */
static gchar *
text_reformat (GdauiNumericEntry *entry, gchar *str, gint *pos, gboolean allow_sep_ending)
{
	g_assert (str);
	GString *string;
	gint len;
	gchar *ptr;
	gint i, last_th_sep_pos;
	gint dec_pos = -1; /* position of the decimal */
	gint cpos;

	len = strlen (str);
	string = g_string_new ("");
	if (entry->priv->num_attr.is_int)
		last_th_sep_pos = len - 1;
	else
		last_th_sep_pos = -1;

	cpos = *pos;
	for (i = len - 1, ptr = str + len - 1;
	     ptr >= str;
	     ptr --, i --) {
		if (*ptr == entry->priv->decimal_sep) {
			last_th_sep_pos = i - 1;
			dec_pos = len - i - 1;
		}
		else if (i == last_th_sep_pos - 3) {
			last_th_sep_pos = i;
			if (entry->priv->thousands_sep) {
				g_string_append_c (string, entry->priv->thousands_sep);
				if (i < cpos)
					cpos ++;
			}
		}
			
		g_string_append_c (string, *ptr);
	}
	if (last_th_sep_pos == -1) {
		cpos = *pos;
		g_string_truncate (string, 0);
		last_th_sep_pos = len - 1;
		for (i = len - 1, ptr = str + len - 1;
		     ptr >= str;
		     ptr --, i --) {
			if (i == last_th_sep_pos - 3) {
				last_th_sep_pos = i;
				if (entry->priv->thousands_sep) {
					g_string_append_c (string, entry->priv->thousands_sep);
					if (i < cpos)
						cpos ++;
				}
			}
				
			g_string_append_c (string, *ptr);
		}
	}
	g_strreverse (string->str);

	/* fix the number of decimals if necessary */
	if ((! entry->priv->num_attr.is_int) &&
	    entry->priv->nb_decimals != G_MAXUINT16) {
		if (dec_pos == -1) {
			/* no decimal */
			if (entry->priv->nb_decimals > 0) {
				g_string_append_c (string, entry->priv->decimal_sep);
				for (i = 0; i < entry->priv->nb_decimals; i++)
					g_string_append_c (string, '0');
			}
		}
		else {
			gint nb_dec;
			len = strlen (string->str);
			nb_dec = dec_pos; /* FIXME */
			if (nb_dec < entry->priv->nb_decimals) {
				for (i = nb_dec; i < entry->priv->nb_decimals; i++)
					g_string_append_c (string, '0');
			}
			else if (nb_dec > entry->priv->nb_decimals) {
				g_string_truncate (string, len - (nb_dec - entry->priv->nb_decimals));
				if (!allow_sep_ending &&
				    (string->str[string->len - 1] == entry->priv->decimal_sep)) {
					/* we don't want numbers terminated by entry->priv->decimal_sep */
					g_string_truncate (string, string->len - 1);
				}
			}
		}
	}
	else if ((! entry->priv->num_attr.is_int) &&
		 dec_pos >= 0) {
		/* remove all the '0' which are useless */
		while ((string->str[string->len - 1] == entry->priv->decimal_sep) ||
		       (string->str[string->len - 1] == '0')) {
			if (string->str[string->len - 1] == entry->priv->decimal_sep) {
				if (!allow_sep_ending)
					g_string_truncate (string, string->len - 1);
				break;
			}
			else
				g_string_truncate (string, string->len - 1);
		}
	}

	*pos = cpos;

	return g_string_free (string, FALSE);
}
예제 #17
0
파일: tools.c 프로젝트: dimkr/beaver
void refresh_converter (GtkWidget *Widget, gint EntryID)
{
  gchar *NumberString;
  gint i = 0, j = 1;
  gulong Number = 0;

  NumberString = gtk_entry_get_text (GTK_ENTRY (Entry[EntryID-1]));
  if (EntryID == 1)
    {
      if ((strlen (NumberString) == 10) &&
	  (g_strcasecmp (NumberString, "4294967295") > 0))
	{
	  if (g_strcasecmp (NumberString, "9999999999") <= 0)
	    {
	      Number = ~0; /* <-- Max Unsigned Long Integer */
	    }
	  else
	    {
	      Number = 0; /* <-- Min Unsigned Long Integer */
	    }
	}
      else
	{
	  g_strreverse (NumberString);
	  while ((NumberString[i] >= '0') && (NumberString[i] <= '9'))
	    {
	      Number = Number + (NumberString[i] - '0')*j;
	      j *= 10;
	      i++;
	    }
	  if (NumberString[i] != '\0')
	    Number = 0; /* <-- Min Unsigned Long Integer */
	}
    }
  if (EntryID == 2)
    {
      g_strreverse (NumberString);

      while ((NumberString[i] == '0') || (NumberString[i] == '1'))
	{
	  Number = Number + (NumberString[i] - '0')*j;
	  j *= 2;
	  i++;
	}
      if (NumberString[i] != '\0')
	Number = 0; /* <-- Min Unsigned Long Integer */
    }
  if (EntryID == 3)
    {
      if ((strlen (NumberString) == 11) &&
	  (g_strcasecmp (NumberString, "37777777777") > 0))
	{
	  if (g_strcasecmp (NumberString, "77777777777") <= 0)
	    {
	      Number = ~0; /* <-- Max Unsigned Long Integer */
	    }
	  else
	    {
	      Number = 0; /* <-- Min Unsigned Long Integer */
	    }
	}
      else
	{
	  g_strreverse (NumberString);
	  while ((NumberString[i] >= '0') && (NumberString[i] <= '7'))
	    {
	      Number = Number + (NumberString[i] - '0')*j;
	      j *= 8;
	      i++;
	    }
	  if (NumberString[i] != '\0')
	    Number = 0; /* <-- Min Unsigned Long Integer */
	}
    }
  if (EntryID == 4)
    {
      gint Zero;

      g_strreverse (NumberString);
      while (((NumberString[i] >= '0') && (NumberString[i] <= '9')) ||
	     ((NumberString[i] >= 'a') && (NumberString[i] <= 'f')) ||
	     ((NumberString[i] >= 'A') && (NumberString[i] <= 'F')))
	{
	  if ((NumberString[i] >= '0') && (NumberString[i] <= '9'))
	    Zero = '0';
	  else if ((NumberString[i] >= 'a') && (NumberString[i] <= 'f'))
	    Zero = 'a' - 10;
	  else
	    Zero = 'A' - 10;
	  Number = Number + (NumberString[i] - Zero)*j;
	  j *= 16;
	  i++;
	}
      if (NumberString[i] != '\0')
	Number = 0; /* <-- Min Unsigned Long Integer */
    }
  gtk_entry_set_text (GTK_ENTRY (Entry[0]),
		      g_strdup_printf ("%lu", Number));
  gtk_entry_set_text (GTK_ENTRY (Entry[2]),
		      g_strdup_printf ("%lo", Number));
  gtk_entry_set_text (GTK_ENTRY (Entry[3]),
		      g_strdup_printf ("%lx", Number));
  if (Number)
    {
      gint Bit;

      gtk_entry_set_text (GTK_ENTRY (Entry[1]), "");
      while (Number > 0)
	{
	  Bit = Number % 2;
	  Number = Number / 2;
	  gtk_entry_prepend_text (GTK_ENTRY (Entry[1]),
				  g_strdup_printf ("%d", Bit));
	}
    }
  else
    gtk_entry_set_text (GTK_ENTRY (Entry[1]), "0");
  (void)Widget; /* avoid the "unused parameter" warning */
}