SphinxBase 5prealpha
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#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#include <assert.h>
45
46#include "sphinxbase/filename.h"
47
48#ifdef _MSC_VER
49#pragma warning (disable: 4996)
50#endif
51
52const char *
53path2basename(const char *path)
54{
55 const char *result;
56
57#if defined(_WIN32) || defined(__CYGWIN__)
58 result = strrchr(path, '\\');
59#else
60 result = strrchr(path, '/');
61#endif
62
63 return (result == NULL ? path : result + 1);
64}
65
66/* Return all leading pathname components */
67void
68path2dirname(const char *path, char *dir)
69{
70 size_t i, l;
71
72 l = strlen(path);
73#if defined(_WIN32) || defined(__CYGWIN__)
74 for (i = l - 1; (i > 0) && !(path[i] == '/' || path[i] == '\\'); --i);
75#else
76 for (i = l - 1; (i > 0) && !(path[i] == '/'); --i);
77#endif
78 if (i == 0) {
79 dir[0] = '.';
80 dir[1] = '\0';
81 } else {
82 memcpy(dir, path, i);
83 dir[i] = '\0';
84 }
85}
86
87
88/* Strip off the shortest trailing .xyz suffix */
89void
90strip_fileext(const char *path, char *root)
91{
92 size_t i, l;
93
94 l = strlen(path);
95 for (i = l - 1; (i > 0) && (path[i] != '.'); --i);
96 if (i == 0) {
97 strcpy(root, path); /* Didn't find a . */
98 } else {
99 strncpy(root, path, i);
100 }
101}
102
103/* Test if this path is absolute. */
104int
105path_is_absolute(const char *path)
106{
107#if defined(_WIN32) && !defined(_WIN32_WCE) /* FIXME: Also SymbianOS */
108 return /* Starts with drive letter : \ or / */
109 (strlen(path) >= 3
110 &&
111 ((path[0] >= 'A' && path[0] <= 'Z')
112 || (path[0] >= 'a' && path[0] <= 'z'))
113 && path[1] == ':'
114 && (path[2] == '/' || path[2] == '\\'));
115#elif defined(_WIN32_WCE)
116 return path[0] == '\\' || path[0] == '/';
117#else /* Assume Unix */
118 return path[0] == '/';
119#endif
120}
File names related operation.
SPHINXBASE_EXPORT const char * path2basename(const char *path)
Returns the last part of the path, without modifying anything in memory.
Definition filename.c:53
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:68
SPHINXBASE_EXPORT int path_is_absolute(const char *file)
Test whether a pathname is absolute for the current OS.
Definition filename.c:105
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:90