File: HTS_misc.c

package info (click to toggle)
htsengine 1.10-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 908 kB
  • sloc: ansic: 5,369; sh: 4,173; makefile: 37
file content (613 lines) | stat: -rw-r--r-- 16,353 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/* ----------------------------------------------------------------- */
/*           The HMM-Based Speech Synthesis Engine "hts_engine API"  */
/*           developed by HTS Working Group                          */
/*           http://hts-engine.sourceforge.net/                      */
/* ----------------------------------------------------------------- */
/*                                                                   */
/*  Copyright (c) 2001-2015  Nagoya Institute of Technology          */
/*                           Department of Computer Science          */
/*                                                                   */
/*                2001-2008  Tokyo Institute of Technology           */
/*                           Interdisciplinary Graduate School of    */
/*                           Science and Engineering                 */
/*                                                                   */
/* All rights reserved.                                              */
/*                                                                   */
/* Redistribution and use in source and binary forms, with or        */
/* without modification, are permitted provided that the following   */
/* conditions are met:                                               */
/*                                                                   */
/* - Redistributions of source code must retain the above copyright  */
/*   notice, this list of conditions and the following disclaimer.   */
/* - Redistributions in binary form must reproduce the above         */
/*   copyright notice, this list of conditions and the following     */
/*   disclaimer in the documentation and/or other materials provided */
/*   with the distribution.                                          */
/* - Neither the name of the HTS working group nor the names of its  */
/*   contributors may be used to endorse or promote products derived */
/*   from this software without specific prior written permission.   */
/*                                                                   */
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND            */
/* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,       */
/* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF          */
/* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE          */
/* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS */
/* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,          */
/* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED   */
/* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,     */
/* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
/* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,   */
/* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY    */
/* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/* POSSIBILITY OF SUCH DAMAGE.                                       */
/* ----------------------------------------------------------------- */

#ifndef HTS_MISC_C
#define HTS_MISC_C

#ifdef __cplusplus
#define HTS_MISC_C_START extern "C" {
#define HTS_MISC_C_END   }
#else
#define HTS_MISC_C_START
#define HTS_MISC_C_END
#endif                          /* __CPLUSPLUS */

HTS_MISC_C_START;

#include <stdlib.h>             /* for exit(),calloc(),free() */
#include <stdarg.h>             /* for va_list */
#include <string.h>             /* for strcpy(),strlen() */

/* hts_engine libraries */
#include "HTS_hidden.h"

#ifdef FESTIVAL
#include "EST_walloc.h"
#endif                          /* FESTIVAL */

#define HTS_FILE  0
#define HTS_DATA  1

typedef struct _HTS_Data {
   unsigned char *data;
   size_t size;
   size_t index;
} HTS_Data;

/* HTS_fopen_from_fn: wrapper for fopen */
HTS_File *HTS_fopen_from_fn(const char *name, const char *opt)
{
   HTS_File *fp = (HTS_File *) HTS_calloc(1, sizeof(HTS_File));

   fp->type = HTS_FILE;
   fp->pointer = (void *) fopen(name, opt);

   if (fp->pointer == NULL) {
      HTS_error(0, "HTS_fopen: Cannot open %s.\n", name);
      HTS_free(fp);
      return NULL;
   }

   return fp;
}

/* HTS_fopen_from_fp: wrapper for fopen */
HTS_File *HTS_fopen_from_fp(HTS_File * fp, size_t size)
{
   if (fp == NULL || size == 0)
      return NULL;
   else if (fp->type == HTS_FILE) {
      HTS_Data *d;
      HTS_File *f;
      d = (HTS_Data *) HTS_calloc(1, sizeof(HTS_Data));
      d->data = (unsigned char *) HTS_calloc(size, sizeof(unsigned char));
      d->size = size;
      d->index = 0;
      if (fread(d->data, sizeof(unsigned char), size, (FILE *) fp->pointer) != size) {
         free(d->data);
         free(d);
         return NULL;
      }
      f = (HTS_File *) HTS_calloc(1, sizeof(HTS_File));
      f->type = HTS_DATA;
      f->pointer = (void *) d;
      return f;
   } else if (fp->type == HTS_DATA) {
      HTS_File *f;
      HTS_Data *tmp1, *tmp2;
      tmp1 = (HTS_Data *) fp->pointer;
      if (tmp1->index + size > tmp1->size)
         return NULL;
      tmp2 = (HTS_Data *) HTS_calloc(1, sizeof(HTS_Data));
      tmp2->data = (unsigned char *) HTS_calloc(size, sizeof(unsigned char));
      tmp2->size = size;
      tmp2->index = 0;
      memcpy(tmp2->data, &tmp1->data[tmp1->index], size);
      tmp1->index += size;
      f = (HTS_File *) HTS_calloc(1, sizeof(HTS_File));
      f->type = HTS_DATA;
      f->pointer = (void *) tmp2;
      return f;
   }

   HTS_error(0, "HTS_fopen_from_fp: Unknown file type.\n");
   return NULL;
}

