summaryrefslogtreecommitdiff
path: root/htk_io/src/KaldiLib/Common.cc
blob: 40909ee1f8e5778fe73bfd9b2f2eabd4969ac457 (plain) (blame)
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
#include <string>
#include <stdexcept>
#include <cmath>
#include <cfloat>
#include <cstdio>

#include "Common.h"
#include "MathAux.h"


/// Defines the white chars for string trimming
#if !defined(WHITE_CHARS)
#  define WHITE_CHARS " \t"
#endif

namespace TNet {

#include <ios>
  
  // Allocating stream variable used by stream modifier MatrixVectorIostreamControl
  const int MATRIX_IOS_FORMAT_IWORD = std::ios_base::xalloc();

  //***************************************************************************
  //***************************************************************************
  int getHTKstr(char *str)
  {
    char termChar = '\0';
    char *chrptr = str;
  
    while (std::isspace(*chrptr)) ++chrptr;
  
    if (*chrptr == '\'' || *chrptr == '"') {
      termChar = *chrptr;
      chrptr++;
    }
  
    for (; *chrptr; chrptr++) {
      if (*chrptr == '\'' || *chrptr == '"') {
        if (termChar == *chrptr) {
          termChar = '\0';
          chrptr++;
          break;
        }
      }
  
      if (std::isspace(*chrptr) && !termChar) {
        break;
      }
  
      if (*chrptr == '\\') {
        ++chrptr;
        if (*chrptr == '\0' || (*chrptr    >= '0' && *chrptr <= '7' &&
                              (*++chrptr  <  '0' || *chrptr >  '7' ||
                              *++chrptr  <  '0' || *chrptr >  '7'))) {
          return -1;
        }
  
        if (*chrptr  >= '0' && *chrptr  <= '7') {
          *chrptr = (char)((*chrptr - '0') + (chrptr[-1] - '0') * 8 + (chrptr[-2] - '0') * 64);
        }
      }
      *str++ = *chrptr;
    }
  
    if (termChar) {
      return -2;
    }
  
    *str = '\0';
  
    return 0;
  }
  

  //*****************************************************************************
  //*****************************************************************************
  void
  ParseHTKString(const std::string & rIn, std::string & rOut)
  {
    int ret_val;

    // the new string will be at most as long as the original, so we allocate
    // space
    char* new_str = new char[rIn.size() + 1];

    char* p_htk_str = new_str;

    strcpy(p_htk_str, rIn.c_str());
    ret_val = getHTKstr(p_htk_str);

    // call the function
    if (!ret_val) {
      rOut = p_htk_str;
    }

    delete [] new_str;

    if (ret_val) {
      throw std::runtime_error("Error parsing HTK string");
    }
  }

  

  //***************************************************************************
  //***************************************************************************
  bool 
  IsBigEndian()
  {
    int a = 1;
    return (bool) ((char *) &a)[0] != 1;
  }
  

  //***************************************************************************
  //***************************************************************************
  void 
  MakeHtkFileName(char* pOutFileName,  const char* inFileName,
               const char* out_dir, const char* out_ext)
  {
    const char* base_name;
    const char* bname_end = NULL;
    const char* chrptr;
  
    //  if (*inFileName == '*' && *++inFileName == '/') ++inFileName;
    
    // we don't do anything if file is stdin/out
    if (!strcmp(inFileName, "-"))
    {
      pOutFileName[0] = '-';
      pOutFileName[1] = '\0';
      return;
    }    
    
    base_name = strrchr(inFileName, '/');
    base_name = base_name != NULL ? base_name + 1 : inFileName;
    
    if (out_ext) bname_end = strrchr(base_name, '.');
    if (!bname_end) bname_end = base_name + strlen(base_name);
  
  
    if ((chrptr = strstr(inFileName, "/./")) != NULL) 
    {
      // what is in path after /./ serve as base name
      base_name = chrptr + 3;
    }
    /* else if (*inFileName != '/') 
    {
      // if inFileName isn't absolut path, don't forget directory structure
      base_name = inFileName;
    }*/
  
    *pOutFileName = '\0';
    if (out_dir) 
    {
      if (*out_dir) 
      {
        strcat(pOutFileName, out_dir);
        strcat(pOutFileName, "/");
      }
      strncat(pOutFileName, base_name, bname_end-base_name);
    } 
    else 
    {
      strncat(pOutFileName, inFileName, bname_end-inFileName);
    }
  
    if (out_ext && *out_ext) 
    {
      strcat(pOutFileName, ".");
      strcat(pOutFileName, out_ext);
    }
  }

  
  //****************************************************************************
  //****************************************************************************
  bool 
  CloseEnough(const float f1, const float f2, const float nRounds)
  {
    bool ret_val = (_ABS((f1 - f2) / (f2 == 0.0f ? 1.0f : f2))
        < (nRounds * FLT_EPSILON));

    return ret_val;
  } 

  
  //****************************************************************************
  //****************************************************************************
  bool 
  CloseEnough(const double f1, const double f2, const double nRounds)
  {
    bool ret_val = (_ABS((f1 - f2) / (f2 == 0.0 ? 1.0 : f2))
        < (nRounds * DBL_EPSILON));

    return ret_val;
  } 


  //****************************************************************************
  //****************************************************************************
  char* 
  ExpandHtkFilterCmd(const char *command, const char *filename, const char* pFilter)
  {

    char *out, *outend;
    const char *chrptr = command;
    int ndollars = 0;
    int fnlen = strlen(filename);

    while (*chrptr++) ndollars += (*chrptr ==  *pFilter);

    out = (char*) malloc(strlen(command) - ndollars + ndollars * fnlen + 1);

    outend = out;

    for (chrptr = command; *chrptr; chrptr++) {
      if (*chrptr ==  *pFilter) {
        strcpy(outend, filename);
        outend += fnlen;
      } else {
        *outend++ = *chrptr;
      }
    }
    *outend = '\0';
    return out;
  }

  //***************************************************************************
  //***************************************************************************
  char *
  StrToUpper(char *str)
  {
    char *chptr;
    for (chptr = str; *chptr; chptr++) {
      *chptr = (char)toupper(*chptr);
    }
    return str;
  }
  

  //**************************************************************************** 
  //**************************************************************************** 
  std::string&
  Trim(std::string& rStr)
  {
    // WHITE_CHARS is defined in common.h
    std::string::size_type pos = rStr.find_last_not_of(WHITE_CHARS);
    if(pos != std::string::npos) 
    {
      rStr.erase(pos + 1);
      pos = rStr.find_first_not_of(WHITE_CHARS);
      if(pos != std::string::npos) rStr.erase(0, pos);
    }
    else 
      rStr.erase(rStr.begin(), rStr.end());

    return rStr;
  }


} // namespace TNet

//#ifdef CYGWIN

void assertf(const char *c, int i, const char *msg){
  printf("Assertion \"%s\" failed: file \"%s\", line %d\n", msg?msg:"(null)", c?c:"(null)", i);
  abort();
}


void assertf_throw(const char *c, int i, const char *msg){
  char buf[2000];
  snprintf(buf, 1999, "Assertion \"%s\" failed, throwing exception: file \"%s\", line %d\n", msg?msg:"(null)", c?c:"(null)", i);
  throw std::runtime_error((std::string)buf);
}
//#endif