]> Dogcows Code - chaz/homebank/blob - src/hb-misc.c
add gitignore
[chaz/homebank] / src / hb-misc.c
1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2014 Maxime DOYEN
3 *
4 * This file is part of HomeBank.
5 *
6 * HomeBank is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * HomeBank is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "homebank.h"
21 #include "hb-misc.h"
22
23 #define MYDEBUG 0
24
25 #if MYDEBUG
26 #define DB(x) (x);
27 #else
28 #define DB(x);
29 #endif
30
31 /* our global datas */
32 extern struct HomeBank *GLOBALS;
33 extern struct Preferences *PREFS;
34
35 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
36
37 static gdouble fint(gdouble amount)
38 {
39 gdouble fi;
40
41 modf(amount, &fi);
42 return(fi);
43 }
44
45 static unsigned dix_puissance_n(unsigned n)
46 {
47 unsigned i, retval = 1;
48
49 for(i = 0; i < n; i++)
50 retval *= 10;
51
52 return retval;
53 }
54
55 double arrondi(const double x, unsigned n)
56 {
57 unsigned N = dix_puissance_n(n);
58 return floor((x * N) + 0.5) / N;
59 }
60
61 // new for v4.5
62
63 /*
64 * convert an amount in base currency
65 *
66 */
67 gdouble to_base_amount(gdouble value, guint32 kcur)
68 {
69 /*
70 gdouble newvalue;
71 Currency *cur;
72
73 if(kcur == GLOBALS->kcur)
74 return value;
75
76 cur = da_cur_get(kcur);
77 if(cur == NULL)
78 return 0;
79 newvalue = value * cur->rate;
80 return newvalue;
81 */
82 return value;
83 }
84
85
86 /* new currency fct
87 static gint real_mystrfmoncurr(gchar *outstr, gint outlen, gchar *buf1, Currency *cur)
88 {
89 gint size = 0;
90 gchar groupbuf[G_ASCII_DTOSTR_BUF_SIZE];
91 gchar **str_array;
92 guint i, length;
93 gchar *monstr;
94
95 str_array = g_strsplit(buf1, ".", 2);
96 monstr = NULL;
97
98 length = strlen(str_array[0]);
99
100 if( cur->grouping_char == NULL || !strlen(cur->grouping_char) )
101 {
102 monstr = g_strjoinv(cur->decimal_char, str_array);
103 }
104 else
105 {
106 gchar *s = str_array[0];
107 gchar *d = groupbuf;
108
109 i = 0;
110 // avoid the - for negative amount
111 if( *s == '-')
112 {
113 *d++ = *s++;
114 length--;
115 }
116
117 // do the grouping
118 do
119 {
120 if( i!=0 && (length % 3) == 0 )
121 {
122 gchar *gc = cur->grouping_char;
123
124 while( *gc )
125 *d++ = *gc++;
126 }
127
128 *d++ = *s;
129 length--;
130 i++;
131 }
132 while (length && *s++ != '\0');
133 *d = 0;
134
135 monstr = g_strjoin(cur->decimal_char, groupbuf, str_array[1], NULL);
136
137 }
138
139 //debug
140 //g_print("**\nmystrfmon %.2f\n 0=%s\n 1=%s\n [%d]\n", value, str_array[0], str_array[1], length );
141 //g_print(" => %s :: %s\n", monstr, groupbuf);
142
143 g_strfreev(str_array);
144
145 // insert our formated number with symbol
146 g_snprintf(outstr, outlen, cur->monfmt, monstr);
147
148 g_free(monstr);
149
150 return size;
151 }
152
153
154 static Currency *hb_strfmon_check(gchar *outstr, guint32 kcur)
155 {
156 Currency *cur = da_cur_get(kcur);
157
158 if(cur == NULL)
159 g_stpcpy(outstr, "error");
160 return cur;
161 }
162
163
164 void hb_strfmon(gchar *outstr, gint outlen, gdouble value, guint32 kcur)
165 {
166 gchar formatd_buf[G_ASCII_DTOSTR_BUF_SIZE];
167 Currency *cur;
168 gdouble monval;
169
170 cur = hb_strfmon_check(outstr, kcur);
171 if(cur != NULL)
172 {
173 monval = arrondi(value, cur->frac_digits);
174 g_ascii_formatd(formatd_buf, G_ASCII_DTOSTR_BUF_SIZE-1, cur->format, monval);
175 real_mystrfmoncurr(outstr, outlen, formatd_buf, cur);
176 }
177 }
178
179
180 void hb_strfmon_int(gchar *outstr, gint outlen, gdouble value, guint32 kcur)
181 {
182 gchar formatd_buf[G_ASCII_DTOSTR_BUF_SIZE];
183 Currency *cur;
184 gdouble monval;
185
186 cur = hb_strfmon_check(outstr, kcur);
187 if(cur != NULL)
188 {
189 monval = arrondi(value, cur->frac_digits);
190 g_ascii_formatd(formatd_buf, sizeof (formatd_buf), "%0.f", monval);
191 real_mystrfmoncurr(outstr, outlen, formatd_buf, cur);
192 }
193 }
194
195 //todo: remove this
196 // test for currecny choose dialog
197 void mystrfmoncurrcurr(gchar *outstr, gint outlen, gdouble value, Currency *cur)
198 {
199 gchar formatd_buf[G_ASCII_DTOSTR_BUF_SIZE];
200 gdouble monval;
201
202 monval = arrondi(value, cur->frac_digits);
203 g_ascii_formatd(formatd_buf, G_ASCII_DTOSTR_BUF_SIZE-1, cur->format, monval);
204 real_mystrfmoncurr(outstr, outlen, formatd_buf, cur);
205 }
206 */
207
208
209
210
211 /* obsolete before currencies */
212 gint real_mystrfmon(gchar *outstr, gint outlen, gchar *buf1, struct CurrencyFmt *cur)
213 {
214 gint size = 0;
215 gchar groupbuf[G_ASCII_DTOSTR_BUF_SIZE];
216 gchar **str_array;
217 guint i, length;
218 gchar *monstr;
219
220 str_array = g_strsplit(buf1, ".", 2);
221 monstr = NULL;
222
223 length = strlen(str_array[0]);
224
225 if( cur->grouping_char == NULL || !strlen(cur->grouping_char) )
226 {
227 monstr = g_strjoinv(cur->decimal_char, str_array);
228 }
229 else
230 {
231 gchar *s = str_array[0];
232 gchar *d = groupbuf;
233
234 i = 0;
235 // avoid the - for negative amount
236 if( *s == '-')
237 {
238 *d++ = *s++;
239 length--;
240 }
241
242 // do the grouping
243 do
244 {
245 if( i!=0 && (length % 3) == 0 )
246 {
247 gchar *gc = cur->grouping_char;
248
249 while( *gc )
250 *d++ = *gc++;
251 }
252
253 *d++ = *s;
254 length--;
255 i++;
256 }
257 while (length && *s++ != '\0');
258 *d = 0;
259
260 monstr = g_strjoin(cur->decimal_char, groupbuf, str_array[1], NULL);
261
262 }
263
264 //debug
265 //g_print("**\nmystrfmon %.2f\n 0=%s\n 1=%s\n [%d]\n", value, str_array[0], str_array[1], length );
266 //g_print(" => %s :: %s\n", monstr, groupbuf);
267
268 g_strfreev(str_array);
269
270 // insert our formated number with symbol
271 g_snprintf(outstr, outlen, cur->monfmt, monstr);
272
273 g_free(monstr);
274
275 return size;
276 }
277
278
279 gint mystrfmon(gchar *outstr, gint outlen, gdouble value, gboolean minor)
280 {
281 struct CurrencyFmt *cur;
282 gchar formatd_buf[G_ASCII_DTOSTR_BUF_SIZE];
283 gdouble monval;
284 gint size;
285
286 cur = minor ? &PREFS->minor_cur : &PREFS->base_cur;
287
288 monval = arrondi(value, cur->frac_digits);
289
290 if(minor == TRUE)
291 {
292 monval = (value * PREFS->euro_value);
293 monval += (monval > 0.0) ? 0.005 : -0.005;
294 monval = (fint(monval * 100) / 100);
295 }
296
297 //DB( g_print("fmt = %s\n", cur->format) );
298
299 g_ascii_formatd(formatd_buf, sizeof (formatd_buf), cur->format, monval);
300
301 size = real_mystrfmon(outstr, outlen, formatd_buf, cur);
302
303 return size;
304 }
305
306
307
308
309
310 gint mystrfmon_int(gchar *outstr, gint outlen, gdouble value, gboolean minor)
311 {
312 struct CurrencyFmt *cur;
313 gchar formatd_buf[G_ASCII_DTOSTR_BUF_SIZE];
314 gdouble monval = value;
315 gint size;
316
317 cur = minor ? &PREFS->minor_cur : &PREFS->base_cur;
318
319 if(minor == TRUE)
320 {
321 monval = (value * PREFS->euro_value);
322 monval += (monval > 0.0) ? 0.005 : -0.005;
323 monval = (fint(monval * 100) / 100);
324 }
325
326 g_ascii_formatd(formatd_buf, sizeof (formatd_buf), "%0.f", monval);
327
328 size = real_mystrfmon(outstr, outlen, formatd_buf, cur);
329
330 return size;
331 }
332
333
334
335
336 /* end obsolete call */
337
338
339 gchar *get_normal_color_amount(gdouble value)
340 {
341 gchar *color = NULL;
342
343 //fix: 400483
344 value = arrondi(value, 2);
345
346 if(value != 0.0 && PREFS->custom_colors == TRUE)
347 {
348 color = (value > 0.0) ? PREFS->color_inc : PREFS->color_exp;
349 }
350 return color;
351 }
352
353
354 gchar *get_minimum_color_amount(gdouble value, gdouble minvalue)
355 {
356 gchar *color = NULL;
357
358 //fix: 400483
359 value = arrondi(value, 2);
360 if(value != 0.0 && PREFS->custom_colors == TRUE)
361 {
362 color = (value > 0.0) ? PREFS->color_inc : PREFS->color_exp;
363 if( value < minvalue)
364 color = PREFS->color_warn;
365 }
366 return color;
367 }
368
369 void hb_label_set_amount(GtkLabel *label, gdouble value, gboolean minor)
370 {
371 gchar strbuffer[G_ASCII_DTOSTR_BUF_SIZE];
372
373 mystrfmon(strbuffer, G_ASCII_DTOSTR_BUF_SIZE-1, value, minor);
374 gtk_label_set_text(GTK_LABEL(label), strbuffer);
375
376 }
377
378
379 /*
380 ** format/color and set a label text with a amount value
381 */
382 void hb_label_set_colvalue(GtkLabel *label, gdouble value, gboolean minor)
383 {
384 gchar strbuffer[G_ASCII_DTOSTR_BUF_SIZE];
385 gchar *markuptxt;
386 gchar *color = NULL;
387
388 mystrfmon(strbuffer, G_ASCII_DTOSTR_BUF_SIZE-1, value, minor);
389
390 if(value != 0.0 && PREFS->custom_colors == TRUE)
391 {
392 color = get_normal_color_amount(value);
393
394 //g_print("color: %s\n", color);
395
396 if(color)
397 {
398 markuptxt = g_strdup_printf("<span color='%s'>%s</span>", color, strbuffer);
399 gtk_label_set_markup(GTK_LABEL(label), markuptxt);
400 g_free(markuptxt);
401 return;
402 }
403 }
404
405 gtk_label_set_text(GTK_LABEL(label), strbuffer);
406
407 }
408
409 /*
410 void hb_label_set_colvaluecurr(GtkLabel *label, gdouble value, guint32 currkey)
411 {
412 gchar strbuffer[G_ASCII_DTOSTR_BUF_SIZE];
413 gchar *markuptxt;
414 gchar *color = NULL;
415
416 hb_strfmon(strbuffer, G_ASCII_DTOSTR_BUF_SIZE-1, value, currkey);
417
418 if(value != 0.0 && PREFS->custom_colors == TRUE)
419 {
420 color = get_normal_color_amount(value);
421
422 if(color)
423 {
424 markuptxt = g_strdup_printf("<span color='%s'>%s</span>", color, strbuffer);
425 gtk_label_set_markup(GTK_LABEL(label), markuptxt);
426 g_free(markuptxt);
427 return;
428 }
429 }
430
431 gtk_label_set_text(GTK_LABEL(label), strbuffer);
432
433 }
434 */
435
436
437 /*
438 void get_range_minmax(guint32 refdate, gint range, guint32 *mindate, guint32 *maxdate)
439 {
440 GDate *date;
441 guint month, year, qnum;
442
443 if(refdate > *maxdate)
444 refdate = *maxdate;
445
446 date = g_date_new_julian(refdate);
447 month = g_date_get_month(date);
448 year = g_date_get_year(date);
449 qnum = ((month-1)/3)+1;
450
451 DB( g_print("m=%d, y=%d, qnum=%d\n", month, year, qnum) );
452
453 switch( range )
454 {
455 case 0: // this month
456 g_date_set_day(date, 1);
457 *mindate = g_date_get_julian(date);
458 g_date_add_days(date, g_date_get_days_in_month(month, year)-1);
459 *maxdate = g_date_get_julian(date);
460 break;
461
462 case 1: // last month
463 g_date_set_day(date, 1);
464 g_date_subtract_months(date, 1);
465 *mindate = g_date_get_julian(date);
466 month = g_date_get_month(date);
467 year = g_date_get_year(date);
468 g_date_add_days(date, g_date_get_days_in_month(month, year)-1);
469 *maxdate = g_date_get_julian(date);
470 break;
471
472 case 2: // this quarter
473 g_date_set_day(date, 1);
474 g_date_set_month(date, (qnum-1)*3+1);
475 *mindate = g_date_get_julian(date);
476 g_date_add_months(date, 3);
477 g_date_subtract_days(date, 1);
478 *maxdate = g_date_get_julian(date);
479 break;
480
481 case 3: // last quarter
482 g_date_set_day(date, 1);
483 g_date_set_month(date, (qnum-1)*3+1);
484 g_date_subtract_months(date, 3);
485 *mindate = g_date_get_julian(date);
486 g_date_add_months(date, 3);
487 g_date_subtract_days(date, 1);
488 *maxdate = g_date_get_julian(date);
489 break;
490
491 case 4: // this year
492 g_date_set_dmy(date, 1, 1, year);
493 *mindate = g_date_get_julian(date);
494 g_date_set_dmy(date, 31, 12, year);
495 *maxdate = g_date_get_julian(date);
496 break;
497
498 // separator
499
500 case 6: // last 30 days
501 *mindate = refdate - 30;
502 *maxdate = refdate;
503 break;
504
505 case 7: // last 60 days
506 *mindate = refdate - 60;
507 *maxdate = refdate;
508 break;
509
510 case 8: // last 90 days
511 *mindate = refdate - 90;
512 *maxdate = refdate;
513 break;
514
515 case 9: // last 12 months
516 g_date_subtract_months(date, 12);
517 *mindate = g_date_get_julian(date);
518 *maxdate = refdate;
519 break;
520
521
522 }
523 g_date_free(date);
524 }
525 */
526
527 /*
528 ** String utility
529 */
530
531
532 /*
533 * compare 2 utf8 string
534 */
535 gint hb_string_utf8_compare(gchar *s1, gchar *s2)
536 {
537 gint retval = 0;
538 gchar *ns1, *ns2;
539
540 if (s1 == NULL || s2 == NULL)
541 {
542 if (s1 == NULL && s2 == NULL)
543 goto end;
544
545 retval = (s1 == NULL) ? -1 : 1;
546 }
547 else
548 {
549 //#1325969
550 //retval = g_utf8_collate(s1 != NULL ? s1 : "", s2 != NULL ? s2 : "");
551 ns1 = g_utf8_normalize(s1, -1, G_NORMALIZE_DEFAULT);
552 ns2 = g_utf8_normalize(s2, -1, G_NORMALIZE_DEFAULT);
553 retval = strcasecmp(ns1, ns2);
554 g_free(ns2);
555 g_free(ns1);
556 }
557 end:
558 return retval;
559 }
560
561
562 void hb_string_strip_crlf(gchar *str)
563 {
564 gchar *p = str;
565
566 if(str)
567 {
568 while( *p )
569 {
570 if( *p == '\n' || *p == '\r')
571 {
572 *p = '\0';
573 }
574 p++;
575 }
576 }
577 }
578
579 gchar*
580 hb_strdup_nobrackets (const gchar *str)
581 {
582 const gchar *s;
583 gchar *new_str, *d;
584 gsize length;
585
586 if (str)
587 {
588 length = strlen (str) + 1;
589 new_str = g_new (char, length);
590 s = str;
591 d = new_str;
592 while(*s != '\0')
593 {
594 if( *s != '[' && *s != ']' )
595 *d++ = *s;
596 s++;
597 }
598 *d = '\0';
599 }
600 else
601 new_str = NULL;
602
603 return new_str;
604 }
605
606
607 static gboolean
608 hb_date_parser_get_nums(gchar *string, gint *n1, gint *n2, gint *n3)
609 {
610 gboolean retval;
611 gchar **str_array;
612
613 //DB( g_print("(qif) hb_qif_parser_get_dmy for '%s'\n", string) );
614
615 retval = FALSE;
616 str_array = g_strsplit (string, "/", 3);
617 if( g_strv_length( str_array ) != 3 )
618 {
619 g_strfreev (str_array);
620 str_array = g_strsplit (string, ".", 3);
621 // fix 371381
622 //todo test
623 if( g_strv_length( str_array ) != 3 )
624 {
625 g_strfreev (str_array);
626 str_array = g_strsplit (string, "-", 3);
627 }
628 }
629
630 if( g_strv_length( str_array ) == 3 )
631 {
632 *n1 = atoi(str_array[0]);
633 *n2 = atoi(str_array[1]);
634 *n3 = atoi(str_array[2]);
635 retval = TRUE;
636 }
637
638 g_strfreev (str_array);
639
640 return retval;
641 }
642
643
644 guint32 hb_date_get_julian(gchar *string, gint datefmt)
645 {
646 GDate *date;
647 gint n1, n2, n3, d, m, y;
648 guint32 julian = 0;
649
650 DB( g_print("hb_date_get_julian: %s, %d\n", string, datefmt) );
651
652 if( hb_date_parser_get_nums(string, &n1, &n2, &n3) )
653 {
654 DB( g_print("-> %d %d %d\n", n1, n2, n3) );
655
656 switch(datefmt)
657 {
658 case PRF_DATEFMT_MDY:
659 d = n2;
660 m = n1;
661 y = n3;
662 break;
663 case PRF_DATEFMT_DMY:
664 d = n1;
665 m = n2;
666 y = n3;
667 break;
668 default:
669 case PRF_DATEFMT_YMD:
670 d = n3;
671 m = n2;
672 y = n1;
673 break;
674 }
675
676 //correct for 2 digits year
677 if(y < 1970)
678 {
679 if(y < 60)
680 y += 2000;
681 else
682 y += 1900;
683 }
684
685 DB( g_print("-> %d %d %d\n", d, m, y) );
686
687 if(d <= 31 && m <= 12)
688 {
689 date = g_date_new();
690 g_date_set_dmy(date, d, m, y);
691 if( g_date_valid (date) )
692 {
693 julian = g_date_get_julian (date);
694 }
695 g_date_free(date);
696 }
697 }
698
699 return julian;
700 }
701
702
703 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
704
705
706 gchar *hb_filename_new_with_extention(gchar *filename, const gchar *extension)
707 {
708 gchar *dirname;
709 gchar *basename;
710 gchar *newbasename;
711 gchar *newfilename;
712 gchar **str_array;
713
714 dirname = g_path_get_dirname (filename);
715 basename = g_path_get_basename(filename);
716 str_array = g_strsplit(basename, ".", 0);
717 newbasename = g_strdup_printf("%s.%s", str_array[0], extension);
718 newfilename = g_build_filename(dirname, newbasename, NULL);
719
720 g_strfreev(str_array);
721 g_free(basename);
722 g_free(dirname);
723 g_free(newbasename);
724
725 return newfilename;
726 }
727
728
729 /* file backup, qif export */
730
731
732 /*gchar *homebank_filename_without_extention(gchar *path)
733 {
734 gchar *basename;
735 gchar *newname;
736 gchar **str_array;
737
738 basename = g_path_get_basename(path);
739
740 str_array = g_strsplit(basename, ".", 0);
741
742 newname = g_strdup(str_array[0]);
743
744 g_strfreev(str_array);
745 g_free(basename);
746
747 return newname;
748 }*/
749
750 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =*/
751
752
753 static gboolean hb_string_isdate(gchar *str)
754 {
755 gint d, m, y;
756
757 return(hb_date_parser_get_nums(str, &d, &m, &y));
758 }
759
760
761 static gboolean hb_string_isdigit(gchar *str)
762 {
763 gboolean valid = TRUE;
764 while(*str && valid)
765 valid = g_ascii_isdigit(*str++);
766 return valid;
767 }
768
769 /*
770 static gboolean hb_string_isprint(gchar *str)
771 {
772 gboolean valid = TRUE;
773 while(*str && valid)
774 valid = g_ascii_isprint(*str++);
775 return valid;
776 }
777 */
778
779
780
781 static gboolean hb_string_isprint(gchar *str)
782 {
783 gboolean valid = TRUE;
784 gchar *p;
785 gunichar c;
786
787 if(g_utf8_validate(str, -1, NULL))
788 {
789 p = str;
790 while(*p && valid)
791 {
792 c = g_utf8_get_char(p);
793 valid = g_unichar_isprint(c);
794 p = g_utf8_next_char(p);
795 }
796 }
797 return valid;
798 }
799
800
801 gboolean hb_string_csv_valid(gchar *str, guint nbcolumns, gint *csvtype)
802 {
803 gchar **str_array;
804 gboolean valid = TRUE;
805 guint i;
806 extern int errno;
807
808 #if MYDEBUG == 1
809 gchar *type[5] = { "string", "date", "int", "double" };
810 gint lasttype;
811 #endif
812
813 DB( g_print("\n** hb_string_csv_valid: init %d\n", valid) );
814
815 hb_string_strip_crlf(str);
816 str_array = g_strsplit (str, ";", 0);
817
818 DB( g_print(" -> length %d, nbcolumns %d\n", g_strv_length( str_array ), nbcolumns) );
819
820 if( g_strv_length( str_array ) != nbcolumns )
821 {
822 valid = FALSE;
823 goto csvend;
824 }
825
826 for(i=0;i<nbcolumns;i++)
827 {
828 #if MYDEBUG == 1
829 lasttype = csvtype[i];
830 #endif
831
832 if(valid == FALSE)
833 {
834 DB( g_print(" -> fail on column %d, type: %s\n", i, type[lasttype]) );
835 break;
836 }
837
838 DB( g_print(" -> control column %d, type: %d, valid: %d '%s'\n", i, lasttype, valid, str_array[i]) );
839
840 switch( csvtype[i] )
841 {
842 case CSV_DATE:
843 valid = hb_string_isdate(str_array[i]);
844 break;
845 case CSV_STRING:
846 valid = hb_string_isprint(str_array[i]);
847 break;
848 case CSV_INT:
849 valid = hb_string_isdigit(str_array[i]);
850 break;
851 case CSV_DOUBLE :
852 g_ascii_strtod(str_array[i], NULL);
853 //todo : see this errno
854 if( errno )
855 {
856 DB( g_print("errno: %d\n", errno) );
857 valid = FALSE;
858 }
859 break;
860 }
861 }
862
863 csvend:
864 g_strfreev (str_array);
865
866 DB( g_print(" --> return %d\n", valid) );
867
868 return valid;
869 }
870
871
872 void hb_print_date(guint32 jdate, gchar *label)
873 {
874 gchar buffer1[128];
875 GDate *date;
876
877 date = g_date_new_julian(jdate);
878 g_date_strftime (buffer1, 128-1, "%x", date);
879 g_date_free(date);
880 g_print(" - %s %s\n", label != NULL ? label:"date is", buffer1);
881 }
882
883
884
885 /*
886 ** parse a string an retrieve an iso date (dd-mm-yy(yy) or dd/mm/yy(yy))
887 **
888 */
889 /* obsolete 4.5
890 guint32 hb_date_get_julian_parse(gchar *str)
891 {
892 gchar **str_array = NULL;
893 GDate *date;
894 guint d, m, y;
895 guint32 julian = GLOBALS->today;
896
897 // try with - separator
898 if( g_strrstr(str, "-") != NULL )
899 {
900 str_array = g_strsplit (str, "-", 3);
901 }
902 else
903 {
904 if( g_strrstr(str, "/") != NULL )
905 {
906 str_array = g_strsplit (str, "/", 3);
907 }
908 }
909
910 if( g_strv_length( str_array ) == 3 )
911 {
912 d = atoi(str_array[0]);
913 m = atoi(str_array[1]);
914 y = atoi(str_array[2]);
915
916 //correct for 2 digits year
917 if(y < 1970)
918 {
919 if(y < 60)
920 y += 2000;
921 else
922 y += 1900;
923 }
924
925 //todo: here if month is > 12 then the format is probably mm/dd/yy(yy)
926 //or maybe check with g_date_valid_julian(julian)
927
928
929
930 date = g_date_new();
931 g_date_set_dmy(date, d, m, y);
932 julian = g_date_get_julian (date);
933 g_date_free(date);
934
935 DB( g_print("date: %s :: %d %d %d :: %d\n", str, d, m, y, julian ) );
936
937 }
938
939 g_strfreev (str_array);
940
941 return julian;
942 }
943 */
944
945 /* -------------------- */
946
947 #if MYDEBUG == 1
948
949 /*
950 ** hex memory dump
951 */
952 #define MAX_DUMP 16
953 void hex_dump(guchar *ptr, guint length)
954 {
955 guchar ascii[MAX_DUMP+4];
956 guint i,j;
957
958 g_print("**hex_dump - %d bytes\n", length);
959
960 for(i=0;i<length;)
961 {
962 g_print("%08x: ", (guint)ptr+i);
963
964 for(j=0;j<MAX_DUMP;j++)
965 {
966 if(i >= length) break;
967
968 //store ascii value
969 if(ptr[i] >= 32 && ptr[i] <= 126)
970 ascii[j] = ptr[i];
971 else
972 ascii[j] = '.';
973
974 g_print("%02x ", ptr[i]);
975 i++;
976 }
977 //newline
978 ascii[j] = 0;
979 g_print(" '%s'\n", ascii);
980 }
981 }
982
983 #endif
This page took 0.076141 seconds and 4 git commands to generate.