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
|
/**
** utility.c - support/utility functions for gnomp3
**
** Copyright (C) 2000 Matthew Pratt <mattpratt@yahoo.com>
**
** This software is licensed under the terms of the GNU General
** Public License (GPL). Please see the file LICENSE for details.
**/
#include "gnomp3.h"
#include <sys/stat.h>
/* breaks the given path into the filename and the dir directly above that */
void path_breakdown(char *path, char **dir, char **file)
{
char *ptr;
g_return_if_fail( path != NULL );
g_return_if_fail( dir != NULL );
g_return_if_fail( file != NULL );
ptr = path;
*dir = *file = NULL;
while( *ptr )ptr++; // find end of string
/* get rid of .mp3 at end if exists */
if( !strcasecmp( ptr - 4, ".mp3" )){
ptr -= 4;
*ptr = 0;
}
/* step back wards from the end of the string looking for the '/' */
while( ptr != path ){
if ( *ptr == '/' ){
/* file found */
if( *file == NULL ){
*file = ptr + 1;
*ptr = 0;
}else if ( *dir == NULL )
*dir = ptr + 1;
else
return;
}
ptr--;
}
}
void strip_underscores(char *string)
{
while( *string ){
if( *string == '_' )
*string = ' ';
string++;
}
}
/* creates the directory if it does not already exist */
void check_and_create_dir(char *prefix, char *dirname)
{
struct stat st;
char *path;
if( prefix )
path = g_strconcat( prefix, "/", dirname, NULL);
else
path = g_strdup (dirname);
if (stat(path, &st) < 0)
if (mkdir(path, 0755) < 0)
fprintf(stderr, "Could not create '%s' directory...functionality may be limited\n", path);
g_free(path);
}
int is_directory(char *path)
{
struct stat st;
if (stat(path, &st) != -1){
if( S_ISDIR(st.st_mode))
return TRUE;
}
return FALSE;
}
int file_exists(char *path)
{
struct stat st;
if (stat(path, &st) != -1){
return TRUE;
}
return FALSE;
}
/*
* Returns the full path of a file name that is assumed to be in the home
* direcory of the caller, in a static buffer.
*/
char *file_in_home(char *filename)
{
static char path[MAX_PATH];
g_snprintf( path, sizeof(path), "%s/%s", getenv("HOME"), filename );
return path;
}
GList *g_list_move_up(GList *list, GList *node)
{
int pos;
GList *nl;
pos = g_list_position( list, node );
if( pos == 0 )
return list;
nl = g_list_remove_link( list, node );
nl = g_list_insert( nl, node->data, pos - 1 );
//g_free(node);
return nl;
}
GList *g_list_move_down(GList *list, GList *node)
{
int pos;
GList *nl;
pos = g_list_position( list, node );
nl = g_list_remove_link( list, node );
nl = g_list_insert( nl, node->data, pos + 1 );
//g_free(node);
return nl;
}
char *itoa(int i)
{
static char a[128];
g_snprintf( a, sizeof(a), "%d", i );
return a;
}
|