Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
StringDataFactory.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include <ext/fstream>
9#include <ext/sstream>
10
11#include <global/GlobalData.h>
13
14#include <core/stringApi.hpp>
15
16namespace factory {
17
19public:
20 class fromFile {
21 const std::string & filename;
22
23 public:
24 fromFile ( const std::string & file ) : filename ( file ) {
25 }
26
27 template < class T >
28 operator T ( ) {
29 ext::ifstream fileStream ( filename );
30
31 return fromStream ( fileStream );
32 }
33 };
34
35 class fromString {
36 const std::string & string;
37
38 public:
39 fromString ( const std::string & str ) : string ( str ) {
40 }
41
42 template < class T >
43 operator T ( ) {
44 ext::istringstream stringStream ( string );
45
46 return fromStream ( stringStream );
47 }
48 };
49
50 class fromStdin {
51 public:
52 template < class T >
53 operator T ( ) {
55 }
56 };
57
58 class fromStream {
59 ext::istream & in;
60
61 public:
62 fromStream ( ext::istream & i ) : in ( i ) {
63 }
64
65 template < class T >
66 operator T ( ) {
67 if ( in.peek ( ) == EOF )
68 throw exception::CommonException ( "Empty stream" );
69
71
72 while ( ext::isspace ( in.peek ( ) ) )
73 in.get ( );
74
75 if ( in.peek ( ) != EOF )
76 throw exception::CommonException ( std::string ( "Unexpected characters at the end of the stream (" ) + static_cast < char > ( in.peek ( ) ) + ", code: " + ext::to_string ( in.peek ( ) ) + ")" );
77
78 return res;
79 }
80 };
81
82 template < class T >
83 static void toFile ( const T & data, const std::string & filename) {
84 ext::ofstream fileStream ( filename );
85 toStream < T > ( data, fileStream );
86 }
87
88 template < class T >
89 static std::string toString ( const T & data ) {
90 ext::ostringstream stringStream;
91 toStream < T > ( data, stringStream );
92 return stringStream.str ( );
93 }
94
95 template < class T >
96 static void toStdout ( const T & data ) {
97 toStream < T > ( data, common::Streams::out );
98 }
99
100 template < class T >
101 static void toStream ( const T & data, ext::ostream & out ) {
102 core::stringApi < T >::compose ( out, data );
103 }
104};
105
106} /* namespace factory */
107
static ext::reference_wrapper< ext::ostream > out
Standard output stream. Mapped to descriptor 1.
Definition: GlobalData.h:66
static ext::reference_wrapper< ext::istream > in
Standard input stream. Mapped to descriptor 0.
Definition: GlobalData.h:60
Basic exception from which all other exceptions are derived.
Definition: CommonException.h:21
Definition: fstream.h:68
Definition: istream.h:32
int peek()
Definition: istream.cpp:41
int get()
Definition: istream.cpp:45
Definition: sstream.h:36
Definition: fstream.h:15
Definition: ostream.h:14
Definition: sstream.h:15
std::string str() const &
Definition: sstream.cpp:29
Definition: StringDataFactory.hpp:20
fromFile(const std::string &file)
Definition: StringDataFactory.hpp:24
Definition: StringDataFactory.hpp:50
Definition: StringDataFactory.hpp:58
fromStream(ext::istream &i)
Definition: StringDataFactory.hpp:62
Definition: StringDataFactory.hpp:35
fromString(const std::string &str)
Definition: StringDataFactory.hpp:39
Definition: StringDataFactory.hpp:18
static void toStdout(const T &data)
Definition: StringDataFactory.hpp:96
static std::string toString(const T &data)
Definition: StringDataFactory.hpp:89
static void toFile(const T &data, const std::string &filename)
Definition: StringDataFactory.hpp:83
static void toStream(const T &data, ext::ostream &out)
Definition: StringDataFactory.hpp:101
int i
Definition: AllEpsilonClosure.h:118
return res
Definition: MinimizeByPartitioning.h:145
std::string to_string(const T &value)
To string method designated for objects that can be casted to string.
Definition: string.hpp:131
bool isspace(int ch)
isspace method.
Definition: string.hpp:279
Definition: NormalizeFactory.hpp:13
Definition: RandomStringFactory.cpp:12
Definition: stringApi.hpp:26