File: gbase.c

package info (click to toggle)
gbase 0.5-2.2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, squeeze, stretch, wheezy
  • size: 64 kB
  • ctags: 32
  • sloc: ansic: 322; makefile: 47
file content (420 lines) | stat: -rw-r--r-- 11,177 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*****************************************************************************
 * gbase - Base Converter                                                    *
 *                                                                           *
 * Copyright (C) 1999 Damian Kramer (psiren@hibernaculum.demon.co.uk)        *
 * Copyright (C) 1999 Roger Dunce (kro@penguinpowered.com)                   *
 *                                                                           *
 * You may distribute this program under the terms of the Artistic License.  *
 *                                                                           *
 * This program is distributed in the hope that it will be useful, but       *
 * WITHOUT ANY WARRANTY; without even the implied warranty of                *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
 * Artistic License for more details.                                        *
 *                                                                           *
 *****************************************************************************/


#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <gtk/gtk.h>

#define BIN 2
#define OCT 8
#define DEC 10
#define HEX 16

void create_window();
void quit();
void usage();
void update_entry(int entry_no);
void generate_string(int base);
void clear_entries();
gboolean bad_string(const char *string, int length, int base, int pos);
void process_args(int argc, char **argv);

gint dump_callback(GtkWidget *widget, gpointer *data);
gint clear_callback(GtkWidget *widget, gpointer *data);
gint quit_callback(GtkWidget *widget, gpointer *data);
void radio_button_callback(GtkWidget *widget, gpointer data);
void entry_changed_callback(GtkEntry *entry, gpointer *data);
void text_entered_callback(GtkEditable *editable, gchar *text, gint length,  gint *pos,
			   gpointer data);

long int accumulator = 0;
gboolean ignore_changed_signal = FALSE;
gboolean val_is_signed = FALSE;
gboolean overflow = FALSE;

char buffer[33];

GtkWidget *entry[4];
GtkWidget *overflow_label;

char *labels[] = {"Dec", "Hex", "Oct", "Bin"};
int bases[] = {DEC, HEX, OCT, BIN};

char *version = "0.5";

