tesseract
3.03
|
00001 /********************************************************************** 00002 * File: basedir.c (Formerly getpath.c) 00003 * Description: Find the directory location of the current executable using PATH. 00004 * Author: Ray Smith 00005 * Created: Mon Jul 09 09:06:39 BST 1990 00006 * 00007 * (C) Copyright 1990, Hewlett-Packard Ltd. 00008 ** Licensed under the Apache License, Version 2.0 (the "License"); 00009 ** you may not use this file except in compliance with the License. 00010 ** You may obtain a copy of the License at 00011 ** http://www.apache.org/licenses/LICENSE-2.0 00012 ** Unless required by applicable law or agreed to in writing, software 00013 ** distributed under the License is distributed on an "AS IS" BASIS, 00014 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 ** See the License for the specific language governing permissions and 00016 ** limitations under the License. 00017 * 00018 **********************************************************************/ 00019 00020 #include "basedir.h" 00021 00022 #include <stdlib.h> 00023 00024 // Assuming that code_path is the name of some file in a desired directory, 00025 // returns the given code_path stripped back to the last slash, leaving 00026 // the last slash in place. If there is no slash, returns ./ assuming that 00027 // the input was the name of something in the current directory. 00028 // Useful for getting to the directory of argv[0], but does not search 00029 // any paths. 00030 TESS_API void truncate_path(const char *code_path, STRING* trunc_path) { 00031 int trunc_index = -1; 00032 if (code_path != NULL) { 00033 const char* last_slash = strrchr(code_path, '/'); 00034 if (last_slash != NULL && last_slash + 1 - code_path > trunc_index) 00035 trunc_index = last_slash + 1 - code_path; 00036 last_slash = strrchr(code_path, '\\'); 00037 if (last_slash != NULL && last_slash + 1 - code_path > trunc_index) 00038 trunc_index = last_slash + 1 - code_path; 00039 } 00040 *trunc_path = code_path; 00041 if (trunc_index >= 0) 00042 trunc_path->truncate_at(trunc_index); 00043 else 00044 *trunc_path = "./"; 00045 }