fltk 1.3.0rc3
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X. Release candidate.
  SfR Fresh Dox: fltk-1.3.0rc3-source.tar.gz ("inofficial" and yet experimental doxygen-generated source code documentation)  

filename_expand.cxx

Go to the documentation of this file.
00001 //
00002 // "$Id: filename_expand.cxx 7903 2010-11-28 21:06:39Z matt $"
00003 //
00004 // Filename expansion routines for the Fast Light Tool Kit (FLTK).
00005 //
00006 // Copyright 1998-2010 by Bill Spitzak and others.
00007 //
00008 // This library is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU Library General Public
00010 // License as published by the Free Software Foundation; either
00011 // version 2 of the License, or (at your option) any later version.
00012 //
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 // Library General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU Library General Public
00019 // License along with this library; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00021 // USA.
00022 //
00023 // Please report all bugs and problems on the following page:
00024 //
00025 //     http://www.fltk.org/str.php
00026 //
00027 
00028 /* expand a file name by substuting environment variables and
00029    home directories.  Returns true if any changes were made.
00030    to & from may be the same buffer.
00031 */
00032 
00033 #include <FL/filename.H>
00034 #include <FL/fl_utf8.h>
00035 #include <stdlib.h>
00036 #include "flstring.h"
00037 #if defined(WIN32) && !defined(__CYGWIN__)
00038 #include <windows.h>
00039 #else
00040 # include <unistd.h>
00041 # include <pwd.h>
00042 #endif
00043 
00044 #if defined(WIN32) || defined(__EMX__) && !defined(__CYGWIN__)
00045 static inline int isdirsep(char c) {return c=='/' || c=='\\';}
00046 #else
00047 #define isdirsep(c) ((c)=='/')
00048 #endif
00049 
00072 int fl_filename_expand(char *to,int tolen, const char *from) {
00073 
00074   char *temp = new char[tolen];
00075   strlcpy(temp,from, tolen);
00076   char *start = temp;
00077   char *end = temp+strlen(temp);
00078 
00079   int ret = 0;
00080 
00081   for (char *a=temp; a<end; ) { // for each slash component
00082     char *e; for (e=a; e<end && !isdirsep(*e); e++); // find next slash
00083     const char *value = 0; // this will point at substitute value
00084     switch (*a) {
00085     case '~':   // a home directory name
00086       if (e <= a+1) {   // current user's directory
00087         value = fl_getenv("HOME");
00088 #ifndef WIN32
00089       } else {  // another user's directory
00090         struct passwd *pwd;
00091         char t = *e; *(char *)e = 0; 
00092         pwd = getpwnam(a+1); 
00093         *(char *)e = t;
00094             if (pwd) value = pwd->pw_dir;
00095 #endif
00096       }
00097       break;
00098     case '$':           /* an environment variable */
00099       {char t = *e; *(char *)e = 0; value = fl_getenv(a+1); *(char *)e = t;}
00100       break;
00101     }
00102     if (value) {
00103       // substitutions that start with slash delete everything before them:
00104       if (isdirsep(value[0])) start = a;
00105 #if defined(WIN32) || defined(__EMX__) && !defined(__CYGWIN__)
00106       // also if it starts with "A:"
00107       if (value[0] && value[1]==':') start = a;
00108 #endif
00109       int t = strlen(value); if (isdirsep(value[t-1])) t--;
00110       if ((end+1-e+t) >= tolen) end += tolen - (end+1-e+t);
00111       memmove(a+t, e, end+1-e);
00112       end = a+t+(end-e);
00113       *end = '\0';
00114       memcpy(a, value, t);
00115       ret++;
00116     } else {
00117       a = e+1;
00118 #if defined(WIN32) || defined(__EMX__) && !defined(__CYGWIN__)
00119       if (*e == '\\') {*e = '/'; ret++;} // ha ha!
00120 #endif
00121     }
00122   }
00123 
00124   strlcpy(to, start, tolen);
00125 
00126   delete[] temp;
00127 
00128   return ret;
00129 }
00130 
00131 
00132 //
00133 // End of "$Id: filename_expand.cxx 7903 2010-11-28 21:06:39Z matt $".
00134 //