All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
basedir.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: basedir.c (Formerly getpath.c)
3  * Description: Find the directory location of the current executable using PATH.
4  * Author: Ray Smith
5  * Created: Mon Jul 09 09:06:39 BST 1990
6  *
7  * (C) Copyright 1990, Hewlett-Packard Ltd.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  **********************************************************************/
19 
20 #include "basedir.h"
21 
22 #include <stdlib.h>
23 
24 // Assuming that code_path is the name of some file in a desired directory,
25 // returns the given code_path stripped back to the last slash, leaving
26 // the last slash in place. If there is no slash, returns ./ assuming that
27 // the input was the name of something in the current directory.
28 // Useful for getting to the directory of argv[0], but does not search
29 // any paths.
30 TESS_API void truncate_path(const char *code_path, STRING* trunc_path) {
31  int trunc_index = -1;
32  if (code_path != NULL) {
33  const char* last_slash = strrchr(code_path, '/');
34  if (last_slash != NULL && last_slash + 1 - code_path > trunc_index)
35  trunc_index = last_slash + 1 - code_path;
36  last_slash = strrchr(code_path, '\\');
37  if (last_slash != NULL && last_slash + 1 - code_path > trunc_index)
38  trunc_index = last_slash + 1 - code_path;
39  }
40  *trunc_path = code_path;
41  if (trunc_index >= 0)
42  trunc_path->truncate_at(trunc_index);
43  else
44  *trunc_path = "./";
45 }
void truncate_at(inT32 index)
Definition: strngs.cpp:264
#define TESS_API
Definition: platform.h:73
Definition: strngs.h:44
#define NULL
Definition: host.h:144
TESS_API void truncate_path(const char *code_path, STRING *trunc_path)
Definition: basedir.cpp:30