mootUtils.h
Go to the documentation of this file.
1 /* -*- Mode: C++ -*- */
2 
3 /*
4  libmoot : moocow's part-of-speech tagging library
5  Copyright (C) 2003-2014 by Bryan Jurish <moocow@cpan.org>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 3 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 
22 /*--------------------------------------------------------------------------
23  * File: mootUtils.h
24  * Author: Bryan Jurish <moocow@cpan.org>
25  * Description:
26  * + moocow's PoS tagger : useful utilities
27  *--------------------------------------------------------------------------*/
28 
33 #ifndef _moot_UTILS_H
34 #define _moot_UTILS_H
35 
36 #include <mootCIO.h>
37 
38 #include <list>
39 #include <vector>
40 
41 namespace moot {
42  using namespace std;
43  using namespace mootio;
44 
45  /*----------------------------------------------------------------------*/
49  void moot_setlocale(void);
50 
52  const char *moot_lc_ctype(void);
53 
55  const char *moot_lc_numeric(void);
57 
58 
59  /*----------------------------------------------------------------------*/
62 
67  bool moot_parse_doubles(const char *str, double *dbls, size_t ndbls);
68 
69  /*----------------------------------------------------------------------
70  * normalize_ws_*()
71  */
83  void moot_normalize_ws(const char *buf,
84  size_t len,
85  std::string &out,
86  bool trim_left=true,
87  bool trim_right=true);
88 
99  void moot_normalize_ws(const std::string &in,
100  std::string &out,
101  bool trim_left=true,
102  bool trim_right=true);
103 
113  inline void moot_normalize_ws(const char *s,
114  std::string &out,
115  bool trim_left=true,
116  bool trim_right=true)
117  {
118  moot_normalize_ws(s, strlen(s), out, trim_left, trim_right);
119  };
120 
130  inline std::string moot_normalize_ws(const char *buf,
131  size_t len,
132  bool trim_left=true,
133  bool trim_right=true)
134  {
135  std::string out;
136  out.reserve(len);
137  moot_normalize_ws(buf,len, out, trim_left,trim_right);
138  return out;
139  };
140 
149  inline std::string moot_normalize_ws(const char *s,
150  bool trim_left=true,
151  bool trim_right=true)
152  {
153  return moot_normalize_ws(s,strlen(s), trim_left,trim_right);
154  };
155 
164  inline std::string moot_normalize_ws(const std::string &s,
165  bool trim_left=true,
166  bool trim_right=true)
167  {
168  return moot_normalize_ws(s.data(),s.size(), trim_left,trim_right);
169  };
170 
171  /*----------------------------------------------------------------------
172  * remove_newlines()
173  */
181  void moot_remove_newlines(char *buf, size_t len);
182 
184  inline void moot_remove_newlines(char *s)
185  { moot_remove_newlines(s, strlen(s)); };
186 
188  void moot_remove_newlines(std::string &s);
189 
198  void moot_strtok(const std::string &s, const std::string &delim, std::list<std::string> &out);
199 
205  std::list<std::string> moot_strtok(const std::string &s, const std::string &delim);
206 
214  void moot_strsplit(const std::string &s, const std::string &delim, std::vector<std::string> &out);
215 
221  std::vector<std::string> moot_strsplit(const std::string &s, const std::string &delim);
222 
223 
230  int std_vsprintf(std::string &s, const char *fmt, va_list &ap);
231 
238  int std_sprintf(std::string &s, const char *fmt, ...);
239 
245  std::string std_vssprintf(const char *fmt, va_list &ap);
246 
252  std::string std_ssprintf(const char *fmt, ...);
254 
255  /*----------------------------------------------------------------------*/
258 
260  bool moot_file_exists(const char *filename);
261 
263  inline bool moot_file_exists(const std::string &filename)
264  { return moot_file_exists(filename.c_str()); };
265 
267  std::string moot_unextend(const char *filename);
268 
275  const char *moot_extension(const char *filename, size_t pos);
276 
278  inline const char *moot_extension(const char *filename)
279  {
280  return moot_extension(filename, strlen(filename));
281  };
282 
283 
288  class cmdutil_file_churner {
289  public:
290  // -- command-line data
291  const char *progname;
292  char **inputs;
293  int ninputs;
295  // -- operation flags
296  bool use_list;
297  bool paranoid;
299  // -- file data
301  mifstream list;
303  // -- buffer data
304  std::string line;
306  private:
307  bool is_first_input;
309  public:
311  cmdutil_file_churner(const char *my_progname=NULL,
312  char **my_inputs=NULL,
313  int my_ninputs=0,
314  bool my_use_list=false,
315  bool my_paranoid=true)
316  : progname(my_progname),
317  inputs(my_inputs),
318  ninputs(my_ninputs),
319  use_list(my_use_list),
320  paranoid(my_paranoid),
321  is_first_input(true)
322  {};
323 
325  ~cmdutil_file_churner() {};
326 
330  FILE *first_input_file();
331 
335  std::string &first_input_name();
336 
338  FILE *next_input_file();
339 
341  std::string &next_input_name();
342 
343  private:
345  FILE *next_list_file();
346  };
348 
349  /*----------------------------------------------------------------------*/
352 
354  typedef enum {
355  vlSilent = 0,
356  vlErrors = 1,
358  vlInfo = 3,
360  vlDebug = 5,
361  vlTrace = 6,
366  std::string moot_banner(void);
367 
369  std::string moot_program_banner(const std::string &prog_name,
370  const std::string &prog_version,
371  const std::string &prog_author,
372  bool is_free=true);
373 
375  void moot_vcarp(const char *fmt, va_list &ap);
376 
378  void moot_carp(const char *fmt, ...);
379 
381  void moot_vcroak(const char *fmt, ...);
382 
384  void moot_croak(const char *fmt, ...);
385 
392  void moot_vmsg(int curLevel, int minLevel, const char *fmt, va_list &ap);
393 
400  void moot_msg(int curLevel, int minLevel, const char *fmt, ...);
402 
403 }; /* namespace moot */
404 
405 #endif /* _moot_UTILS_H */
Definition: mootAssocVector.h:39
std::string moot_unextend(const char *filename)
void moot_msg(int curLevel, int minLevel, const char *fmt,...)
Definition: mootUtils.h:365
mootio abstraction layer for C FILE*s
Definition: mootUtils.h:362
void moot_strtok(const std::string &s, const std::string &delim, std::list< std::string > &out)
std::string moot_banner(void)
Class for churning through many input files, given either directly or as a list-file.
Definition: mootUtils.h:287
Definition: mootUtils.h:364
Wrapper class for named file input using C FILE*s.
Definition: mootCIO.h:286
int std_sprintf(std::string &s, const char *fmt,...)
void moot_remove_newlines(char *buf, size_t len)
bool moot_parse_doubles(const char *str, double *dbls, size_t ndbls)
void moot_carp(const char *fmt,...)
int std_vsprintf(std::string &s, const char *fmt, va_list &ap)
Definition: mootUtils.h:360
VerbosityLevel
Definition: mootUtils.h:357
Definition: mootUtils.h:361
void moot_vmsg(int curLevel, int minLevel, const char *fmt, va_list &ap)
Definition: mootUtils.h:358
void moot_setlocale(void)
void moot_strsplit(const std::string &s, const std::string &delim, std::vector< std::string > &out)
Namespace for I/O stream wrappers.
Definition: mootBufferIO.h:45
void moot_vcarp(const char *fmt, va_list &ap)
bool moot_file_exists(const char *filename)
const char * moot_lc_numeric(void)
void moot_vcroak(const char *fmt,...)
void moot_croak(const char *fmt,...)
std::string moot_program_banner(const std::string &prog_name, const std::string &prog_version, const std::string &prog_author, bool is_free=true)
Definition: mootUtils.h:359
Definition: mootUtils.h:363
void moot_normalize_ws(const char *buf, size_t len, std::string &out, bool trim_left=true, bool trim_right=true)
std::string std_vssprintf(const char *fmt, va_list &ap)
const char * moot_extension(const char *filename, size_t pos)
const char * moot_lc_ctype(void)
std::string std_ssprintf(const char *fmt,...)