Tesseract 3.02

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cutil.cpp
Go to the documentation of this file.
1 /* -*-C-*-
2 ********************************************************************************
3 *
4 * File: cutil.c
5 * Description: General utility functions
6 * Author: Mark Seaman, SW Productivity
7 * Created: Fri Oct 16 14:37:00 1987
8 * Modified: Wed Jun 6 16:29:17 1990 (Mark Seaman) marks@hpgrlt
9 * Language: C
10 * Package: N/A
11 * Status: Reusable Software Component
12 *
13 * (c) Copyright 1987, Hewlett-Packard Company.
14 ** Licensed under the Apache License, Version 2.0 (the "License");
15 ** you may not use this file except in compliance with the License.
16 ** You may obtain a copy of the License at
17 ** http://www.apache.org/licenses/LICENSE-2.0
18 ** Unless required by applicable law or agreed to in writing, software
19 ** distributed under the License is distributed on an "AS IS" BASIS,
20 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 ** See the License for the specific language governing permissions and
22 ** limitations under the License.
23 *
24 ********************************************************************************
25 Revision 1.1 2007/02/02 23:39:07 theraysmith
26 Fixed portability issues
27
28 Revision 1.1.1.1 2004/02/20 19:39:06 slumos
29 Import original HP distribution
30
31 * Revision 1.3 90/03/06 15:39:10 15:39:10 marks (Mark Seaman)
32 * Look for correct file of <malloc.h> or <stdlib.h>
33 *
34 * Revision 1.2 90/01/15 13:02:13 13:02:13 marks (Mark Seaman)
35 * Added memory allocator (*allocate) and (*deallocate)
36 *
37 * Revision 1.1 89/10/09 14:58:29 14:58:29 marks (Mark Seaman)
38 * Initial revision
39 **/
40
41 #include "cutil.h"
42 #include "tprintf.h"
43 #include "callcpp.h"
44
45 #include <stdlib.h>
46
47 #define RESET_COUNT 2000
48
49 /**********************************************************************
50 * long_rand
51 *
52 * Return a long random number whose value is less than limit. Do this
53 * by calling the standard cheepo random number generator and reseting
54 * it pretty often.
55 **********************************************************************/
56 long long_rand(long limit) {
57 #if RAND_MAX < 0x1000000
58 static long seed;
59
60 long num;
61 num = (long) rand () << 16;
62 num |= rand () & 0xffff;
63 seed ^= num;
64 long result = num % limit;
65 while (result < 0) {
66 result += limit;
67 }
68 return result;
69 #else
70 return (long)((double)limit * rand()/(RAND_MAX + 1.0));
71 #endif
72 }
73
74
75 /**********************************************************************
76 * open_file
77 *
78 * Open a file for reading or writing. If the file name parameter is
79 * NULL use stdin (or stdout) for the file. If the file can not be
80 * opened then call the error routine.
81 **********************************************************************/
82 FILE *open_file(const char *filename, const char *mode) {
83 FILE *thisfile = NULL;
84 if ((thisfile = fopen (filename, mode)) == NULL) {
85 tprintf ("Could not open file, %s\n", filename);
86 exit (1);
87 }
88 return (thisfile);
89 }
90
92 bool exists_file(const char *filename) {
93 bool exists = false;
94 FILE *f = NULL;
95 if ((f = fopen(filename, "rb")) != NULL) {
96 fclose(f);
97 exists = true;
98 }
99 return exists;
100 }