SphinxBase 0.6
filename.c
1/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/* ====================================================================
3 * Copyright (c) 1999-2004 Carnegie Mellon University. All rights
4 * reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * This work was supported in part by funding from the Defense Advanced
19 * Research Projects Agency and the National Science Foundation of the
20 * United States of America, and the CMU Sphinx Speech Consortium.
21 *
22 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
23 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
26 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 * ====================================================================
35 *
36 */
37/*
38 * filename.c -- File and path name operations.
39 *
40 * **********************************************
41 * CMU ARPA Speech Project
42 *
43 * Copyright (c) 1999 Carnegie Mellon University.
44 * ALL RIGHTS RESERVED.
45 * **********************************************
46 *
47 * HISTORY
48 * $Log: filename.c,v $
49 * Revision 1.5 2005/06/22 03:01:07 arthchan2003
50 * Added keyword
51 *
52 * Revision 1.3 2005/03/30 01:22:48 archan
53 * Fixed mistakes in last updates. Add
54 *
55 *
56 * 30-Oct-1997 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University.
57 * Started.
58 */
59
60
61#include <stdio.h>
62#include <stdlib.h>
63#include <string.h>
64#include <assert.h>
65
66#include "sphinxbase/filename.h"
67
68#ifdef _MSC_VER
69#pragma warning (disable: 4996)
70#endif
71
72
73/* Strip off all leading pathname components */
74void
75path2basename(const char *path, char *base)
76{
77 int32 i, l;
78
79 l = strlen(path);
80#if defined(_WIN32) || defined(__CYGWIN__)
81 for (i = l - 1; (i >= 0) && !(path[i] == '/' || path[i] == '\\'); --i);
82#else
83 for (i = l - 1; (i >= 0) && !(path[i] == '/'); --i);
84#endif
85 strcpy(base, path + i + 1);
86}
87
88/* Return all leading pathname components */
89void
90path2dirname(const char *path, char *dir)
91{
92 int32 i, l;
93
94 l = strlen(path);
95#if defined(_WIN32) || defined(__CYGWIN__)
96 for (i = l - 1; (i >= 0) && !(path[i] == '/' || path[i] == '\\'); --i);
97#else
98 for (i = l - 1; (i >= 0) && !(path[i] == '/'); --i);
99#endif
100 if (i <= 0)
101 dir[0] = '\0';
102 else {
103 memcpy(dir, path, i);
104 dir[i] = '\0';
105 }
106}
107
108
109/* Strip off the shortest trailing .xyz suffix */
110void
111strip_fileext(const char *path, char *root)
112{
113 int32 i, l;
114
115 l = strlen(path);
116 for (i = l - 1; (i >= 0) && (path[i] != '.'); --i);
117 if (i < 0)
118 strcpy(root, path); /* Didn't find a . */
119 else {
120 strncpy(root, path, i);
121 }
122}
123
124/* Test if this path is absolute. */
125int
126path_is_absolute(const char *path)
127{
128#if defined(_WIN32) && !defined(_WIN32_WCE) /* FIXME: Also SymbianOS */
129 return /* Starts with drive letter : \ or / */
130 (strlen(path) >= 3
131 &&
132 ((path[0] >= 'A' && path[0] <= 'Z')
133 || (path[0] >= 'a' && path[0] <= 'z'))
134 && path[1] == ':'
135 && (path[2] == '/' || path[2] == '\\'));
136#elif defined(_WIN32_WCE)
137 return path[0] == '\\' || path[0] == '/';
138#else /* Assume Unix */
139 return path[0] == '/';
140#endif
141}
File names related operation.
SPHINXBASE_EXPORT void path2dirname(const char *path, char *dir)
Strip off filename from the given path and copy the directory name into dir Caller must have allocate...
Definition filename.c:90
SPHINXBASE_EXPORT int path_is_absolute(const char *file)
Test whether a pathname is absolute for the current OS.
Definition filename.c:126
SPHINXBASE_EXPORT void strip_fileext(const char *file, char *root)
Strip off the smallest trailing file-extension suffix and copy the rest into the given root argument.
Definition filename.c:111
SPHINXBASE_EXPORT void path2basename(const char *path, char *base)
Strip off leading path components from the given path and copy the base into base.
Definition filename.c:75