File: lib.c

package info (click to toggle)
xcfa 5.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,300 kB
  • sloc: ansic: 47,148; sh: 4,380; makefile: 136; sed: 16
file content (561 lines) | stat: -rw-r--r-- 12,492 bytes parent folder | download | duplicates (4)
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
 /*
 * file      : lib/lib.c
 * project   : xcfa
 * with      : Gtk-2
 *
 * copyright : (C) 2003 - 2015 by Claude Bulin
 *
 * xcfa - Creation d'une base de programmation en langage C de type GNU avec les autotools
 * GNU General Public License
 *
 *  This file is part of XCFA.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * OLD ADRESS:
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * NEW ADRESS:
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */


#ifdef HAVE_CONFIG_H
	#include "../config.h"
#endif

#ifdef ENABLE_NLS
	#include <libintl.h>
	#define _(String) gettext (String)
#endif

#include <gtk/gtk.h>
#include <glib.h>
#include <glib/gstdio.h>

#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>

#include "lib.h"



OPTIONS_COMMAND_LINE OptionsCommandLine = {
	FALSE,		// gboolean	BoolVerboseMode
	FALSE,		// gboolean	BoolVersionMode
	FALSE		// gboolean	BoolHelpMode
};


/*
typedef struct {
	guint32 pixel;
	guint16 red;
	guint16 green;
	guint16 blue;
} GdkColor;
*/
GdkColor YellowColor = { 0, 63479,63479,48830 };

