softcount: tolerate zero ngrams
[vspell.git] / libvspell / lm / CM_funcs.c
blob9eccf3286d688f91134569d7495e98c6aec8da2c
1 /* ====================================================================
2 * Copyright (c) 1999-2001 Carnegie Mellon University. All rights
3 * reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
17 * This work was supported in part by funding from the Defense Advanced
18 * Research Projects Agency and the National Science Foundation of the
19 * United States of America, and the CMU Sphinx Speech Consortium.
21 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
22 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
25 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * ====================================================================
36 /* $Header: /cvsroot/cmusphinx/sphinx2/src/libsphinx2/CM_funcs.c,v 1.8 2004/05/18 19:02:40 egouvea Exp $
37 * DESCRIPTION
38 * CMPSL functions to make error handling a little easier.
41 #include <stdio.h>
42 #include <string.h>
43 #include <stdlib.h>
45 #ifdef WIN32
46 #include <posixwin32.h>
47 #endif
49 #include <sys/types.h>
51 #include "s2types.h"
52 #include "CM_macros.h"
54 FILE *
55 _CM_fopen (char const *file, char const *mode, char const *srcfile, int32 srcline)
57 FILE *fs = fopen (file, mode);
59 if (fs == NULL) {
60 fprintf (stdout, "%s(%d): fopen(%s,%s) failed\n",
61 srcfile, srcline, file, mode);
62 perror ("fopen");
63 exit (-1);
66 return (fs);
69 FILE *
70 _CM_fopenp (char const *dirl, char const *file, char const *mode,
71 char const *srcfile, int32 srcline)
73 char buffer[2048];
74 FILE *fs;
76 sprintf (buffer, "%s/%s", dirl, file);
77 fs = (FILE *) fopen (buffer, mode);
79 if (fs == NULL) {
80 fprintf (stdout, "%s(%d): fopen(%s,%s) failed\n", srcfile, srcline,
81 buffer, mode);
82 perror ("fopen");
83 exit (-1);
86 return (fs);
89 void *_CM_calloc (int32 cnt, int32 size, char const *file, int32 line)
91 void *ret;
93 if (cnt == 0)
94 return 0;
96 ret = calloc ((size_t)cnt, (size_t)size);
97 if (ret == 0) {
98 fprintf (stdout, "%s(%d): calloc(%d,%d) failed\n", file, line, cnt, size);
99 exit (-1);
101 return (ret);
104 void *_CM_2dcalloc (int32 rcnt, int32 ccnt, int32 size,
105 char const *srcfile, int32 srcline)
106 /*------------------------------------------------------------*
107 * DESCRIPTION - allocate row pointers and data in one chunk
110 char *ret;
111 caddr_t *rowPtr;
112 int32 r;
114 if ((rcnt == 0) || (ccnt == 0))
115 return 0;
117 ret = calloc ((size_t)(rcnt * ccnt * size) +
118 rcnt * sizeof(caddr_t), 1);
119 rowPtr = (caddr_t *) ret;
121 if (ret == 0) {
122 fprintf (stdout, "%s(%d): CM_2dcalloc(%d,%d,%d) failed\n", srcfile, srcline,
123 rcnt, ccnt, size);
124 exit (-1);
127 for (r = 0; r < rcnt; r++)
128 rowPtr[r] = (caddr_t)(ret + (rcnt * sizeof(caddr_t)) + (r * ccnt * size));
130 return (ret);
133 void *_CM_3dcalloc (int32 lcnt, int32 rcnt, int32 ccnt, int32 size,
134 char const *srcfile, int32 srcline)
135 /*------------------------------------------------------------*
136 * DESCRIPTION - allocate row pointers and data in one chunk
139 char *ret;
140 caddr_t *rowPtr;
141 caddr_t *lvlPtr;
142 int32 r, l;
144 ret = (char *) calloc ((size_t)(lcnt * rcnt * ccnt * size) +
145 (size_t)(lcnt * rcnt * sizeof(caddr_t)) +
146 (size_t)(lcnt * sizeof(caddr_t)), 1);
147 rowPtr = (caddr_t *) ret;
148 lvlPtr = (caddr_t *) ret;
150 if (ret == 0) {
151 fprintf (stdout, "%s(%d): CM_3dcalloc(%d,%d,%d) failed\n",
152 srcfile, srcline, rcnt, ccnt, size);
153 exit (-1);
156 for (l = 0; l < lcnt; l++) {
157 lvlPtr[l] = ret + (lcnt * sizeof(caddr_t)) + (rcnt * sizeof(caddr_t) * l);
158 rowPtr = (caddr_t *) lvlPtr[l];
159 for (r = 0; r < rcnt; r++) {
160 rowPtr[r] = (caddr_t)(ret + (lcnt * sizeof(caddr_t)) +
161 (lcnt * rcnt * sizeof(caddr_t)) +
162 (l * rcnt * ccnt * size) +
163 (r * ccnt * size));
167 return (ret);
170 void *_CM_recalloc (void *ptr, int32 cnt, int32 size,
171 char const *srcfile, int32 srcline)
173 void *ret;
175 if (ptr == 0)
176 ret = calloc ((size_t)cnt, (size_t)size);
177 else
178 ret = realloc (ptr, (size_t)size * (size_t)cnt);
180 if (ret == 0) {
181 fprintf (stdout, "%s(%d): recalloc(0x%lX,%d,%d) failed\n", srcfile, srcline,
182 (unsigned long) ptr, cnt, size);
183 exit (-1);
185 return (ret);