/* HTS_fopen_from_data: wrapper for fopen */
HTS_File *HTS_fopen_from_data(void *data, size_t size)
{
   HTS_Data *d;
   HTS_File *f;

   if (data == NULL || size == 0)
      return NULL;

   d = (HTS_Data *) HTS_calloc(1, sizeof(HTS_Data));
   d->data = (unsigned char *) HTS_calloc(size, sizeof(unsigned char));
   d->size = size;
   d->index = 0;

   memcpy(d->data, data, size);

   f = (HTS_File *) HTS_calloc(1, sizeof(HTS_File));
   f->type = HTS_DATA;
   f->pointer = (void *) d;

   return f;
}

/* HTS_fclose: wrapper for fclose */
void HTS_fclose(HTS_File * fp)
{
   if (fp == NULL) {
      return;
   } else if (fp->type == HTS_FILE) {
      if (fp->pointer != NULL)
         fclose((FILE *) fp->pointer);
      HTS_free(fp);
      return;
   } else if (fp->type == HTS_DATA) {
      if (fp->pointer != NULL) {
         HTS_Data *d = (HTS_Data *) fp->pointer;
         if (d->data != NULL)
            HTS_free(d->data);
         HTS_free(d);
      }
      HTS_free(fp);
      return;
   }
   HTS_error(0, "HTS_fclose: Unknown file type.\n");
}

/* HTS_fgetc: wrapper for fgetc */
int HTS_fgetc(HTS_File * fp)
{
   if (fp == NULL) {
      return EOF;
   } else if (fp->type == HTS_FILE) {
      return fgetc((FILE *) fp->pointer);
   } else if (fp->type == HTS_DATA) {
      HTS_Data *d = (HTS_Data *) fp->pointer;
      if (d->size <= d->index)
         return EOF;
      return (int) d->data[d->index++];
   }
   HTS_error(0, "HTS_fgetc: Unknown file type.\n");
   return EOF;
}

/* HTS_feof: wrapper for feof */
int HTS_feof(HTS_File * fp)
{
   if (fp == NULL) {
      return 1;
   } else if (fp->type == HTS_FILE) {
      return feof((FILE *) fp->pointer);
   } else if (fp->type == HTS_DATA) {
      HTS_Data *d = (HTS_Data *) fp->pointer;
      return d->size <= d->index ? 1 : 0;
   }
   HTS_error(0, "HTS_feof: Unknown file type.\n");
   return 1;
}

/* HTS_fseek: wrapper for fseek */
int HTS_fseek(HTS_File * fp, long offset, int origin)
{
   if (fp == NULL) {
      return 1;
   } else if (fp->type == HTS_FILE) {
      return fseek((FILE *) fp->pointer, offset, origin);
   } else if (fp->type == HTS_DATA) {
      HTS_Data *d = (HTS_Data *) fp->pointer;
      if (origin == SEEK_SET) {
         d->index = (size_t) offset;
      } else if (origin == SEEK_CUR) {
         d->index += offset;
      } else if (origin == SEEK_END) {
         d->index = d->size + offset;
      } else {
         return 1;
      }
      return 0;
   }
   HTS_error(0, "HTS_fseek: Unknown file type.\n");
   return 1;
}

/* HTS_ftell: rapper for ftell */
size_t HTS_ftell(HTS_File * fp)
{
   if (fp == NULL) {
      return 0;
   } else if (fp->type == HTS_FILE) {
      fpos_t pos;
      fgetpos((FILE *) fp->pointer, &pos);
#if defined(_WIN32) || defined(__CYGWIN__) || defined(__APPLE__) || defined(__ANDROID__)
      return (size_t) pos;
#else
      return (size_t) pos.__pos;
#endif                          /* _WIN32 || __CYGWIN__ || __APPLE__ || __ANDROID__ */
   } else if (fp->type == HTS_DATA) {
      HTS_Data *d = (HTS_Data *) fp->pointer;
      return d->index;
   }
   HTS_error(0, "HTS_ftell: Unknown file type.\n");
   return 0;
}

