JWS C Library
C language utility library
fname.c
Go to the documentation of this file.
00001 /*
00002  * This work is licensed under a Creative Commons 
00003  * Attribution-Noncommercial-Share Alike 3.0 United States License.
00004  * 
00005  *    http://creativecommons.org/licenses/by-nc-sa/3.0/us/
00006  * 
00007  * You are free:
00008  * 
00009  *    to Share - to copy, distribute, display, and perform the work
00010  *    to Remix - to make derivative works
00011  * 
00012  * Under the following conditions:
00013  * 
00014  *    Attribution. You must attribute the work in the manner specified by the
00015  *    author or licensor (but not in any way that suggests that they endorse you
00016  *    or your use of the work).
00017  * 
00018  *    Noncommercial. You may not use this work for commercial purposes.
00019  * 
00020  *    Share Alike. If you alter, transform, or build upon this work, you may
00021  *    distribute the resulting work only under the same or similar license to
00022  *    this one.
00023  * 
00024  * For any reuse or distribution, you must make clear to others the license
00025  * terms of this work. The best way to do this is by including this header.
00026  * 
00027  * Any of the above conditions can be waived if you get permission from the
00028  * copyright holder.
00029  * 
00030  * Apart from the remix rights granted under this license, nothing in this
00031  * license impairs or restricts the author's moral rights.
00032  */
00033 
00034 
00046 #include <jwsc/config.h>
00047 
00048 #include <stdlib.h>
00049 #include <string.h>
00050 #include <assert.h>
00051 #include <inttypes.h>
00052 
00053 #include "jwsc/base/fname.h"
00054 
00055 #ifdef JWSC_HAVE_DMALLOC
00056 #include <dmalloc.h>
00057 #endif
00058 
00059 
00065 static const char* last_occurrence(const char* fname, char c)
00066 {
00067     int len = strlen(fname);
00068     const char* fptr = fname+len-1;
00069 
00070     if (len == 1 && *fptr == c)
00071     {
00072         return fptr;
00073     }
00074 
00075     while (fptr != fname && *fptr != c)
00076     {
00077         fptr--;
00078     }
00079 
00080     if (fptr == fname && *fptr != c)
00081     {
00082         return 0;
00083     }
00084 
00085     return fptr;
00086 }
00087 
00088 
00096 int fname_has_suffix(const char* fname, const char* suffix)
00097 {
00098     char* fsuffix = NULL;
00099     int len = strlen(suffix);
00100     int rval = 0;
00101 
00102     if ((fsuffix = get_fname_suffix(fname)))
00103     {
00104         rval = (strncmp(fsuffix, suffix, len) == 0);
00105     }
00106 
00107     free(fsuffix);
00108     return rval;
00109 }
00110 
00111 
00120 char* get_fname_suffix(const char* fname)
00121 {
00122     const char* fsuffix;
00123     char*       suffix;
00124 
00125     if ((fsuffix = last_occurrence(fname, '.')) == 0)
00126     {
00127         return 0;
00128     }
00129 
00130     fsuffix++;
00131 
00132     suffix = malloc((strlen(fsuffix)+1)*sizeof(char));
00133     strcpy(suffix, fsuffix);
00134 
00135     return suffix;
00136 }
00137 
00138 
00147 char* get_fname_dir(const char* fname)
00148 {
00149     uint32_t    len = strlen(fname);
00150     const char* fdir;
00151     char*       dir;
00152 
00153     /* remove any trailing slashes and try again */
00154     if (len > 1 && fname[ len-1 ] == '/')
00155     {
00156         char* dir2 = malloc(len*sizeof(char));
00157         strncpy(dir2, fname, len);
00158         dir2[len-1]='\0';
00159         dir = get_fname_dir(dir2);
00160         free(dir2);
00161         return dir;
00162     }
00163 
00164     /* copy file directory name */
00165     fdir = last_occurrence(fname, '/');
00166     len = fdir - fname;
00167 
00168     if (fdir == 0)
00169     {
00170         fname = ".";
00171         len = 1;
00172     }
00173     else if (len == 0)
00174     {
00175         fname = "/";
00176         len = 1;
00177     }
00178 
00179     dir = malloc((len+1)*sizeof(char));
00180     strncpy(dir, fname, len);
00181     dir[len]='\0';
00182 
00183     return dir;
00184 }
00185 
00186 
00194 char* get_fname_base(const char* fname)
00195 {
00196     uint32_t    len = strlen(fname);
00197     const char* fbase;
00198     char*       base;
00199 
00200     /* remove any trailing slashes and try again */
00201     if (len > 1 && fname[ len-1 ] == '/')
00202     {
00203         char* base2 = malloc(len*sizeof(char));
00204         strncpy(base2, fname, len);
00205         base2[len-1]='\0';
00206         base = get_fname_base(base2);
00207         free(base2);
00208         return base;
00209     }
00210 
00211     /* copy file base name */
00212     fbase = last_occurrence(fname, '/');
00213 
00214     if (fbase == 0 || len == 1)
00215     {
00216         fbase = fname;
00217         len = strlen(fname);
00218     }
00219     else
00220     {
00221         fbase++;
00222     }
00223 
00224     base = malloc((strlen(fbase)+1)*sizeof(char));
00225     strcpy(base, fbase);
00226 
00227     return base;
00228 }