//
//
gchar *libutils_dup_chomp( gchar *p_str )
{
	gchar	*StrNew = g_strdup( p_str );
	gchar	*Ptr = StrNew;

	while( *Ptr ) {
		if( '\n' == *Ptr ) *Ptr = '\0';
		if( '\r' == *Ptr ) *Ptr = '\0';
		Ptr ++;
	}
	return( StrNew );
}
//
//
gchar *libutils_chomp( gchar *p_str )
{
	gchar	*Ptr = p_str;
	while( *Ptr ) {
		if( '\n' == *Ptr ) *Ptr = '\0';
		if( '\r' == *Ptr ) *Ptr = '\0';
		Ptr ++;
	}
	return( p_str );
}
//
//
gchar *libutils_get_prefix (gchar *PathNameFile)
{
	gchar	*Prefix = g_strdup (PathNameFile);
	gchar	*Ptr = NULL;

	if (NULL != (Ptr = strrchr (Prefix, '/'))) {
		*Ptr = '\0';
	}

	return (Prefix);
}
//
//
gboolean libutils_access_mode (gchar *PathNameFile)
{
	gboolean	FileAccessModeFileExist = access (PathNameFile, F_OK) == -1 ? FALSE : TRUE;
	return (FileAccessModeFileExist);
}
// CREATION D UN DOSSIER TEMPORAIRE
//
gchar *libutils_create_temporary_rep( gchar *SrcTmp, gchar *NewRepTmp )
{
	gchar	*Path = NULL;
	gint	NumPath = 0;

	// FIXED
	// Mon, 20 Jan 2014 16:33:36 +0100
	// Plusieurs instances de XCFA peuvent ĂȘtre activĂ©es sans collision pour les conversions et extractions
	//
	while( TRUE ) {
		Path = g_strdup_printf( "%s/%s_%d", SrcTmp, NewRepTmp, NumPath );
		if( TRUE == g_file_test( Path, G_FILE_TEST_IS_DIR ) ) {
			g_free( Path );
			Path = NULL;
		}
		else {
			if( TRUE == OptionsCommandLine.BoolVerboseMode )
				g_print( "CREATE TEMPORARY REP:\n\t%s\n", Path );
			g_mkdir_with_parents( Path, 0700 );
			break;
		}
		NumPath ++;
	}

	return( (gchar *)Path );
}
// SUPPRESSION D UN DOSSIER TEMPORAIRE
//
gchar *libutils_remove_temporary_rep (gchar *path_tmprep)
{
	pid_t  pid;

	if (path_tmprep != NULL) {
		if ((pid = fork ()) == 0) {
			execlp (
				"rm",
				"rm",
				"-rf",
				path_tmprep,
				NULL
				);
		}
		if( TRUE == OptionsCommandLine.BoolVerboseMode )
			g_print ("REMOVE TEMPORARY REP:\n\t%s\n", path_tmprep);
		g_free (path_tmprep);
		path_tmprep = NULL;
	}
	return ((gchar *)NULL);
}
// TRUE SI LE FICHIER EXISTE SINON FALSE
//
gboolean libutils_test_file_exist (gchar *File)
{
	/*struct stat status;
	gint	    ret;
	ret = stat (File, &status);
	return (ret > -1 && S_ISREG (status.st_mode));*/
	return (g_file_test (File, G_FILE_TEST_IS_REGULAR));
}
// RENVOIE LE PAHTNAME AVEC LA NOUVELLE EXTENTION
//
gchar *libutils_get_pathname_with_new_ext (gchar *PathName, gchar *NewExt)
{
	gchar	*NewPathName = g_strdup_printf ("%s0123456789", PathName);
	gchar	*Ptr = strrchr (NewPathName, '.');

	if (NULL == Ptr) {
		for (Ptr = NewPathName; *Ptr; Ptr ++);
		*Ptr ++ = '.';
		*Ptr = '\0';
	}
	else {
		Ptr ++;
	}
	strcpy (Ptr, NewExt);

	return (NewPathName);
}
// RENVOIE TRUE SI LE FICHIER EXISTE SINON FALSE
//
gboolean libutils_test_file_with_new_ext_exist (gchar *PathName, gchar *NewExt)
{
	gchar		*NewPathName = libutils_get_pathname_with_new_ext (PathName, NewExt);
	gboolean	BoolFileExist;

	BoolFileExist = libutils_test_file_exist (NewPathName);

	g_free (NewPathName);
	NewPathName = NULL;

	return (BoolFileExist);
}
// TRUE SI LE DOSSIER EXISTE SINON FALSE
//
gboolean libutils_test_dir_exist (gchar *File)
{
	/*struct stat status;
	gint	    ret;
	ret = stat (File, &status);
	return (ret > -1 && S_ISDIR (status.st_mode));*/
	return (g_file_test (File, G_FILE_TEST_IS_DIR));
}
// AFFICHE L ICONE A GAUCHE DANS LA BARRE DE TITRE
//
gchar *libutils_get_pathname_pixmaps (gchar *p_name_pixmap)
{
	gchar *LineCommand = NULL;

	LineCommand = xdg_search_data_xdg( p_name_pixmap );
	return (LineCommand);
}
// AFFICHE L ICONE A GAUCHE DANS LA BARRE DE TITRE
//
void libutils_set_default_icone_to_win (GtkWidget *win)
{
	gchar     *LineCommand = NULL;
	GError    *error = NULL;

	if( NULL != (LineCommand = xdg_search_data_xdg( "xcfa.png" ))) {
	 	gtk_window_set_icon_from_file (GTK_WINDOW(win), LineCommand, &error);
	 	if (error) {
	 		GDK_PIXBUF_ERROR;
			g_critical ("Could not load pixbuf: %s\n", error->message);
			g_error_free (error);
		}
	}
	g_free (LineCommand);
	LineCommand = NULL;
}
// Execute WHICH Name_Prg et si 'gchar **Pathname_Prg' est non null retourne le resultat
//	gchar *g_find_program_in_path( const gchar *program );
//
gboolean libutils_find_file (gchar *p_find_file)
{
	gchar      *PathName = NULL;

	if( NULL != p_find_file ) {
		if( NULL != (PathName = g_find_program_in_path( p_find_file ))) {
			g_free( PathName );	PathName = NULL;
			return( TRUE );
		}
	}

	return( FALSE );
}
/* utils_hexa_to_int : convertit en entier la chaine pointee par 'gchar *Ptr_Hexa' en hexa
*  La chaine doit commencer par 0x..
*  VALUES A = 65  et   a = 97
*/
gint libutils_hexa_to_int (gchar *Ptr_Hexa)
{
	gint     chiffre_hexa = 0;
	/*gint     cpt = 0;*/
	gint     Gint_Ret = 0;
	gboolean Bool_dans_hexa = FALSE;

	/* PRINT_FUNC_LF(); */

	if (NULL == Ptr_Hexa) return (0);
	if ('\0' == *(Ptr_Hexa) || '\0' == *(Ptr_Hexa+1) || '\0' == *(Ptr_Hexa+2) || '\0' == *(Ptr_Hexa+3))  return (0);

	if (*Ptr_Hexa == '0' && (*(Ptr_Hexa+1) == 'x' || *(Ptr_Hexa+1) == 'X')) {
		Ptr_Hexa += 2;
		Bool_dans_hexa = TRUE;
	}

	Gint_Ret = 0;
	while (Bool_dans_hexa) {
		if (g_ascii_isdigit (*Ptr_Hexa)) {
			chiffre_hexa = *Ptr_Hexa - '0';
		}
		else if (g_ascii_isxdigit (*Ptr_Hexa)) {
			if ((gint)*Ptr_Hexa >= 'a')
				chiffre_hexa = *Ptr_Hexa - 'a' + 10;
			else    chiffre_hexa = *Ptr_Hexa - 'A' + 10;
		}
		else Bool_dans_hexa = FALSE;

		if (Bool_dans_hexa) {
			Gint_Ret = 16 * Gint_Ret + chiffre_hexa;
			Ptr_Hexa ++;
		}
	}

	return (Gint_Ret);
}
// SUPPRESSION SIMPLE ALLOC POINTEE PAR LE GLIST ANSI QUE LE GLIST
//
GList *libutils_remove_glist (GList *New)
{
	GList *list = NULL;
	gchar *ptr = NULL;

	if (New != NULL) {
		list = g_list_first (New);
		while (list) {
			if ((ptr = (gchar*)list->data)) {
				g_free (ptr);
				ptr = NULL;
				list->data = NULL;
			}
			list = g_list_next(list);
		}
		g_list_free (New);
		New = NULL;
	}
	return ((GList *)NULL);
}
// SUPPRESSION SIMPLE ALLOC POINTEE PAR LE GSLIST ANSI QUE LE GSLIST
//
GSList *libutils_remove_gslist (GSList *New)
{
	GSList *gslist = NULL;

	if (NULL != (gslist = New)) {
		while (gslist) {
			if (NULL != (gchar*)gslist->data) {
				g_free (gslist->data);
				gslist->data = NULL;
			}
			gslist = g_slist_next (gslist);
		}
		g_slist_free (New);
		gslist = New = NULL;
	}
	return ((GSList *)NULL);
}
// RETOURNE LA TAILLE DU FICHIER EN OCTETS
//
size_t libutils_get_size_file (gchar *PathName)
{
	FILE   *fp;
	size_t  size = 0;

	if ((fp = fopen (PathName, "r"))) {
		fseek (fp, 0, SEEK_END);
		size = ftell (fp);
		fclose (fp);
	}
	return (size);
}
// Ecriture de datas sur disk, recuperation puis effacement du fichier
//
void libutils_add_datas_on_disk (gchar *data)
{
	FILE *fp = NULL;

	fp = fopen ("/tmp/data_xcfa.txt", "a");
	fprintf (fp, "%s", data);
	fclose (fp);
}
gchar *libutils_get_datas_on_disk (void)
{
	/*
	FILE   *fp = NULL;
	gchar  *buf = NULL;
	size_t  size;

	size = libutils_get_size_file ("/tmp/data_xcfa.txt");
	buf = (gchar *)g_malloc0 (sizeof(gchar) * (size + 10));
	if ((fp = fopen ("/tmp/data_xcfa.txt", "r")) != NULL) {
		size = fread (buf, 1, size, fp);
		fclose (fp);
	} else {
		g_free (buf);
		buf = NULL;
	}

	g_unlink ("/tmp/data_xcfa.txt");

	return ((gchar  *)buf);
	*/
	FILE   *fp = NULL;
	gchar  *buf = NULL;
	size_t  size;

	if( (size = libutils_get_size_file( "/tmp/data_xcfa.txt" )) > 10 ) {
		buf = (gchar *)g_malloc0 (sizeof(gchar) * (size + 10));
		if ((fp = fopen ("/tmp/data_xcfa.txt", "r")) != NULL) {
			size = fread (buf, 1, size, fp);
			fclose (fp);
		} else {
			g_free (buf);
			buf = NULL;
		}
	}
	g_unlink ("/tmp/data_xcfa.txt");

	return ((gchar  *)buf);
}
// TRUE SI ECRITURE OK SINON FALSE
//
gboolean libutils_test_write (gchar *path)
{
	gboolean  BoolWriteOk = FALSE;
	FILE     *fp = NULL;
	gchar    *str = NULL;

	if (path[ 0 ] == '/' && path[ 1 ] != '\0') {
		str = g_strdup_printf ("%s/xcfa_tst_write.txt", path);
		fp = fopen (str, "w");
		if (fp) {
			BoolWriteOk = TRUE;
			fclose (fp);
			g_unlink (str);
		}
		g_free (str);
		str = NULL;
	}

	return (BoolWriteOk);
}
// INIT PIXBUF
//
GdkPixbuf *libutils_init_pixbufs (gchar *NameFilePixbuf)
{
	GdkPixbuf *NewPixbuf = NULL;
	GError    *error = NULL;
	gchar     *Pathname_Pixbuf = NULL;

	Pathname_Pixbuf = libutils_get_pathname_pixmaps (NameFilePixbuf);

	NewPixbuf = gdk_pixbuf_new_from_file(Pathname_Pixbuf, &error);
 	if (error) {
 		GDK_PIXBUF_ERROR;
		g_critical ("Could not load pixbuf: %s\n", error->message);
		g_error_free (error);
		g_free (Pathname_Pixbuf);
		Pathname_Pixbuf = NULL;
		return (NULL);
	}
	g_free (Pathname_Pixbuf);
	Pathname_Pixbuf = NULL;

	return (NewPixbuf);
}
//
// typedef enum {
// 	TYPE_ALL_FILE = 0,
// 	TYPE_WAV_FILE,
// 	TYPE_MP3_OGG_FILE,
// 	TYPE_TAG_FILE
// } TYPE_FILE;
//
gint libutils_get_first_line_is_selected( GtkTreeSelection *AdrLine, GtkTreeModel *AdrTreeModel )
{
	gboolean	valid;
	GtkTreeIter	iter;
	gint		Cpt = -1;

	if (NULL != AdrLine && NULL != AdrTreeModel ) {
		valid = gtk_tree_model_get_iter_first( AdrTreeModel, &iter );
		while (valid) {
			if (TRUE == gtk_tree_selection_iter_is_selected( AdrLine, &iter) ) {
				Cpt ++;
				return( Cpt );
			}
			valid = gtk_tree_model_iter_next( AdrTreeModel, &iter );
			if( TRUE == valid ) Cpt ++;
		}
	}
	return( -1 );
}
//
//
gchar *libutils_get_name_without_ext_with_amp( gchar *p_PathNameFile )
{
	gchar *NewName = g_strnfill (strlen(p_PathNameFile) * 4, '\0');
	gchar *NewPtr = NewName;
	gchar *NameRet = NULL;
	gchar *ptr = NULL;

	// POINTEUR SUR LE DEBUT DU NOM REEL
	if (NULL != (ptr = strrchr (p_PathNameFile, '/')))
		ptr ++;
	else	ptr = p_PathNameFile;

	// COPIE DE p_PathNameFile VERS NewName
	while (*ptr) {
		if (*ptr == '&') {
			*NewPtr ++ = *ptr ++;
			*NewPtr ++ = 'a';
			*NewPtr ++ = 'm';
			*NewPtr ++ = 'p';
			*NewPtr ++ = ';';
		}
		else {
			*NewPtr ++ = *ptr ++;
		}
	}

	// DUPLIQUER NewName VERS name
	NameRet = g_strdup (NewName);
	g_free (NewName);
	NewName = NULL;

	// CACHER L EXTENTION SI ELLE EXISTE DANS name
	if (NULL != (ptr = strrchr (NameRet, '.'))) *ptr = '\0';

	return ((gchar *)NameRet);
}
//
//
gchar *libutils_get_name_without_ext( gchar *p_PathNameFile )
{
	gchar *NewName = NULL;
	gchar *Ptr = NULL;

	// POINTEUR SUR LE DEBUT DU NOM REEL
	if( NULL != (Ptr = strrchr (p_PathNameFile, '/' )))
		Ptr ++;
	else	Ptr = p_PathNameFile;

	// COPIE
	NewName = g_strdup( Ptr );

	// CACHER L EXTENTION SI ELLE EXISTE DANS NewName
	if( NULL != ( Ptr = strrchr( NewName, '.' ))) *Ptr = '\0';

	return( (gchar *)NewName );
}
//
//
gchar *libutils_string_toupper( gchar *p_str )
{
	gchar	*Str = g_strdup( p_str );
	gchar	*Ptr = Str;

	while( Ptr && *Ptr ) {
		*Ptr = g_ascii_toupper( *Ptr );
		Ptr ++;
	}

	return( Str );
}