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

filename_expand.cxx
Go to the documentation of this file.
1 //
2 // "$Id$"
3 //
4 // Filename expansion routines for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2010 by Bill Spitzak and others.
7 //
8 // This library is free software. Distribution and use rights are outlined in
9 // the file "COPYING" which should have been included with this file. If this
10 // file is missing or damaged, see the license at:
11 //
12 // http://www.fltk.org/COPYING.php
13 //
14 // Please report all bugs and problems on the following page:
15 //
16 // http://www.fltk.org/str.php
17 //
18 
19 /* expand a file name by substuting environment variables and
20  home directories. Returns true if any changes were made.
21  to & from may be the same buffer.
22 */
23 
24 #include <FL/filename.H>
25 #include <FL/fl_utf8.h>
26 #include <stdlib.h>
27 #include "flstring.h"
28 #if defined(WIN32) && !defined(__CYGWIN__)
29 #include <windows.h>
30 #else
31 # include <unistd.h>
32 # include <pwd.h>
33 #endif
34 
35 #if defined(WIN32) || defined(__EMX__) && !defined(__CYGWIN__)
36 static inline int isdirsep(char c) {return c=='/' || c=='\\';}
37 #else
38 #define isdirsep(c) ((c)=='/')
39 #endif
40 
63 int fl_filename_expand(char *to,int tolen, const char *from) {
64 
65  char *temp = new char[tolen];
66  strlcpy(temp,from, tolen);
67  char *start = temp;
68  char *end = temp+strlen(temp);
69 
70  int ret = 0;
71 
72  for (char *a=temp; a<end; ) { // for each slash component
73  char *e; for (e=a; e<end && !isdirsep(*e); e++) {/*empty*/} // find next slash
74  const char *value = 0; // this will point at substitute value
75  switch (*a) {
76  case '~': // a home directory name
77  if (e <= a+1) { // current user's directory
78  value = fl_getenv("HOME");
79 #ifndef WIN32
80  } else { // another user's directory
81  struct passwd *pwd;
82  char t = *e; *(char *)e = 0;
83  pwd = getpwnam(a+1);
84  *(char *)e = t;
85  if (pwd) value = pwd->pw_dir;
86 #endif
87  }
88  break;
89  case '$': /* an environment variable */
90  {char t = *e; *(char *)e = 0; value = fl_getenv(a+1); *(char *)e = t;}
91  break;
92  }
93  if (value) {
94  // substitutions that start with slash delete everything before them:
95  if (isdirsep(value[0])) start = a;
96 #if defined(WIN32) || defined(__EMX__) && !defined(__CYGWIN__)
97  // also if it starts with "A:"
98  if (value[0] && value[1]==':') start = a;
99 #endif
100  int t = (int) strlen(value); if (isdirsep(value[t-1])) t--;
101  if ((end+1-e+t) >= tolen) end += tolen - (end+1-e+t);
102  memmove(a+t, e, end+1-e);
103  end = a+t+(end-e);
104  *end = '\0';
105  memcpy(a, value, t);
106  ret++;
107  } else {
108  a = e+1;
109 #if defined(WIN32) || defined(__EMX__) && !defined(__CYGWIN__)
110  if (*e == '\\') {*e = '/'; ret++;} // ha ha!
111 #endif
112  }
113  }
114 
115  strlcpy(to, start, tolen);
116 
117  delete[] temp;
118 
119  return ret;
120 }
121 
122 
123 //
124 // End of "$Id$".
125 //
isdirsep
#define isdirsep(c)
Definition: filename_expand.cxx:38
filename.H
pwd
static char * pwd
Definition: fluid.cxx:124
fl_utf8.h
header for Unicode and UTF-8 character handling
strlcpy
#define strlcpy
Definition: flstring.h:84
fl_getenv
char * fl_getenv(const char *name)
Definition: fl_utf8.cxx:421
flstring.h
start
static int start(Fl_RGB_Image *img, int XP, int YP, int WP, int HP, int w, int h, int &cx, int &cy, int &X, int &Y, int &W, int &H)
Definition: Fl_Image.cxx:655
fl_filename_expand
int fl_filename_expand(char *to, int tolen, const char *from)
Definition: filename_expand.cxx:63