/* HTS_fread: wrapper for fread */
static size_t HTS_fread(void *buf, size_t size, size_t n, HTS_File * fp)
{
   if (fp == NULL || size == 0 || n == 0) {
      return 0;
   }
   if (fp->type == HTS_FILE) {
      return fread(buf, size, n, (FILE *) fp->pointer);
   } else if (fp->type == HTS_DATA) {
      HTS_Data *d = (HTS_Data *) fp->pointer;
      size_t i, length = size * n;
      unsigned char *c = (unsigned char *) buf;
      for (i = 0; i < length; i++) {
         if (d->index < d->size)
            c[i] = d->data[d->index++];
         else
            break;
      }
      if (i == 0)
         return 0;
      else
         return i / size;
   }
   HTS_error(0, "HTS_fread: Unknown file type.\n");
   return 0;
}

/* HTS_byte_swap: byte swap */
static void HTS_byte_swap(void *p, size_t size, size_t block)
{
   char *q, tmp;
   size_t i, j;

   q = (char *) p;

   for (i = 0; i < block; i++) {
      for (j = 0; j < (size / 2); j++) {
         tmp = *(q + j);
         *(q + j) = *(q + (size - 1 - j));
         *(q + (size - 1 - j)) = tmp;
      }
      q += size;
   }
}

/* HTS_fread_big_endian: fread with byteswap */
size_t HTS_fread_big_endian(void *buf, size_t size, size_t n, HTS_File * fp)
{
   size_t block = HTS_fread(buf, size, n, fp);

#ifdef WORDS_LITTLEENDIAN
   HTS_byte_swap(buf, size, block);
#endif                          /* WORDS_LITTLEENDIAN */

   return block;
}

/* HTS_fread_little_endian: fread with byteswap */
size_t HTS_fread_little_endian(void *buf, size_t size, size_t n, HTS_File * fp)
{
   size_t block = HTS_fread(buf, size, n, fp);

#ifdef WORDS_BIGENDIAN
   HTS_byte_swap(buf, size, block);
#endif                          /* WORDS_BIGENDIAN */

   return block;
}

/* HTS_fwrite_little_endian: fwrite with byteswap */
size_t HTS_fwrite_little_endian(const void *buf, size_t size, size_t n, FILE * fp)
{
#ifdef WORDS_BIGENDIAN
   HTS_byte_swap(buf, size, n * size);
#endif                          /* WORDS_BIGENDIAN */
   return fwrite(buf, size, n, fp);
}

/* HTS_get_pattern_token: get pattern token (single/double quote can be used) */
HTS_Boolean HTS_get_pattern_token(HTS_File * fp, char *buff)
{
   char c;
   size_t i;
   HTS_Boolean squote = FALSE, dquote = FALSE;

   if (fp == NULL || HTS_feof(fp))
      return FALSE;
   c = HTS_fgetc(fp);

   while (c == ' ' || c == '\n') {
      if (HTS_feof(fp))
         return FALSE;
      c = HTS_fgetc(fp);
   }

   if (c == '\'') {             /* single quote case */
      if (HTS_feof(fp))
         return FALSE;
      c = HTS_fgetc(fp);
      squote = TRUE;
   }

   if (c == '\"') {             /*double quote case */
      if (HTS_feof(fp))
         return FALSE;
      c = HTS_fgetc(fp);
      dquote = TRUE;
   }

   if (c == ',') {              /*special character ',' */
      strcpy(buff, ",");
      return TRUE;
   }

   i = 0;
   while (1) {
      buff[i++] = c;
      c = HTS_fgetc(fp);
      if (squote && c == '\'')
         break;
      if (dquote && c == '\"')
         break;
      if (!squote && !dquote) {
         if (c == ' ')
            break;
         if (c == '\n')
            break;
         if (HTS_feof(fp))
            break;
      }
   }

   buff[i] = '\0';
   return TRUE;
}

/* HTS_get_token: get token from file pointer (separators are space, tab, and line break) */
HTS_Boolean HTS_get_token_from_fp(HTS_File * fp, char *buff)
{
   char c;
   size_t i;

   if (fp == NULL || HTS_feof(fp))
      return FALSE;
   c = HTS_fgetc(fp);
   while (c == ' ' || c == '\n' || c == '\t') {
      if (HTS_feof(fp))
         return FALSE;
      c = HTS_fgetc(fp);
      if (c == EOF)
         return FALSE;
   }

   for (i = 0; c != ' ' && c != '\n' && c != '\t';) {
      buff[i++] = c;
      if (HTS_feof(fp))
         break;
      c = HTS_fgetc(fp);
      if (c == EOF)
         break;
   }

   buff[i] = '\0';
   return TRUE;
}

