SphinxBase 0.6
src/libsphinxbase/util/filename.c
00001 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
00002 /* ====================================================================
00003  * Copyright (c) 1999-2004 Carnegie Mellon University.  All rights
00004  * reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  *
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer. 
00012  *
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in
00015  *    the documentation and/or other materials provided with the
00016  *    distribution.
00017  *
00018  * This work was supported in part by funding from the Defense Advanced 
00019  * Research Projects Agency and the National Science Foundation of the 
00020  * United States of America, and the CMU Sphinx Speech Consortium.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 
00023  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
00024  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00025  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
00026  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00027  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
00028  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
00029  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
00030  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
00031  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
00032  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  * ====================================================================
00035  *
00036  */
00037 /*
00038  * filename.c -- File and path name operations.
00039  *
00040  * **********************************************
00041  * CMU ARPA Speech Project
00042  *
00043  * Copyright (c) 1999 Carnegie Mellon University.
00044  * ALL RIGHTS RESERVED.
00045  * **********************************************
00046  * 
00047  * HISTORY
00048  * $Log: filename.c,v $
00049  * Revision 1.5  2005/06/22 03:01:07  arthchan2003
00050  * Added  keyword
00051  *
00052  * Revision 1.3  2005/03/30 01:22:48  archan
00053  * Fixed mistakes in last updates. Add
00054  *
00055  * 
00056  * 30-Oct-1997  M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University.
00057  *              Started.
00058  */
00059 
00060 
00061 #include <stdio.h>
00062 #include <stdlib.h>
00063 #include <string.h>
00064 #include <assert.h>
00065 
00066 #include "sphinxbase/filename.h"
00067 
00068 #ifdef _MSC_VER
00069 #pragma warning (disable: 4996)
00070 #endif
00071 
00072 
00073 /* Strip off all leading pathname components */
00074 void
00075 path2basename(const char *path, char *base)
00076 {
00077     int32 i, l;
00078 
00079     l = strlen(path);
00080 #if defined(_WIN32) || defined(__CYGWIN__)
00081     for (i = l - 1; (i >= 0) && !(path[i] == '/' || path[i] == '\\'); --i);
00082 #else
00083     for (i = l - 1; (i >= 0) && !(path[i] == '/'); --i);
00084 #endif
00085     strcpy(base, path + i + 1);
00086 }
00087 
00088 /* Return all leading pathname components */
00089 void
00090 path2dirname(const char *path, char *dir)
00091 {
00092     int32 i, l;
00093 
00094     l = strlen(path);
00095 #if defined(_WIN32) || defined(__CYGWIN__)
00096     for (i = l - 1; (i >= 0) && !(path[i] == '/' || path[i] == '\\'); --i);
00097 #else
00098     for (i = l - 1; (i >= 0) && !(path[i] == '/'); --i);
00099 #endif
00100     if (i <= 0)
00101         dir[0] = '\0';
00102     else {
00103         memcpy(dir, path, i);
00104         dir[i] = '\0';
00105     }
00106 }
00107 
00108 
00109 /* Strip off the shortest trailing .xyz suffix */
00110 void
00111 strip_fileext(const char *path, char *root)
00112 {
00113     int32 i, l;
00114 
00115     l = strlen(path);
00116     for (i = l - 1; (i >= 0) && (path[i] != '.'); --i);
00117     if (i < 0)
00118         strcpy(root, path);     /* Didn't find a . */
00119     else {
00120         strncpy(root, path, i);
00121     }
00122 }
00123 
00124 /* Test if this path is absolute. */
00125 int
00126 path_is_absolute(const char *path)
00127 {
00128 #if defined(_WIN32) && !defined(_WIN32_WCE) /* FIXME: Also SymbianOS */
00129     return /* Starts with drive letter : \ or / */
00130         (strlen(path) >= 3
00131          &&
00132          ((path[0] >= 'A' && path[0] <= 'Z')
00133           || (path[0] >= 'a' && path[0] <= 'z'))
00134          && path[1] == ':'
00135          && (path[2] == '/' || path[2] == '\\'));
00136 #elif defined(_WIN32_WCE)
00137     return path[0] == '\\' || path[0] == '/';
00138 #else /* Assume Unix */
00139     return path[0] == '/';
00140 #endif
00141 }