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