1 /* -*-C-*-
2 ################################################################################
3 #
4 # File: array.c
5 # Description: Dynamic Array of Strings
6 # Author: Mark Seaman, Software Productivity
7 # Created: Thu Jul 23 13:24:09 1987
8 # Modified: Wed Mar 6 15:18:33 1991 (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
26 This file contains the implentations of a set of dynamic array of string
27 manipulation routines. For the interface definitions and documentation
28 of these routines see the file "das.h".
29
30 ***************************************************************************/
31
32 #include "tessarray.h"
33 #include "callcpp.h"
34 #include "freelist.h"
35
36 #include <stdio.h>
37 #include <string.h>
38 #ifdef __MSW32__
39 #include <process.h>
40 #endif
41 #include <ctype.h>
42 #if MAC_OR_DOS
43 #include <stdlib.h>
44 #endif
45
46 /**********************************************************************
47 * array_insert
48 *
49 * Insert a data element into a particular spot in the array. Move all
50 * the elements in the array (past that spot) down one to make room for
51 * the new element.
52 **********************************************************************/
array_insert(ARRAY array,int index,void * value)53 ARRAY array_insert(ARRAY array, int index, void *value) {
54 int x;
55
56 array = array_push (array, NULL);
57 for (x = array_count (array) - 1; x > index; x--)
58 array_value (array, x) = array_value (array, x - 1);
59 array_value (array, index) = value;
60 return (array);
61 }
62
63
64 /**********************************************************************
65 * array_new
66 *
67 * Create a new array with a certain number of elements. If the number
68 * of elements requested is 0 then the default number will be used.
69 **********************************************************************/
array_new(int num)70 ARRAY array_new(int num) {
71 ARRAY temp;
72 int x;
73
74 if (num == 0)
75 num = DEFAULT_SIZE;
76 temp = (ARRAY) memalloc ((num - 2) * sizeof (char *) +
77 sizeof (struct array_record));
78 if (!temp) {
79 cprintf ("error: Out of memory in array_new\n");
80 exit (1); //?err_exit ();
81 }
82 array_count (temp) = 0;
83 array_limit (temp) = num;
84 for (x = 0; x < num; x++)
85 array_value (temp, x) = (char *) 0;
86 return (temp);
87 }
88
89
90 /**********************************************************************
91 * array_push
92 *
93 * Add a new element onto the top of the array. If there is not room
94 * more room is made by "realloc"ing the array. This means that the
95 * new array location may change. All previous references to its old
96 * location may no longer be valid.
97 **********************************************************************/
array_push(ARRAY array,void * value)98 ARRAY array_push(ARRAY array, void *value) {
99 if (array_count (array) == array_limit (array)) {
100 array = (ARRAY) memrealloc (array, (array_limit (array) * 2 - 2) *
101 sizeof (char *) +
102 sizeof (struct array_record),
103 (array_limit (array) -
104 2) * sizeof (char *) +
105 sizeof (struct array_record));
106 if (!array) {
107 cprintf ("error: Out of memory in array_push\n");
108 exit (1); //?err_exit ();
109 }
110 array_limit (array) *= 2;
111 }
112 array_count (array)++;
113 array_top (array) = value;
114 return (array);
115 }
116