]> git.sur5r.net Git - glabels/blob - glabels2/src/util.c
124d492c86bcc23bfa47ad8054cd2895e5be4df1
[glabels] / glabels2 / src / util.c
1 /*
2  *  (GLABELS) Label and Business Card Creation program for GNOME
3  *
4  *  util.c:  various small utility functions
5  *
6  *  Copyright (C) 2001  Jim Evins <evins@snaught.com>.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22
23 #include <config.h>
24
25 #include <string.h>
26 #include <glib.h>
27 #include <libgnome/gnome-util.h>
28 #include <math.h>
29 #include <libgnomeprint/gnome-font.h>
30
31 #include "util.h"
32
33 #define FRAC_DELTA 0.00005
34
35 \f
36 /****************************************************************************/
37 /* Get '~/.glabels' directory path.                                         */
38 /****************************************************************************/
39 gchar *
40 gl_util_get_home_data_dir (void)
41 {
42         gchar *dir = gnome_util_prepend_user_home (".glabels");
43  
44         /* Try to create ~/.glabels directory.  If it exists, no problem. */
45         mkdir (dir, 0775);
46  
47         return dir;
48 }
49
50
51 \f
52 /****************************************************************************/
53 /* Append ".glabels" extension to filename if needed.                       */
54 /****************************************************************************/
55 gchar *
56 gl_util_add_extension (const gchar *orig_filename)
57 {
58         gchar *new_filename, *extension;
59
60         extension = strrchr (orig_filename, '.');
61         if (extension == NULL) {
62                 new_filename = g_strconcat (orig_filename, ".glabels", NULL);
63         } else {
64                 if (g_strcasecmp (extension, ".glabels") != 0) {
65                         new_filename =
66                             g_strconcat (orig_filename, ".glabels", NULL);
67                 } else {
68                         new_filename = g_strdup (orig_filename);
69                 }
70         }
71
72         return new_filename;
73 }
74
75 /****************************************************************************/
76 /* Remove ".glabels" extension from filename if needed.                     */
77 /****************************************************************************/
78 gchar *
79 gl_util_remove_extension (const gchar *orig_filename)
80 {
81         gchar *new_filename, *extension;
82
83         new_filename = g_strdup (orig_filename);
84
85         extension = strrchr (new_filename, '.');
86         if (extension != NULL) {
87                 if (g_strcasecmp (extension, ".glabels") == 0) {
88                         *extension = 0; /* truncate string, rm extension */
89                 }
90         }
91
92         return new_filename;
93 }
94
95 /****************************************************************************/
96 /* Make sure we have an absolute path to filename.                          */
97 /****************************************************************************/
98 gchar *
99 gl_util_make_absolute (const gchar *filename)
100 {
101         gchar *pwd, *absolute_filename;
102
103         if (g_path_is_absolute (filename)) {
104                 absolute_filename = g_strdup (filename);
105         } else {
106                 pwd = g_get_current_dir ();
107                 absolute_filename = g_build_filename (pwd, filename, NULL);
108                 g_free (pwd);
109         }
110
111         return absolute_filename;
112 }
113
114 /****************************************************************************/
115 /* Create fractional representation of number, if possible.                 */
116 /****************************************************************************/
117 gchar *
118 gl_util_fraction (gdouble x)
119 {
120         static gdouble denom[] = { 1., 2., 3., 4., 8., 16., 32., 0. };
121         gint i;
122         gdouble product, remainder;
123         gint n, d;
124
125         for ( i=0; denom[i] != 0.0; i++ ) {
126                 product = x * denom[i];
127                 remainder = fabs(product - ((gint)(product+0.5)));
128                 if ( remainder < FRAC_DELTA ) break;
129         }
130
131         if ( denom[i] == 0.0 ) {
132                 /* None of our denominators work. */
133                 return g_strdup_printf ("%.5g", x);
134         }
135         if ( denom[i] == 1.0 ) {
136                 /* Simple integer. */
137                 return g_strdup_printf ("%d", (gint)x);
138         }
139         n = (gint)( x * denom[i] + 0.5 );
140         d = (gint)denom[i];
141         if ( n > d ) {
142                 return g_strdup_printf ("%d_%d/%d", (n/d), (n%d), d);
143         } else {
144                 return g_strdup_printf ("%d/%d", (n%d), d);
145         }
146 }
147
148 /****************************************************************************/
149 /* Utilities to deal with GTK_JUSTIFICATION types.                          */
150 /****************************************************************************/
151 const gchar *
152 gl_util_just_to_string (GtkJustification just)
153 {
154         switch (just) {
155         case GTK_JUSTIFY_LEFT:
156                 return "Left";
157         case GTK_JUSTIFY_CENTER:
158                 return "Center";
159         case GTK_JUSTIFY_RIGHT:
160                 return "Right";
161         default:
162                 return "?";
163         }
164 }
165
166 GtkJustification
167 gl_util_string_to_just (const gchar *string)
168 {
169
170         if (g_strcasecmp (string, "Left") == 0) {
171                 return GTK_JUSTIFY_LEFT;
172         } else if (g_strcasecmp (string, "Center") == 0) {
173                 return GTK_JUSTIFY_CENTER;
174         } else if (g_strcasecmp (string, "Right") == 0) {
175                 return GTK_JUSTIFY_RIGHT;
176         } else {
177                 return GTK_JUSTIFY_LEFT;
178         }
179
180 }
181
182 /****************************************************************************/
183 /* Utilities to deal with GNOME_FONT_WEIGHT types                           */
184 /****************************************************************************/
185 const gchar *
186 gl_util_weight_to_string (GnomeFontWeight weight)
187 {
188         switch (weight) {
189         case GNOME_FONT_BOOK:
190                 return "Regular";
191         case GNOME_FONT_BOLD:
192                 return "Bold";
193         default:
194                 return "?";
195         }
196 }
197
198 GnomeFontWeight
199 gl_util_string_to_weight (const gchar *string)
200 {
201
202         if (g_strcasecmp (string, "Regular") == 0) {
203                 return GNOME_FONT_BOOK;
204         } else if (g_strcasecmp (string, "Bold") == 0) {
205                 return GNOME_FONT_BOLD;
206         } else {
207                 return GNOME_FONT_BOOK;
208         }
209
210 }
211