/* HTS_get_token_with_separator: get token from file pointer with specified separator */
HTS_Boolean HTS_get_token_from_fp_with_separator(HTS_File * fp, char *buff, char separator)
{
   char c;
   size_t i;

   if (fp == NULL || HTS_feof(fp))
      return FALSE;
   c = HTS_fgetc(fp);
   while (c == separator) {
      if (HTS_feof(fp))
         return FALSE;
      c = HTS_fgetc(fp);
      if (c == EOF)
         return FALSE;
   }

   for (i = 0; c != separator;) {
      buff[i++] = c;
      if (HTS_feof(fp))
         break;
      c = HTS_fgetc(fp);
      if (c == EOF)
         break;
   }

   buff[i] = '\0';
   return TRUE;
}

/* HTS_get_token_from_string: get token from string (separators are space, tab, and line break) */
HTS_Boolean HTS_get_token_from_string(const char *string, size_t * index, char *buff)
{
   char c;
   size_t i;

   c = string[(*index)];
   if (c == '\0')
      return FALSE;
   c = string[(*index)++];
   if (c == '\0')
      return FALSE;
   while (c == ' ' || c == '\n' || c == '\t') {
      if (c == '\0')
         return FALSE;
      c = string[(*index)++];
   }
   for (i = 0; c != ' ' && c != '\n' && c != '\t' && c != '\0'; i++) {
      buff[i] = c;
      c = string[(*index)++];
   }

   buff[i] = '\0';
   return TRUE;
}

/* HTS_get_token_from_string_with_separator: get token from string with specified separator */
HTS_Boolean HTS_get_token_from_string_with_separator(const char *str, size_t * index, char *buff, char separator)
{
   char c;
   size_t len = 0;

   if (str == NULL)
      return FALSE;

   c = str[(*index)];
   if (c == '\0')
      return FALSE;
   while (c == separator) {
      if (c == '\0')
         return FALSE;
      (*index)++;
      c = str[(*index)];
   }
   while (c != separator && c != '\0') {
      buff[len++] = c;
      (*index)++;
      c = str[(*index)];
   }
   if (c != '\0')
      (*index)++;

   buff[len] = '\0';

   if (len > 0)
      return TRUE;
   else
      return FALSE;
}

/* HTS_calloc: wrapper for calloc */
void *HTS_calloc(const size_t num, const size_t size)
{
   size_t n = num * size;
   void *mem;

   if (n == 0)
      return NULL;

#ifdef FESTIVAL
   mem = (void *) safe_wcalloc(n);
#else
   mem = (void *) malloc(n);
#endif                          /* FESTIVAL */

   memset(mem, 0, n);

   if (mem == NULL)
      HTS_error(1, "HTS_calloc: Cannot allocate memory.\n");

   return mem;
}

/* HTS_Free: wrapper for free */
void HTS_free(void *ptr)
{
#ifdef FESTIVAL
   wfree(ptr);
#else
   free(ptr);
#endif                          /* FESTIVAL */
}

/* HTS_strdup: wrapper for strdup */
char *HTS_strdup(const char *string)
{
#ifdef FESTIVAL
   return (wstrdup(string));
#else
   char *buff = (char *) HTS_calloc(strlen(string) + 1, sizeof(char));
   strcpy(buff, string);
   return buff;
#endif                          /* FESTIVAL */
}

/* HTS_alloc_matrix: allocate double matrix */
double **HTS_alloc_matrix(size_t x, size_t y)
{
   size_t i;
   double **p;

   if (x == 0 || y == 0)
      return NULL;

   p = (double **) HTS_calloc(x, sizeof(double *));

   for (i = 0; i < x; i++)
      p[i] = (double *) HTS_calloc(y, sizeof(double));
   return p;
}

/* HTS_free_matrix: free double matrix */
void HTS_free_matrix(double **p, size_t x)
{
   size_t i;

   for (i = 0; i < x; i++)
      HTS_free(p[i]);
   HTS_free(p);
}

/* HTS_error: output error message */
void HTS_error(int error, const char *message, ...)
{
   va_list arg;

   fflush(stdout);
   fflush(stderr);

   if (error > 0)
      fprintf(stderr, "\nError: ");
   else
      fprintf(stderr, "\nWarning: ");

   va_start(arg, message);
   vfprintf(stderr, message, arg);
   va_end(arg);

   fflush(stderr);

   if (error > 0)
      exit(error);
}

HTS_MISC_C_END;

#endif                          /* !HTS_MISC_C */