~ubuntu-branches/ubuntu/hardy/gnomad2/hardy

« back to all changes in this revision

Viewing changes to src/util.c

  • Committer: Bazaar Package Importer
  • Author(s): Shaun Jackman
  • Date: 2004-10-25 10:24:21 UTC
  • Revision ID: james.westby@ubuntu.com-20041025102421-hnnl6uzlkutcibvi
Tags: upstream-2.5.0
ImportĀ upstreamĀ versionĀ 2.5.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* util.c
 
2
   General utility functions, eg for string formatting
 
3
   Copyright (C) 2001 Linus Walleij
 
4
 
 
5
This file is part of the GNOMAD package.
 
6
 
 
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)
 
10
any later version.
 
11
 
 
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. 
 
16
 
 
17
*/
 
18
 
 
19
#include "common.h"
 
20
#include <libgnomeui/libgnomeui.h>
 
21
 
 
22
/* Find the length of a string vector */
 
23
gint vectorlength(gchar **vector)
 
24
{
 
25
  gchar **tmp = vector;
 
26
  gint size = 0;
 
27
 
 
28
  if (!tmp)
 
29
    return 0;
 
30
  
 
31
  while (*tmp)
 
32
    {
 
33
      size ++;
 
34
      tmp ++;
 
35
    }
 
36
  return size;
 
37
}
 
38
 
 
39
/* Concatenate two dynamic strings */
 
40
gchar *stringcat(gchar *org, gchar *add)
 
41
{
 
42
  /* This can be improved under GTK+ 2.0 
 
43
   * new GLib functions -> 2.0 */
 
44
  gchar *tmpstring;
 
45
 
 
46
  if (!add)
 
47
    return org;
 
48
  if (!org)
 
49
    return g_strdup(add);
 
50
  tmpstring = (gchar *) g_malloc(strlen(org) + strlen(add) + 1);
 
51
  tmpstring = strcpy(tmpstring, org);
 
52
  tmpstring = strcat(tmpstring, add);
 
53
  g_free(org);
 
54
  return tmpstring;
 
55
}
 
56
 
 
57
void replacechar(gchar *string, gchar find, gchar replace)
 
58
{
 
59
  /* Replaces character find with character replace
 
60
   * in string */
 
61
  gchar *tmp = string;
 
62
 
 
63
  if (!string || !find || !replace)
 
64
    return;
 
65
 
 
66
  while (*tmp)
 
67
    {
 
68
      if (*tmp == find)
 
69
        *tmp = replace;
 
70
      tmp ++;
 
71
    }
 
72
}
 
73
 
 
74
gchar *replacestring(gchar *string, gchar *find, gchar *replace)
 
75
{
 
76
  /* Replaces the substring find with substring replace
 
77
   * in string */
 
78
  static gchar stringbuffer[512];
 
79
  gchar *returnstring;
 
80
  gchar *tmp = string;
 
81
  gchar **tmp2;
 
82
  gchar **tmp3;
 
83
  gint i;
 
84
 
 
85
  if (!string || !find || !replace)
 
86
    return string;
 
87
  
 
88
  tmp2 = g_strsplit(string, find, 0);
 
89
 
 
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);
 
97
    g_free(string);
 
98
    returnstring = g_strdup(stringbuffer);
 
99
  }
 
100
  /* If there were no matches, return original string */
 
101
  else if (vectorlength(tmp2) > 1) {
 
102
    stringbuffer[0] = '\0';
 
103
    tmp3 = tmp2;
 
104
    while (*tmp3)
 
105
      {
 
106
        strcat(stringbuffer, *tmp3);
 
107
        /* If there are more strings in the list, insert the replacement */
 
108
        if (*(tmp3+1))
 
109
          strcat(stringbuffer, replace);
 
110
        tmp3 ++;
 
111
      }
 
112
    /* Free old string */
 
113
    g_free(string);
 
114
    returnstring = g_strdup(stringbuffer);
 
115
  } else {
 
116
    returnstring = string;
 
117
  }
 
118
  g_strfreev(tmp2);
 
119
  return returnstring;
 
120
}
 
121
 
 
122
/* Converts a string into guint representation (if possible) */
 
123
guint string_to_guint(gchar *string)
 
124
{
 
125
  gchar *dummy;
 
126
 
 
127
  if (!string)
 
128
    return 0;
 
129
  
 
130
  return (guint) strtoul(string, &dummy, 10);
 
131
}
 
132
 
 
133
/* Converts a figure representing a number of seconds to
 
134
 * a string in mm:ss notation */
 
135
gchar *seconds_to_mmss(guint seconds)
 
136
{
 
137
  gchar tmp2[4];
 
138
  gchar tmp[10];
 
139
 
 
140
  if (!seconds)
 
141
    return g_strdup("0:00");
 
142
  sprintf(tmp2, "0%u", seconds%60);
 
143
  while (strlen(tmp2)>2) {
 
144
    tmp2[0]=tmp2[1];
 
145
    tmp2[1]=tmp2[2];
 
146
    tmp2[2]='\0';
 
147
  }
 
148
  sprintf(tmp, "%lu:%s", seconds/60, tmp2);
 
149
  return g_strdup(tmp);
 
150
}
 
151
 
 
152
/* Converts a string in mm:ss notation to a figure
 
153
 * representing seconds */
 
154
guint mmss_to_seconds(gchar *mmss)
 
155
{
 
156
  gchar **tmp;
 
157
  guint seconds = 0;
 
158
 
 
159
  if (!mmss)
 
160
    return seconds;
 
161
 
 
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]);
 
166
  }
 
167
  if (tmp != NULL)
 
168
    g_strfreev(tmp);
 
169
  return seconds;
 
170
}
 
171
 
 
172
/* are there only numbers in this string? */
 
173
gboolean is_a_number(gchar *string)
 
174
{
 
175
  gchar *tmp;
 
176
  
 
177
  if (string == NULL)
 
178
    return FALSE;
 
179
  tmp = string;
 
180
  while (*tmp)
 
181
    {
 
182
      /* See if it is not a number, skip spaces */
 
183
      if ((*tmp < '0' || 
 
184
           *tmp > '9') && 
 
185
          *tmp != ' ' &&
 
186
          *tmp != '.')
 
187
        return FALSE;
 
188
      tmp ++;
 
189
    }
 
190
  return TRUE;
 
191
}
 
192
 
 
193
/* Good for mocking up correctly adjusted dialogs */
 
194
void add_empty_hbox (GtkWidget *tobox)
 
195
{
 
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);
 
199
}
 
200
 
 
201
/* Create an error dialog */
 
202
void create_error_dialog(gchar *errorstring)
 
203
{
 
204
  GtkWidget *dialog;
 
205
  GtkWidget *label, *button;
 
206
 
 
207
  dialog = gnome_message_box_new (errorstring,
 
208
                                  GNOME_MESSAGE_BOX_ERROR,
 
209
                                  GNOME_STOCK_BUTTON_OK,
 
210
                                  NULL);
 
211
  gtk_widget_show(dialog);
 
212
}
 
213
 
 
214
void hexdump(unsigned char *data, guint len)
 
215
{
 
216
  guint i;
 
217
 
 
218
  for (i=0; i<len; i++) {
 
219
    if (i%16 == 0 && i != 0)
 
220
      g_print("\n");
 
221
    g_print("%02x ", (guchar) data[i]);
 
222
  }
 
223
  g_print("\n");
 
224
}
 
225