Search
lxdream.org :: lxdream/src/lxpaths.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/lxpaths.c
changeset 1041:5fcc39857c5c
next1056:d0896e6530d6
author nkeynes
date Sun Jun 28 10:39:51 2009 +0000 (14 years ago)
permissions -rw-r--r--
last change Remove -Wl,-z,defs from dpkg build
Add explicit library deps (so that optional libs are Recommended/Suggested)
Tweak copyright file
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/lxpaths.c Sun Jun 28 10:39:51 2009 +0000
1.3 @@ -0,0 +1,158 @@
1.4 +/**
1.5 + * $Id$
1.6 + *
1.7 + * GUI helper functions that aren't specific to any particular implementation.
1.8 + *
1.9 + * Copyright (c) 2009 Nathan Keynes.
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify
1.12 + * it under the terms of the GNU General Public License as published by
1.13 + * the Free Software Foundation; either version 2 of the License, or
1.14 + * (at your option) any later version.
1.15 + *
1.16 + * This program is distributed in the hope that it will be useful,
1.17 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.19 + * GNU General Public License for more details.
1.20 + */
1.21 +
1.22 +#include <ctype.h>
1.23 +#include <unistd.h>
1.24 +#include <wordexp.h>
1.25 +#include <glib/gstrfuncs.h>
1.26 +#include <glib/gutils.h>
1.27 +
1.28 +#include "gui.h"
1.29 +#include "config.h"
1.30 +
1.31 +static gchar *gui_paths[CONFIG_KEY_MAX];
1.32 +
1.33 +/**
1.34 + * Test if we need to escape a path to prevent substitution mangling.
1.35 + * @return TRUE if the input value contains any character that doesn't
1.36 + * match [a-zA-Z0-9._@%/] (this will escape slightly more than it needs to,
1.37 + * but is safe)
1.38 + */
1.39 +static gboolean path_needs_escaping( const gchar *value )
1.40 +{
1.41 + const gchar *p = value;
1.42 + while( *p ) {
1.43 + if( !isalnum(*p) && *p != '.' && *p != '_' &&
1.44 + *p != '@' && *p != '%' && *p != '/' ) {
1.45 + return TRUE;
1.46 + }
1.47 + p++;
1.48 + }
1.49 + return FALSE;
1.50 +}
1.51 +
1.52 +gchar *get_escaped_path( const gchar *value )
1.53 +{
1.54 + if( value != NULL && path_needs_escaping(value) ) {
1.55 + /* Escape with "", and backslash the remaining characters:
1.56 + * \ " $ `
1.57 + */
1.58 + char buf[strlen(value)*2+3];
1.59 + const char *s = value;
1.60 + char *p = buf;
1.61 + *p++ = '\"';
1.62 + while( *s ) {
1.63 + if( *s == '\\' || *s == '"' || *s == '$' || *s == '`' ) {
1.64 + *p++ = '\\';
1.65 + }
1.66 + *p++ = *s++;
1.67 + }
1.68 + *p++ = '\"';
1.69 + *p = '\0';
1.70 + return g_strdup(buf);
1.71 + } else {
1.72 + return g_strdup(value);
1.73 + }
1.74 +}
1.75 +
1.76 +gchar *get_expanded_path( const gchar *input )
1.77 +{
1.78 + wordexp_t we;
1.79 + if( input == NULL ) {
1.80 + return NULL;
1.81 + }
1.82 + memset(&we,0,sizeof(we));
1.83 + int result = wordexp(input, &we, WRDE_NOCMD);
1.84 + if( result != 0 || we.we_wordc == 0 ) {
1.85 + /* On failure, return the original input unchanged */
1.86 + return g_strdup(input);
1.87 + } else {
1.88 + /* On success, concatenate all 'words' together into a single
1.89 + * space-separated string
1.90 + */
1.91 + int length = we.we_wordc, i;
1.92 + gchar *result, *p;
1.93 +
1.94 + for( i=0; i<we.we_wordc; i++ ) {
1.95 + length += strlen(we.we_wordv[i]);
1.96 + }
1.97 + p = result = g_malloc(length);
1.98 + for( i=0; i<we.we_wordc; i++ ) {
1.99 + if( i != 0 )
1.100 + *p++ = ' ';
1.101 + strcpy( p, we.we_wordv[i] );
1.102 + p += strlen(p);
1.103 + }
1.104 + wordfree(&we);
1.105 + return result;
1.106 + }
1.107 +}
1.108 +
1.109 +gchar *get_absolute_path( const gchar *in_path )
1.110 +{
1.111 + char tmp[PATH_MAX];
1.112 + if( in_path == NULL ) {
1.113 + return NULL;
1.114 + }
1.115 + if( in_path[0] == '/' || in_path[0] == 0 ) {
1.116 + return g_strdup(in_path);
1.117 + } else {
1.118 + getcwd(tmp, sizeof(tmp));
1.119 + return g_strdup_printf("%s%c%s", tmp, G_DIR_SEPARATOR, in_path);
1.120 + }
1.121 +}
1.122 +
1.123 +gchar *get_filename_at( const gchar *at, const gchar *filename )
1.124 +{
1.125 + char tmp[PATH_MAX];
1.126 + char *p = strrchr( filename, '/' );
1.127 + if( p == NULL ) {
1.128 + /* No path at all, so just return filename */
1.129 + return g_strdup(filename);
1.130 + } else {
1.131 + int off = p-filename;
1.132 + return g_strdup_printf("%.*s%c%s", off, at, G_DIR_SEPARATOR, filename );
1.133 + }
1.134 +}
1.135 +
1.136 +const gchar *get_gui_path( int key )
1.137 +{
1.138 + if( gui_paths[key] == NULL ) {
1.139 + gui_paths[key] = lxdream_get_global_config_path_value(key);
1.140 + /* If no path defined, go with the current working directory */
1.141 + if( gui_paths[key] == NULL ) {
1.142 + gui_paths[key] = get_absolute_path(".");
1.143 + }
1.144 + }
1.145 + return gui_paths[key];
1.146 +}
1.147 +
1.148 +void set_gui_path( int key, const gchar *path )
1.149 +{
1.150 + g_free(gui_paths[key]);
1.151 + gui_paths[key] = g_strdup(path);
1.152 +}
1.153 +
1.154 +void reset_gui_paths()
1.155 +{
1.156 + int i;
1.157 + for( i=0; i < CONFIG_KEY_MAX; i++ ) {
1.158 + g_free(gui_paths[i]);
1.159 + gui_paths[i] = NULL;
1.160 + }
1.161 +}
.