Search
lxdream.org :: lxdream/src/lxpaths.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/lxpaths.c
changeset 1205:a486ac64f34b
prev1056:d0896e6530d6
next1283:2cbafe321d6f
author nkeynes
date Fri Mar 02 23:49:10 2012 +1000 (8 years ago)
permissions -rw-r--r--
last change Android WIP:
* Rename gui_jni.c to gui_android.c - now quite android specific.
* Implement generic EGL driver with very minimal Java wrapper
* Run emulation in separate thread, and implement simple queue for
inter-thread communication.
* Add menu/action-bar items for start + reset
file annotate diff log raw
nkeynes@1041
     1
/**
nkeynes@1041
     2
 * $Id$
nkeynes@1041
     3
 *
nkeynes@1041
     4
 * GUI helper functions that aren't specific to any particular implementation.
nkeynes@1041
     5
 *
nkeynes@1041
     6
 * Copyright (c) 2009 Nathan Keynes.
nkeynes@1041
     7
 *
nkeynes@1041
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@1041
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@1041
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@1041
    11
 * (at your option) any later version.
nkeynes@1041
    12
 *
nkeynes@1041
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@1041
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@1041
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@1041
    16
 * GNU General Public License for more details.
nkeynes@1041
    17
 */
nkeynes@1041
    18
nkeynes@1041
    19
#include <ctype.h>
nkeynes@1041
    20
#include <unistd.h>
nkeynes@1205
    21
#include <stdlib.h>
nkeynes@1041
    22
#include <glib/gstrfuncs.h>
nkeynes@1041
    23
#include <glib/gutils.h>
nkeynes@1041
    24
nkeynes@1041
    25
#include "gui.h"
nkeynes@1041
    26
#include "config.h"
nkeynes@1041
    27
nkeynes@1041
    28
static gchar *gui_paths[CONFIG_KEY_MAX];
nkeynes@1041
    29
nkeynes@1041
    30
/**
nkeynes@1041
    31
 * Test if we need to escape a path to prevent substitution mangling. 
nkeynes@1041
    32
 * @return TRUE if the input value contains any character that doesn't
nkeynes@1041
    33
 * match [a-zA-Z0-9._@%/] (this will escape slightly more than it needs to,
nkeynes@1041
    34
 * but is safe)
nkeynes@1041
    35
 */
nkeynes@1041
    36
static gboolean path_needs_escaping( const gchar *value )
nkeynes@1041
    37
{
nkeynes@1041
    38
   const gchar *p = value;
nkeynes@1041
    39
   while( *p ) {
nkeynes@1041
    40
       if( !isalnum(*p) && *p != '.' && *p != '_' &&
nkeynes@1041
    41
               *p != '@' && *p != '%' && *p != '/' ) {
nkeynes@1041
    42
           return TRUE;
nkeynes@1041
    43
       }
nkeynes@1041
    44
       p++;
nkeynes@1041
    45
   }
nkeynes@1041
    46
   return FALSE;
nkeynes@1041
    47
}
nkeynes@1041
    48
nkeynes@1041
    49
gchar *get_escaped_path( const gchar *value )
nkeynes@1041
    50
{
nkeynes@1041
    51
    if( value != NULL && path_needs_escaping(value) ) {
nkeynes@1041
    52
        /* Escape with "", and backslash the remaining characters:
nkeynes@1041
    53
         *   \ " $ `
nkeynes@1041
    54
         */
nkeynes@1041
    55
        char buf[strlen(value)*2+3];  
nkeynes@1041
    56
        const char *s = value;
nkeynes@1041
    57
        char *p = buf;
nkeynes@1041
    58
        *p++ = '\"';
nkeynes@1041
    59
        while( *s ) {
nkeynes@1041
    60
            if( *s == '\\' || *s == '"' || *s == '$' || *s == '`' ) {
nkeynes@1041
    61
                *p++ = '\\';
nkeynes@1041
    62
            }
nkeynes@1041
    63
            *p++ = *s++;
nkeynes@1041
    64
        }
nkeynes@1041
    65
        *p++ = '\"';
nkeynes@1041
    66
        *p = '\0';
nkeynes@1041
    67
        return g_strdup(buf);
nkeynes@1041
    68
    } else {
nkeynes@1041
    69
        return g_strdup(value);
nkeynes@1041
    70
    }
nkeynes@1041
    71
}
nkeynes@1041
    72
nkeynes@1041
    73
