Tesseract  3.02
tesseract-ocr/cutil/cutil.cpp
Go to the documentation of this file.
00001 /* -*-C-*-
00002  ********************************************************************************
00003  *
00004  * File:         cutil.c
00005  * Description:  General utility functions
00006  * Author:       Mark Seaman, SW Productivity
00007  * Created:      Fri Oct 16 14:37:00 1987
00008  * Modified:     Wed Jun  6 16:29:17 1990 (Mark Seaman) marks@hpgrlt
00009  * Language:     C
00010  * Package:      N/A
00011  * Status:       Reusable Software Component
00012  *
00013  * (c) Copyright 1987, Hewlett-Packard Company.
00014  ** Licensed under the Apache License, Version 2.0 (the "License");
00015  ** you may not use this file except in compliance with the License.
00016  ** You may obtain a copy of the License at
00017  ** http://www.apache.org/licenses/LICENSE-2.0
00018  ** Unless required by applicable law or agreed to in writing, software
00019  ** distributed under the License is distributed on an "AS IS" BASIS,
00020  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00021  ** See the License for the specific language governing permissions and
00022  ** limitations under the License.
00023  *
00024  ********************************************************************************
00025 Revision 1.1  2007/02/02 23:39:07  theraysmith
00026 Fixed portability issues
00027 
00028 Revision 1.1.1.1  2004/02/20 19:39:06  slumos
00029 Import original HP distribution
00030 
00031  * Revision 1.3  90/03/06  15:39:10  15:39:10  marks (Mark Seaman)
00032  * Look for correct file of <malloc.h> or <stdlib.h>
00033  *
00034  * Revision 1.2  90/01/15  13:02:13  13:02:13  marks (Mark Seaman)
00035  * Added memory allocator (*allocate) and (*deallocate)
00036  *
00037  * Revision 1.1  89/10/09  14:58:29  14:58:29  marks (Mark Seaman)
00038  * Initial revision
00039  **/
00040 
00041 #include "cutil.h"
00042 #include "tprintf.h"
00043 #include "callcpp.h"
00044 
00045 #include <stdlib.h>
00046 
00047 #define RESET_COUNT 2000
00048 
00049 /**********************************************************************
00050  * long_rand
00051  *
00052  *  Return a long random number whose value is less than limit.  Do this
00053  *  by calling the standard cheepo random number generator and reseting
00054  *  it pretty often.
00055  **********************************************************************/
00056 long long_rand(long limit) {
00057 #if RAND_MAX < 0x1000000
00058   static long seed;
00059 
00060   long num;
00061   num = (long) rand () << 16;
00062   num |= rand () & 0xffff;
00063   seed ^= num;
00064   long result = num % limit;
00065   while (result < 0) {
00066     result += limit;
00067   }
00068   return result;
00069 #else
00070   return (long)((double)limit * rand()/(RAND_MAX + 1.0));
00071 #endif
00072 }
00073 
00074 
00075 /**********************************************************************
00076  * open_file
00077  *
00078  *  Open a file for reading or writing.  If the file name parameter is
00079  *  NULL use stdin (or stdout) for the file.  If the file can not be
00080  *  opened then call the error routine.
00081  **********************************************************************/
00082 FILE *open_file(const char *filename, const char *mode) {
00083   FILE *thisfile = NULL;
00084   if ((thisfile = fopen (filename, mode)) == NULL) {
00085     tprintf ("Could not open file, %s\n", filename);
00086     exit (1);
00087   }
00088   return (thisfile);
00089 }
00090 
00092 bool exists_file(const char *filename) {
00093   bool exists = false;
00094   FILE *f = NULL;
00095   if ((f = fopen(filename, "rb")) != NULL) {
00096     fclose(f);
00097     exists = true;
00098   }
00099   return exists;
00100 }