int main(int argc, char *argv[])
{
  int i;

  GtkWidget *window;
  GtkWidget *vbox;
  GtkWidget *hbox;
  GtkWidget *sep;
  GtkWidget *button;
  GSList *group;
  GtkWidget *dump_button;
  GtkWidget *clear_button;
  GtkWidget *quit_button;
  GtkWidget *table;
  GtkWidget *label;
  GtkWidget *frame;

  gtk_init(&argc, &argv);

  process_args(argc, argv);
  
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window), "Base Converter");
  gtk_window_set_policy(GTK_WINDOW(window), TRUE, FALSE, TRUE);
  gtk_container_border_width(GTK_CONTAINER(window), 5);

  gtk_signal_connect(GTK_OBJECT(window), "delete_event", GTK_SIGNAL_FUNC(quit), NULL);
  gtk_signal_connect(GTK_OBJECT(window), "destroy", GTK_SIGNAL_FUNC(quit), NULL);

  vbox = gtk_vbox_new(FALSE, 5);
  gtk_container_add(GTK_CONTAINER(window), vbox);
  gtk_widget_show(vbox);

  table = gtk_table_new(4, 2, FALSE);
  gtk_box_pack_start_defaults(GTK_BOX(vbox), table);
  gtk_table_set_col_spacings(GTK_TABLE(table), 5);
  gtk_table_set_row_spacings(GTK_TABLE(table), 3);
  gtk_widget_show(table);

  for(i=0; i<4; i++) {
    entry[i] = gtk_entry_new();
    gtk_widget_set_usize(GTK_WIDGET(entry[i]), 240, 22);
    label = gtk_label_new(labels[i]);
    GTK_WIDGET_UNSET_FLAGS(label, GTK_CAN_FOCUS);
    gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, i, i+1);
    gtk_table_attach_defaults(GTK_TABLE(table), entry[i], 1, 2, i, i+1);
    gtk_signal_connect(GTK_OBJECT(entry[i]), "insert-text",
		       (GtkSignalFunc) text_entered_callback, GINT_TO_POINTER(i));
    gtk_signal_connect(GTK_OBJECT(entry[i]), "changed",
 		       (GtkSignalFunc) entry_changed_callback, GINT_TO_POINTER(i));
    gtk_widget_show(label);
    gtk_widget_show(entry[i]);
  }

  hbox = gtk_hbox_new(TRUE, 5);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show(hbox);

  button = gtk_radio_button_new_with_label(NULL, "Unsigned");
  GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS);
  gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
  gtk_widget_show(button);
  gtk_signal_connect(GTK_OBJECT (button), "toggled",
		     GTK_SIGNAL_FUNC(radio_button_callback), NULL);

  gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), TRUE);
  
  group = gtk_radio_button_group(GTK_RADIO_BUTTON(button));
  button = gtk_radio_button_new_with_label(group, "Signed");
  GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS);
  gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
  gtk_widget_show(button);

  frame = gtk_frame_new(NULL);
  gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_ETCHED_IN);
  gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, TRUE, 0);
  gtk_widget_show(frame);

  overflow_label = gtk_label_new(NULL);
  GTK_WIDGET_UNSET_FLAGS(overflow_label, GTK_CAN_FOCUS);
  gtk_container_add(GTK_CONTAINER(frame), overflow_label);
  gtk_widget_show(overflow_label);

  sep = gtk_hseparator_new();
  gtk_box_pack_start(GTK_BOX(vbox), sep, FALSE, FALSE, 0);
  gtk_widget_show(sep);

  hbox = gtk_hbox_new(TRUE, 5);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
  gtk_widget_show(hbox);

  dump_button = gtk_button_new_with_label("Dump");
  gtk_signal_connect(GTK_OBJECT(dump_button), "clicked",
		     (GtkSignalFunc) dump_callback, NULL);
  gtk_box_pack_start(GTK_BOX(hbox), dump_button, TRUE, TRUE, 0);
  gtk_widget_show(dump_button);

  clear_button = gtk_button_new_with_label("Clear");
  gtk_signal_connect(GTK_OBJECT(clear_button), "clicked",
		     (GtkSignalFunc) clear_callback, NULL);
  gtk_box_pack_start(GTK_BOX(hbox), clear_button, TRUE, TRUE, 0);
  gtk_widget_show(clear_button);

  quit_button = gtk_button_new_with_label("Quit");
  gtk_signal_connect(GTK_OBJECT(quit_button), "clicked",
		     (GtkSignalFunc) quit_callback, NULL);
  gtk_box_pack_start(GTK_BOX(hbox), quit_button, TRUE, TRUE, 0);
  gtk_widget_show(quit_button);

  gtk_widget_show(window);

  gtk_main();

  return 0;
}


void text_entered_callback(GtkEditable *editable, gchar *text, gint length,  gint *pos, gpointer data)
{
  g_strup(text);

  if(bad_string(text, length, bases[GPOINTER_TO_INT(data)], *pos)) {
    gtk_signal_emit_stop_by_name(GTK_OBJECT(editable), "insert-text");
    ignore_changed_signal = TRUE;
  }
  return;
}


gboolean bad_string(const char *string, int length, int base, int pos)
{
  static char *charset = "0123456789ABCDEF";
  const gchar *p;
  int i,j = 0;
  gboolean bad_data = FALSE;

  for(i=0, p=string; i<length; i++, p++) {
    if(base == 10 && i == 0 && *p == '-' && val_is_signed && pos == 0) {
      continue;
    }
    for(j=0; j<base; j++) {
      if(toupper(*p) == charset[j])
        break;
    }
    if(j == base) {
      bad_data = TRUE;
      break;
    }
  }
  return bad_data;
}


void entry_changed_callback(GtkEntry *entry, gpointer *data)
{
  int id = GPOINTER_TO_INT(data);
  int i;

  if(ignore_changed_signal) {
    ignore_changed_signal = FALSE;
    return;
  }

  if(strlen(gtk_entry_get_text(GTK_ENTRY(entry))) == 0) {
    clear_entries();
    return;
  }

  errno = 0;

  if(val_is_signed)
    accumulator = strtol(gtk_entry_get_text(GTK_ENTRY(entry)), (char **)NULL, bases[id]);
  else 
    accumulator = strtoul(gtk_entry_get_text(GTK_ENTRY(entry)), (char **)NULL, bases[id]);

  if(errno == ERANGE) {
    gtk_label_set_text(GTK_LABEL(overflow_label), "OVERFLOW");
    overflow = TRUE;
  }
  else {
    gtk_label_set_text(GTK_LABEL(overflow_label), NULL);
    overflow = FALSE;
  }

  for(i=0; i<4; i++) {
    if(i == id)
      continue;
    
    ignore_changed_signal = TRUE;
    update_entry(i);
  }
}


