2
General utility functions, eg for string formatting
3
Copyright (C) 2001 Linus Walleij
5
This file is part of the GNOMAD package.
7
GNOMAD is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2, or (at your option)
12
You should have received a copy of the GNU General Public License
13
along with GNOMAD; see the file COPYING. If not, write to
14
the Free Software Foundation, 59 Temple Place - Suite 330,
15
Boston, MA 02111-1307, USA.
20
#include <libgnomeui/libgnomeui.h>
22
/* Find the length of a string vector */
23
gint vectorlength(gchar **vector)
39
/* Concatenate two dynamic strings */
40
gchar *stringcat(gchar *org, gchar *add)
42
/* This can be improved under GTK+ 2.0
43
* new GLib functions -> 2.0 */
50
tmpstring = (gchar *) g_malloc(strlen(org) + strlen(add) + 1);
51
tmpstring = strcpy(tmpstring, org);
52
tmpstring = strcat(tmpstring, add);
57
void replacechar(gchar *string, gchar find, gchar replace)
59
/* Replaces character find with character replace
63
if (!string || !find || !replace)
74
gchar *replacestring(gchar *string, gchar *find, gchar *replace)
76
/* Replaces the substring find with substring replace
78
static gchar stringbuffer[512];
85
if (!string || !find || !replace)
88
tmp2 = g_strsplit(string, find, 0);
90
/* This is the case when the replacement is in the
91
* last characters of the string */
92
if (vectorlength(tmp2) == 1 &&
93
strlen(string) > strlen(*tmp2)) {
94
stringbuffer[0] = '\0';
95
strcat(stringbuffer, *tmp2);
96
strcat(stringbuffer, replace);
98
returnstring = g_strdup(stringbuffer);
100
/* If there were no matches, return original string */
101
else if (vectorlength(tmp2) > 1) {
102
stringbuffer[0] = '\0';
106
strcat(stringbuffer, *tmp3);
107
/* If there are more strings in the list, insert the replacement */
109
strcat(stringbuffer, replace);
112
/* Free old string */
114
returnstring = g_strdup(stringbuffer);
116
returnstring = string;
122
/* Converts a string into guint representation (if possible) */
123
guint string_to_guint(gchar *string)
130
return (guint) strtoul(string, &dummy, 10);
133
/* Converts a figure representing a number of seconds to
134
* a string in mm:ss notation */
135
gchar *seconds_to_mmss(guint seconds)
141
return g_strdup("0:00");
142
sprintf(tmp2, "0%u", seconds%60);
143
while (strlen(tmp2)>2) {
148
sprintf(tmp, "%lu:%s", seconds/60, tmp2);
149
return g_strdup(tmp);
152
/* Converts a string in mm:ss notation to a figure
153
* representing seconds */
154
guint mmss_to_seconds(gchar *mmss)
162
tmp = g_strsplit(mmss, ":", 0);
163
if (vectorlength(tmp) == 2) {
164
seconds = 60 * string_to_guint(tmp[0]);
165
seconds += string_to_guint(tmp[1]);
172
/* are there only numbers in this string? */
173
gboolean is_a_number(gchar *string)
182
/* See if it is not a number, skip spaces */
193
/* Good for mocking up correctly adjusted dialogs */
194
void add_empty_hbox (GtkWidget *tobox)
196
GtkWidget *thing = gtk_hbox_new (FALSE, 0);
197
gtk_widget_show (thing);
198
gtk_box_pack_start (GTK_BOX (tobox), thing, TRUE, FALSE, 0);
201
/* Create an error dialog */
202
void create_error_dialog(gchar *errorstring)
205
GtkWidget *label, *button;
207
dialog = gnome_message_box_new (errorstring,
208
GNOME_MESSAGE_BOX_ERROR,
209
GNOME_STOCK_BUTTON_OK,
211
gtk_widget_show(dialog);
214
void hexdump(unsigned char *data, guint len)
218
for (i=0; i<len; i++) {
219
if (i%16 == 0 && i != 0)
221
g_print("%02x ", (guchar) data[i]);