mp3splt-gtk
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-2013 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 
44 gint 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 
62 gint 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,
87  _("Processing file '%s' ..."), filename);
88  put_status_message(fname_status, ui);
89  if (fname_status)
90  {
91  free(fname_status);
92  fname_status = NULL;
93  }
94 }
95 
99 {
100  if (filename == NULL)
101  {
102  return;
103  }
104 
105  gint index = strlen(filename) - 1;
106  while (index >= 0)
107  {
108  if (filename[index] == '\n' ||
109  filename[index] == '\r')
110  {
111  filename[index] = '\0';
112  }
113  else if (filename[index] != '\0')
114  {
115  break;
116  }
117 
118  index--;
119  }
120 }
121 
132 gchar *transform_to_utf8(gchar *text, gint free_or_not, gint *must_be_freed)
133 {
134  gchar *temp;
135 
136  gsize bytes_read;
137  gsize bytes_written;
138 
139  if (!(g_utf8_validate (text, -1,NULL)) && (text != NULL))
140  {
141  temp = g_convert(text, -1, "UTF-8", "ISO-8859-1", &bytes_read, &bytes_written, NULL);
142  if (free_or_not)
143  {
144  g_free(text);
145  }
146 
147  *must_be_freed = TRUE;
148 
149  return temp;
150  }
151 
152  *must_be_freed = FALSE;
153 
154  return text;
155 }
156 
157 void build_path(GString *path, const gchar *dir, const gchar *filename)
158 {
159 #ifdef __WIN32__
160  g_string_assign(path, ".");
161  g_string_append(path, G_DIR_SEPARATOR_S);
162  g_string_append(path, filename);
163 #else
164  if (strlen(dir) == 0)
165  {
166  g_string_assign(path, filename);
167  }
168  else
169  {
170  g_string_assign(path, dir);
171  g_string_append(path, G_DIR_SEPARATOR_S);
172  g_string_append(path, filename);
173  }
174 #endif
175 }
176 
177 gboolean double_equals(gdouble double_to_compare, gdouble compared_value)
178 {
179  return fabs(double_to_compare - compared_value) < DOUBLE_PRECISION;
180 }
181 
182 
void remove_end_slash_n_r_from_filename(char *filename)
Removes trailing \r or \n characters from a filename.
Definition: utilities.c:98
gint file_exists(const gchar *fname)
check if specified file exists
Definition: utilities.c:62
void print_processing_file(gchar *filename, ui_state *ui)
Issues the message &quot;Processing file &lt;filename&gt;&quot; into the message bar.
Definition: utilities.c:82
gchar * transform_to_utf8(gchar *text, gint free_or_not, gint *must_be_freed)
transform text to utf8
Definition: utilities.c:132
void put_status_message(const gchar *text, ui_state *ui)
Output a info message to the status message bar.
Definition: main_window.c:264
gint directory_exists(const gchar *directory)
check if specified directory exists
Definition: utilities.c:44