gchar *get_expanded_path( const gchar *input )
nkeynes@1041
    74
{
nkeynes@1205
    75
    char result[PATH_MAX];
nkeynes@1205
    76
nkeynes@1205
    77
    char *d, *e;
nkeynes@1205
    78
    const char *s;
nkeynes@1205
    79
    d = result;
nkeynes@1205
    80
    e = result+sizeof(result)-1;
nkeynes@1205
    81
    s = input;
nkeynes@1205
    82
nkeynes@1205
    83
    if( input == NULL )
nkeynes@1041
    84
        return NULL;
nkeynes@1205
    85
nkeynes@1205
    86
    while( *s ) {
nkeynes@1205
    87
        if( d == e ) {
nkeynes@1205
    88
            return g_strdup(input); /* expansion too long */
nkeynes@1205
    89
        }
nkeynes@1205
    90
        char c = *s++;
nkeynes@1205
    91
        if( c == '$' ) {
nkeynes@1205
    92
            if( *s == '{' ) {
nkeynes@1205
    93
                s++;
nkeynes@1205
    94
                const char *q = s;
nkeynes@1205
    95
                while( *q != '}' ) {
nkeynes@1205
    96
                    if( ! *q ) {
nkeynes@1205
    97
                        return g_strdup(input); /* unterminated variable */
nkeynes@1205
    98
                    }
nkeynes@1205
    99
                    q++;
nkeynes@1205
   100
                }
nkeynes@1205
   101
                char *tmp = g_strndup(s, (q-s));
nkeynes@1205
   102
                s = q+1;
nkeynes@1205
   103
                char *value = getenv(tmp);
nkeynes@1205
   104
                g_free(tmp);
nkeynes@1205
   105
                if( value != NULL ) {
nkeynes@1205
   106
                    int len = strlen(value);
nkeynes@1205
   107
                    if( d + len > e )
nkeynes@1205
   108
                        return g_strdup(input);
nkeynes@1205
   109
                    strcpy(d, value);
nkeynes@1205
   110
                    d+=len;
nkeynes@1205
   111
                } /* Else, empty string */
nkeynes@1205
   112
            } else {
nkeynes@1205
   113
                const char *q = s;
nkeynes@1205
   114
                while( isalnum(*q) || *q == '_' ) {
nkeynes@1205
   115
                    q++;
nkeynes@1205
   116
                }
nkeynes@1205
   117
                if( q == s ) {
nkeynes@1205
   118
                    *d++ = '$';
nkeynes@1205
   119
                } else {
nkeynes@1205
   120
                    char *tmp = g_strndup(s,q-s);
nkeynes@1205
   121
                    s = q;
nkeynes@1205
   122
                    char *value = getenv(tmp);
nkeynes@1205
   123
                    g_free(tmp);
nkeynes@1205
   124
                    if( value != NULL ) {
nkeynes@1205
   125
                        int len = strlen(value);
nkeynes@1205
   126
                        if( d + len > e )
nkeynes@1205
   127
                            return g_strdup(input);
nkeynes@1205
   128
                        strcpy(d, value);
nkeynes@1205
   129
                        d += len;
nkeynes@1205
   130
                    }
nkeynes@1205
   131
                }
nkeynes@1205
   132
            }
nkeynes@1205
   133
        } else if( c == '\\' ) {
nkeynes@1205
   134
            c = *s++;
nkeynes@1205
   135
            if( c ) {
nkeynes@1205
   136
                *d++ = c;
nkeynes@1205
   137
            } else {
nkeynes@1205
   138
                *d++ = '\\';
nkeynes@1205
   139
            }
nkeynes@1205
   140
        } else {
nkeynes@1205
   141
            *d++ = c;
nkeynes@1205
   142
        }
nkeynes@1041
   143
    }
nkeynes@1205
   144
    *d = '\0';
nkeynes@1205
   145
    return g_strdup(result);
nkeynes@1041
   146
}
nkeynes@1041
   147
gchar *get_absolute_path( const gchar *in_path )
nkeynes@1041
   148
{
nkeynes@1041
   149
    char tmp[PATH_MAX];
nkeynes@1041
   150
    if( in_path == NULL ) {
nkeynes@1041
   151
        return NULL;
nkeynes@1041
   152
    }
nkeynes@1041
   153
    if( in_path[0] == '/' || in_path[0] == 0 ) {
nkeynes@1041
   154
        return g_strdup(in_path);
nkeynes@1041
   155
    } else {
nkeynes@1041
   156
        getcwd(tmp, sizeof(tmp));
nkeynes@1041
   157
        return g_strdup_printf("%s%c%s", tmp, G_DIR_SEPARATOR, in_path);
nkeynes@1041
   158
    }
nkeynes@1041
   159
}
nkeynes@1041
   160
nkeynes@1041
   161
gchar *get_filename_at( const gchar *at, const gchar *filename )
nkeynes@1041
   162
{
nkeynes@1041
   163
    char tmp[PATH_MAX];
nkeynes@1056
   164
    char *p = strrchr( at, '/' );
nkeynes@1041
   165
    if( p == NULL ) {
nkeynes@1041
   166
        /* No path at all, so just return filename */
nkeynes@1041
   167
        return g_strdup(filename);
nkeynes@1041
   168
    } else {
nkeynes@1056
   169
        int off = p-at;
nkeynes@1041
   170
        return g_strdup_printf("%.*s%c%s", off, at, G_DIR_SEPARATOR, filename );
nkeynes@1041
   171
    }
nkeynes@1041
   172
}
nkeynes@1041
   173
nkeynes@1041
   174
const gchar *get_gui_path( int key )
nkeynes@1041
   175
{
nkeynes@1041
   176
    if( gui_paths[key] == NULL ) {
nkeynes@1041
   177
        gui_paths[key] = lxdream_get_global_config_path_value(key);
nkeynes@1041
   178
        /* If no path defined, go with the current working directory */
nkeynes@1041
   179
        if( gui_paths[key] == NULL ) {
nkeynes@1041
   180
            gui_paths[key] = get_absolute_path(".");
nkeynes@1041
   181
        }
nkeynes@1041
   182
    }
nkeynes@1041
   183
    return gui_paths[key];
nkeynes@1041
   184
}
nkeynes@1041
   185
nkeynes@1041
   186
void set_gui_path( int key, const gchar *path )
nkeynes@1041
   187
{
nkeynes@1041
   188
    g_free(gui_paths[key]);
nkeynes@1041
   189
    gui_paths[key] = g_strdup(path);
nkeynes@1041
   190
}
nkeynes@1041
   191
nkeynes@1041
   192
void reset_gui_paths()
nkeynes@1041
   193
{
nkeynes@1041
   194
    int i;
nkeynes@1041
   195
    for( i=0; i < CONFIG_KEY_MAX; i++ ) {
nkeynes@1041
   196
        g_free(gui_paths[i]);
nkeynes@1041
   197
        gui_paths[i] = NULL;
nkeynes@1041
   198
    }
nkeynes@1041
   199
}
.