void update_entry(gint entry_no)
{
  generate_string(bases[entry_no]);
  gtk_entry_set_text(GTK_ENTRY(entry[entry_no]), buffer); 
}


void generate_string(int base)
{
  static char *fmt[] = {"%lu", "%lX", "%lo"};
  int pos;
  int i;
  
  if(base == DEC && val_is_signed)
    sprintf(buffer, "%ld", accumulator);
  else {
    switch(base) {
    case DEC:
      sprintf(buffer, "%lu", accumulator);
      break;
    case HEX:
      sprintf(buffer, "%lX", accumulator);
      break;
    case OCT:
      sprintf(buffer, "%lo", accumulator);
      break;
    case BIN:
      for(i = 31; i > 0 && !((1<<i) & accumulator); i--);
      for(pos = 0;i > -1; buffer[pos++] = ((1<<i--) & accumulator) ? '1' : '0');
      buffer[pos] = '\0';
    }
  }
}

gint dump_callback(GtkWidget *widget, gpointer *data)
{
  int i;

  if(overflow)
    return TRUE;

  for(i=0; i<4; i++) {
    generate_string(bases[i]);
    g_print("%s: %s\n", labels[i], buffer);
  }

  return TRUE;
}


gint clear_callback(GtkWidget *widget, gpointer *data)
{
  clear_entries();
  return TRUE;
}


void clear_entries()
{
  int i;

  for(i=0; i<4; i++) {
    ignore_changed_signal = TRUE;
    gtk_entry_set_text(GTK_ENTRY(entry[i]), "");
  }

  accumulator = 0;
}


void radio_button_callback(GtkWidget *widget, gpointer data)
{
  if(GTK_TOGGLE_BUTTON(widget)->active)
    val_is_signed = FALSE;
  else
    val_is_signed = TRUE;

  update_entry(0);
  update_entry(1);
}


gint quit_callback(GtkWidget *widget, gpointer *data)
{
  gtk_exit(0);
}


void quit()
{
  gtk_exit(0);
}

void process_args(int argc, char **argv)
{
  int i,j;
  int type = 0;

  for(i=1; i<argc; i++) {

    if(!strcmp(argv[i], "--help")) {
      usage();
    }
   
    if(!strcmp(argv[i], "--version") || !strcmp(argv[i], "-v")) {
      g_print("gbase version %s\n", version);
      gtk_exit(0);
    }

    if(!strcmp(argv[i], "--signed") || !strcmp(argv[i], "-s")) {
      val_is_signed = TRUE;
      continue;
    }

    if(!strcmp(argv[i], "-d"))
      type = DEC;
    else if(!strcmp(argv[i], "-h"))
      type = HEX;
    else if(!strcmp(argv[i], "-o"))
      type = OCT;
    else if(!strcmp(argv[i], "-b"))
      type = BIN;
    else 
      usage();

    if(i+1 == argc)
      usage();

    
    if(bad_string(argv[i+1], strlen(argv[i+1]), type, 0)) {
      g_print("Bad data: %s\n", argv[i+1]);
      gtk_exit(0);
    }
    
    if(val_is_signed)
      accumulator = strtol(argv[i+1], (char **)NULL, type);
    else 
      accumulator = strtoul(argv[i+1], (char **)NULL, type);

    if(errno == ERANGE) {
      g_print("OVERFLOW\n");
      gtk_exit(0);
    }

    for(j=0; j<4; j++) {
      generate_string(bases[j]);
      g_print("%s: %s\n", labels[j], buffer);
    }
    gtk_exit(0);
  }
}


void usage()
{
  g_print("Usage gbase [OPTIONS]\n");
  g_print("\nOptions:\n\n");
  g_print("    --help                This help\n");
  g_print("    --version             Print version number and exit\n\n");
  g_print("    -s                    Treat the number as a signed value\n\n");
  g_print("    -d <decimal number>   Print this decimal number in all bases\n");
  g_print("    -h <hex number>       Print this hexadecimal number in all bases\n");
  g_print("    -o <octal number>     Print this octal number in all bases\n");
  g_print("    -b <binary number>    Print this binary number in all bases\n");
  gtk_exit(0);
}