Tesseract
3.02
|
00001 /* -*-C-*- 00002 ################################################################################ 00003 # 00004 # File: array.c 00005 # Description: Dynamic Array of Strings 00006 # Author: Mark Seaman, Software Productivity 00007 # Created: Thu Jul 23 13:24:09 1987 00008 # Modified: Wed Mar 6 15:18:33 1991 (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 00026 This file contains the implentations of a set of dynamic array of string 00027 manipulation routines. For the interface definitions and documentation 00028 of these routines see the file "das.h". 00029 00030 ***************************************************************************/ 00031 00032 #include "tessarray.h" 00033 #include "callcpp.h" 00034 #include "freelist.h" 00035 00036 #include <stdio.h> 00037 #include <string.h> 00038 #ifdef _WIN32 00039 #include <process.h> 00040 #endif 00041 #include <ctype.h> 00042 #if MAC_OR_DOS 00043 #include <stdlib.h> 00044 #endif 00045 00046 /********************************************************************** 00047 * array_insert 00048 * 00049 * Insert a data element into a particular spot in the array. Move all 00050 * the elements in the array (past that spot) down one to make room for 00051 * the new element. 00052 **********************************************************************/ 00053 ARRAY array_insert(ARRAY array, int index, void *value) { 00054 int x; 00055 00056 array = array_push (array, NULL); 00057 for (x = array_count (array) - 1; x > index; x--) 00058 array_value (array, x) = array_value (array, x - 1); 00059 array_value (array, index) = value; 00060 return (array); 00061 } 00062 00063 00064 /********************************************************************** 00065 * array_new 00066 * 00067 * Create a new array with a certain number of elements. If the number 00068 * of elements requested is 0 then the default number will be used. 00069 **********************************************************************/ 00070 ARRAY array_new(int num) { 00071 ARRAY temp; 00072 int x; 00073 00074 if (num == 0) 00075 num = DEFAULT_SIZE; 00076 temp = (ARRAY) memalloc ((num - 2) * sizeof (char *) + 00077 sizeof (struct array_record)); 00078 if (!temp) { 00079 cprintf ("error: Out of memory in array_new\n"); 00080 exit (1); //?err_exit (); 00081 } 00082 array_count (temp) = 0; 00083 array_limit (temp) = num; 00084 for (x = 0; x < num; x++) 00085 array_value (temp, x) = (char *) 0; 00086 return (temp); 00087 } 00088 00089 00090 /********************************************************************** 00091 * array_push 00092 * 00093 * Add a new element onto the top of the array. If there is not room 00094 * more room is made by "realloc"ing the array. This means that the 00095 * new array location may change. All previous references to its old 00096 * location may no longer be valid. 00097 **********************************************************************/ 00098 ARRAY array_push(ARRAY array, void *value) { 00099 if (array_count (array) == array_limit (array)) { 00100 array = (ARRAY) memrealloc (array, (array_limit (array) * 2 - 2) * 00101 sizeof (char *) + 00102 sizeof (struct array_record), 00103 (array_limit (array) - 00104 2) * sizeof (char *) + 00105 sizeof (struct array_record)); 00106 if (!array) { 00107 cprintf ("error: Out of memory in array_push\n"); 00108 exit (1); //?err_exit (); 00109 } 00110 array_limit (array) *= 2; 00111 } 00112 array_count (array)++; 00113 array_top (array) = value; 00114 return (array); 00115 }