Search
lxdream.org :: lxdream/src/lxpaths.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/lxpaths.c
changeset 1056:d0896e6530d6
prev1041:5fcc39857c5c
next1205:a486ac64f34b
author nkeynes
date Sun Jun 28 12:07:21 2009 +0000 (11 years ago)
permissions -rw-r--r--
last change Fix handling of rename failure
Fix boneheadedness in get_filename_at
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * GUI helper functions that aren't specific to any particular implementation.
     5  *
     6  * Copyright (c) 2009 Nathan Keynes.
     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  */
    19 #include <ctype.h>
    20 #include <unistd.h>
    21 #include <wordexp.h>
    22 #include <glib/gstrfuncs.h>
    23 #include <glib/gutils.h>
    25 #include "gui.h"
    26 #include "config.h"
    28 static gchar *gui_paths[CONFIG_KEY_MAX];
    30 /**
    31  * Test if we need to escape a path to prevent substitution mangling. 
    32  * @return TRUE if the input value contains any character that doesn't
    33  * match [a-zA-Z0-9._@%/] (this will escape slightly more than it needs to,
    34  * but is safe)
    35  */
    36 static gboolean path_needs_escaping( const gchar *value )
    37 {
    38    const gchar *p = value;
    39    while( *p ) {
    40        if( !isalnum(*p) && *p != '.' && *p != '_' &&
    41                *p != '@' && *p != '%' && *p != '/' ) {
    42            return TRUE;
    43        }
    44        p++;
    45    }
    46    return FALSE;
    47 }
    49 gchar *get_escaped_path( const gchar *value )
    50 {
    51     if( value != NULL && path_needs_escaping(value) ) {
    52         /* Escape with "", and backslash the remaining characters:
    53          *   \ " $ `
    54          */
    55         char buf[strlen(value)*2+3];  
    56         const char *s = value;
    57         char *p = buf;
    58         *p++ = '\"';
    59         while( *s ) {
    60             if( *s == '\\' || *s == '"' || *s == '$' || *s == '`' ) {
    61                 *p++ = '\\';
    62             }
    63             *p++ = *s++;
    64         }
    65         *p++ = '\"';
    66         *p = '\0';
    67         return g_strdup(buf);
    68     } else {
    69         return g_strdup(value);
    70     }
    71 }
    73 gchar *get_expanded_path( const gchar *input )
    74 {
    75     wordexp_t we;
    76     if( input == NULL ) {
    77         return NULL;
    78     }
    79     memset(&we,0,sizeof(we));
    80     int result = wordexp(input, &we, WRDE_NOCMD);
    81     if( result != 0 || we.we_wordc == 0 ) {
    82         /* On failure, return the original input unchanged */
    83         return g_strdup(input);
    84     } else {
    85         /* On success, concatenate all 'words' together into a single 
    86          * space-separated string
    87          */
    88         int length = we.we_wordc, i;
    89         gchar *result, *p;
    91         for( i=0; i<we.we_wordc; i++ ) {
    92             length += strlen(we.we_wordv[i]);
    93         }
    94         p = result = g_malloc(length);
    95         for( i=0; i<we.we_wordc; i++ ) {
    96             if( i != 0 )
    97                 *p++ = ' ';
    98             strcpy( p, we.we_wordv[i] );
    99             p += strlen(p);
   100         }
   101         wordfree(&we);
   102         return result;
   103     }        
   104 }
   106 gchar *get_absolute_path( const gchar *in_path )
   107 {
   108     char tmp[PATH_MAX];
   109     if( in_path == NULL ) {
   110         return NULL;
   111     }
   112     if( in_path[0] == '/' || in_path[0] == 0 ) {
   113         return g_strdup(in_path);
   114     } else {
   115         getcwd(tmp, sizeof(tmp));
   116         return g_strdup_printf("%s%c%s", tmp, G_DIR_SEPARATOR, in_path);
   117     }
   118 }
   120 gchar *get_filename_at( const gchar *at, const gchar *filename )
   121 {
   122     char tmp[PATH_MAX];
   123     char *p = strrchr( at, '/' );
   124     if( p == NULL ) {
   125         /* No path at all, so just return filename */
   126         return g_strdup(filename);
   127     } else {
   128         int off = p-at;
   129         return g_strdup_printf("%.*s%c%s", off, at, G_DIR_SEPARATOR, filename );
   130     }
   131 }
   133 const gchar *get_gui_path( int key )
   134 {
   135     if( gui_paths[key] == NULL ) {
   136         gui_paths[key] = lxdream_get_global_config_path_value(key);
   137         /* If no path defined, go with the current working directory */
   138         if( gui_paths[key] == NULL ) {
   139             gui_paths[key] = get_absolute_path(".");
   140         }
   141     }
   142     return gui_paths[key];
   143 }
   145 void set_gui_path( int key, const gchar *path )
   146 {
   147     g_free(gui_paths[key]);
   148     gui_paths[key] = g_strdup(path);
   149 }
   151 void reset_gui_paths()
   152 {
   153     int i;
   154     for( i=0; i < CONFIG_KEY_MAX; i++ ) {
   155         g_free(gui_paths[i]);
   156         gui_paths[i] = NULL;
   157     }
   158 }
.