mp3splt-gtk: src/utilities.c Source File

mp3splt-gtk 0.9.3.1519

Loading...
Searching...
No Matches
utilities.c
Go to the documentation of this file.
1/**********************************************************
2 *
3 * mp3splt-gtk -- utility based on mp3splt,
4 * for mp3/ogg splitting without decoding
5 *
6 * Copyright: (C) 2005-2014 Alexandru Munteanu
7 * Contact: m@ioalex.net
8 *
9 * http://mp3splt.sourceforge.net/
10 *
11 *********************************************************/
12
13/**********************************************************
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
28 * USA.
29 *
30 *********************************************************/
31
32/*!********************************************************
33 * \file
34 * miscellaneous utilities
35 *
36 * Miscellaneous utilities like the check if a string may
37 * contain a valid file- or directory name.
38 ********************************************************/
39
40#include "utilities.h"
41
44gint directory_exists(const gchar *directory)
45{
46 if (directory == NULL)
47 {
48 return FALSE;
49 }
50
51 struct stat buffer;
52 gint status = g_stat(directory, &buffer);
53
54 if (status == 0 && S_ISDIR(buffer.st_mode) != 0)
55 return TRUE;
56
57 return FALSE;
58}
59
62gint file_exists(const gchar *fname)
63{
64 if (fname == NULL)
65 {
66 return FALSE;
67 }
68
69 struct stat buffer;
70 gint status = g_stat(fname, &buffer);
71
72 if (status == 0 && S_ISREG(buffer.st_mode) != 0)
73 return TRUE;
74
75 return FALSE;
76}
77
82 void print_processing_file(gchar *filename, ui_state *ui)
83{
84 gint fname_status_size = (strlen(filename) + 255);
85 gchar *fname_status = g_malloc(sizeof(char) * fname_status_size);
86 g_snprintf(fname_status, fname_status_size, _("Processing file '%s' ..."), filename);
87 put_status_message_in_idle(fname_status, ui);
88 if (fname_status)
89 {
90 free(fname_status);
91 fname_status = NULL;
92 }
93}
94
97 void remove_end_slash_n_r_from_filename(char *filename)
98{
99 if (filename == NULL)
100 {
101 return;
102 }
103
104 gint index = strlen(filename) - 1;
105 while (index >= 0)
106 {
107 if (filename[index] == '\n' ||
108 filename[index] == '\r')
109 {
110 filename[index] = '\0';
111 }
112 else if (filename[index] != '\0')
113 {
114 break;
115 }
116
117 index--;
118 }
119}
120
131gchar *transform_to_utf8(gchar *text, gint free_or_not, gint *must_be_freed)
132{
133 gchar *temp;
134
135 gsize bytes_read;
136 gsize bytes_written;
137
138 if (!(g_utf8_validate (text, -1,NULL)) && (text != NULL))
139 {
140 temp = g_convert(text, -1, "UTF-8", "ISO-8859-1", &bytes_read, &bytes_written, NULL);
141 if (free_or_not)
142 {
143 g_free(text);
144 }
145
146 *must_be_freed = TRUE;
147
148 return temp;
149 }
150
151 *must_be_freed = FALSE;
152
153 return text;
154}
155
156void build_path(GString *path, const gchar *dir, const gchar *filename)
157{
158#ifdef __WIN32__
159 g_string_assign(path, ".");
160 g_string_append(path, G_DIR_SEPARATOR_S);
161 g_string_append(path, filename);
162#else
163 if (strlen(dir) == 0)
164 {
165 g_string_assign(path, filename);
166 }
167 else
168 {
169 g_string_assign(path, dir);
170 g_string_append(path, G_DIR_SEPARATOR_S);
171 g_string_append(path, filename);
172 }
173#endif
174}
175
176gboolean double_equals(gdouble double_to_compare, gdouble compared_value)
177{
178 return fabs(double_to_compare - compared_value) < DOUBLE_PRECISION;
179}
180
181//points and tags utilities
182
183 points_and_tags *new_points_and_tags()
184{
185 points_and_tags *pat = g_malloc(sizeof(points_and_tags));
186 pat->splitpoints = g_ptr_array_new();
187 pat->tags = g_ptr_array_new();
188 return pat;
189}
190
191void free_points_and_tags(points_and_tags **pat)
192{
193 if (!pat || !*pat) { return; }
194 g_ptr_array_free((*pat)->splitpoints, SPLT_TRUE);
195 (*pat)->splitpoints = NULL;
196 g_ptr_array_free((*pat)->tags, SPLT_TRUE);
197 (*pat)->tags = NULL;
198 g_free(*pat);
199 *pat = NULL;
200}
201
202void append_point_to_pat(splt_point *point, points_and_tags *pat)
203{
204 g_ptr_array_add(pat->splitpoints, point);
205}
206
207void append_tags_to_pat(splt_tags *tags, points_and_tags *pat)
208{
209 g_ptr_array_add(pat->tags, tags);
210}
211
points_and_tags
Definition ui_types.h:99
ui_state
Definition ui_types.h:509
print_processing_file
void print_processing_file(gchar *filename, ui_state *ui)
Issues the message "Processing file <filename>" into the message bar.
Definition utilities.c:82
transform_to_utf8
gchar * transform_to_utf8(gchar *text, gint free_or_not, gint *must_be_freed)
transform text to utf8
Definition utilities.c:131
file_exists
gint file_exists(const gchar *fname)
check if specified file exists
Definition utilities.c:62
remove_end_slash_n_r_from_filename
void remove_end_slash_n_r_from_filename(char *filename)
Removes trailing \r or \n characters from a filename.
Definition utilities.c:97
directory_exists
gint directory_exists(const gchar *directory)
check if specified directory exists
Definition utilities.c:44