lines.h
説明を見る。
1 //
2 // Copyright (c) 2003-2011, MIST Project, Nagoya University
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the name of the Nagoya University nor the names of its contributors
16 // may be used to endorse or promote products derived from this software
17 // without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
20 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28 
33 #ifndef __INCLUDE_MIST_LINES__
34 #define __INCLUDE_MIST_LINES__
35 
36 
37 #ifndef __INCLUDE_MIST_CONF_H__
38 #include "../config/mist_conf.h"
39 #endif
40 
41 #include <iostream>
42 #include <string>
43 #include <vector>
44 
45 #include <zlib.h>
46 
47 
48 // mist名前空間の始まり
50 
51 
52 namespace __lines_controller__
53 {
54  struct lines_controller
55  {
56  static const unsigned char *get_line( const unsigned char *s, const unsigned char *e, std::string &line, bool &__has_eol__ )
57  {
58  __has_eol__ = false;
59  while( s < e )
60  {
61  if( s[ 0 ] == '\r' )
62  {
63  if( s + 1 != e && s[ 1 ] == '\n' )
64  {
65  __has_eol__ = true;
66  s = s + 2;
67  }
68  else
69  {
70  s++;
71  }
72  break;
73  }
74  else if( s[ 0 ] == '\n' )
75  {
76  __has_eol__ = true;
77  s = s + 1;
78  break;
79  }
80 
81  line += *s;
82  s++;
83  }
84 
85  return( s > e ? e : s );
86  }
87 
88  template < template < typename T, typename A > class Array, class Allocator >
89  static bool read( Array< std::string, Allocator > &lines, const std::string &filename )
90  {
91  gzFile fp;
92  if( ( fp = gzopen( filename.c_str( ), "rb" ) ) == NULL )
93  {
94  return( false );
95  }
96 
97  std::string line = "";
98  size_t numBytes = 4096;
99  unsigned char *buff = new unsigned char[ numBytes ];
100 
101  while( gzeof( fp ) == 0 )
102  {
103  ptrdiff_t read_size = gzread( fp, ( void * )buff, static_cast< unsigned int >( sizeof( unsigned char ) * numBytes ) );
104 
105  const unsigned char *sp = buff;
106  const unsigned char *ep = sp + read_size;
107 
108  while( sp < ep )
109  {
110  bool __has_eol__ = false;
111  sp = get_line( sp, ep, line, __has_eol__ );
112 
113  if( !line.empty( ) && __has_eol__ )
114  {
115  lines.push_back( line );
116  line.resize( 0 );
117  }
118  }
119  }
120 
121  if( !line.empty( ) )
122  {
123  lines.push_back( line );
124  }
125 
126  gzclose( fp );
127 
128  delete [] buff;
129 
130  return( true );
131  }
132  };
133 }
134 
135 
146 
147 
159 template < template < typename T, typename A > class Array, class Allocator >
160 bool read_lines( Array< std::string, Allocator > &lines, const std::string &filename )
161 {
162  // データをクリアする
163  lines.clear( );
164  return( __lines_controller__::lines_controller::read( lines, filename ) );
165 }
166 
167 
179 template < template < typename T, typename A > class Array, class Allocator >
180 bool read_lines( Array< std::string, Allocator > &lines, const std::wstring &filename )
181 {
182  return( read_lines( lines, wstr2str( filename ) ) );
183 }
184 
186 // 改行区切りのデータの入出力グループの終わり
187 
188 
189 // mist名前空間の終わり
190 _MIST_END
191 
192 
193 #endif // __INCLUDE_MIST_LINES__
194 

Generated on Wed Nov 12 2014 19:44:17 for MIST by doxygen 1.8.1.2