filename.h File Reference


Typedefs

typedef int( File_Sort_F )(const dirent *const *, const dirent *const *)

Functions

int filename_absolute (char *to, int tolen, const char *from, const char *cwd=0)
int filename_relative (char *to, int tolen, const char *from, const char *cwd=0)
const char * filename_name (const char *)
char * filename_name (char *a)
const char * filename_ext (const char *)
char * filename_ext (char *a)
bool filename_match (const char *, const char *pattern)
bool filename_exist (const char *)
bool filename_isdir (const char *)
double filename_size (const char *)
long int filename_mtime (const char *)
int alphasort (const dirent *const *, const dirent *const *)
int casealphasort (const dirent *const *, const dirent *const *)
int casenumericsort (const dirent *const *, const dirent *const *)
int numericsort (const dirent *const *, const dirent *const *)
int filename_list (const char *d, dirent ***list, File_Sort_F *sort)
int filename_list (const char *d, dirent ***list)

Detailed Description

Some functions to manipulate filenames, to make portable programs.

Function Documentation

int filename_absolute char *  output,
int  length,
const char *  input,
const char *  pwd
 

Return the filename from expanded to a full "absolute" path name by prepending the current directory:

  • output is a pointer to a buffer that the result is written to.
  • length is the size of output. No more than length-1 bytes are written, plus a trailing nul.
  • input is the initial filename. Must be different than output!
  • cwd is the directory that filename is relative to. If this is NULL then the result of getcwd() is used.

The return value is the number of bytes this wants to write to output. If this value is greater or equal to length, then you know the output has been truncated, and you can call this again with a buffer of n+1 bytes.

Leading "./" sequences in input are removed, and "../" sequences are removed as well as the matching trailing part of the prefixed directory. Technically this is incorrect if symbolic links are used but this matches the behavior of most programs.

If the pwd argument is null, this also expands names starting with '~' to the user or another user's HOME directory. To expand a filename starting with ~ in the current directory you must start it with "./~".

If input is a zero-length string then the pwd is returned with a slash added to the end.

bool filename_exist const char *  name  ) 
 

Returns true if the file exists .

const char* filename_ext const char *  buf  ) 
 

Returns a pointer to the last period in filename_name(f), or a pointer to the trailing nul if none. Notice that this points at the period, not after it!

bool filename_isdir const char *  name  ) 
 

Returns true if the file exists and is a directory.

bool filename_match const char *  s,
const char *  p
 

Returns true if filename s matches pattern p. The following glob syntax is used by pattern:

  • "\x" quotes the character x so it must be matched exactly
  • "*" matches any sequence of 0 or more characters.
  • "?" matches any single character.
  • "[set]" matches any character in the set. Set can contain any single characters, or a-z to represent a range. To match ] or - they must be the first characters. To match ^ or ! they must not be the first characters.
  • "[^set]" or "[!set]" matches any character not in the set.
  • "{X|Y|Z}" or "{X,Y,Z}" matches any one of the subexpressions literally.
  • lowercase letters match both upper and lowercase
  • all other characters must match exactly

long int filename_mtime const char *  name  ) 
 

Returns the modification time of the file as a Unix timestamp (number of seconds since the start of 1970 in GMT). Returns 0 if the file does not exist.

const char* filename_name const char *  name  ) 
 

Returns a pointer to after the last slash in name. If the name ends with a slash then this returns a pointer to the NUL. If there is no slash this returns a pointer to the start of name.

int filename_relative char *  to,
int  tolen,
const char *  from,
const char *  cwd
 

Does the opposite of filename_absolute(). Produces the shortest possible name in output that is relative to the current directory. If the filename does not start with any text that matches the current directory then it is returned unchanged.

Return value is the number of characters it wants to write to output.

double filename_size const char *  name  ) 
 

Returns the size of the file in bytes. Returns zero if it does not exist.


Sun Jan 7 00:55:16 2007. FLTK ©2006 Bill Spitzak and others.
Permission is granted to reproduce this manual or any portion for any purpose, provided this copyright and permission notice are preserved.