Merged revisions 195034,195219,195245,195357,195374,195428,195599,195673,195809 via...
[official-gcc.git] / main / libstdc++-v3 / testsuite / util / io / text_populate.hpp
blob9c466b1fcec45e098dbd9e7e8ba9f3d9ac06677f
1 // -*- C++ -*-
3 // Copyright (C) 2005-2013 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 3, or (at your option) any later
9 // version.
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
21 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
23 // Permission to use, copy, modify, sell, and distribute this software
24 // is hereby granted without fee, provided that the above copyright
25 // notice appears in all copies, and that both that copyright notice
26 // and this permission notice appear in supporting documentation. None
27 // of the above authors, nor IBM Haifa Research Laboratories, make any
28 // representation about the suitability of this software for any
29 // purpose. It is provided "as is" without express or implied
30 // warranty.
32 /**
33 * @file text_populate.hpp
34 * Contains a function for populating a vector with text words from
35 * a file.
38 #ifndef PB_DS_TEXT_POPULATE_HPP
39 #define PB_DS_TEXT_POPULATE_HPP
41 #include <io/illegal_input_error.hpp>
42 #include <set>
43 #include <fstream>
44 #include <string>
45 #include <iostream>
47 namespace __gnu_pbds
50 namespace test
53 template<typename Vec>
54 void
55 text_populate(const std::string& r_f_name, Vec& r_a_txt)
57 const size_t size = r_a_txt.size();
59 try
61 std::ifstream f(r_f_name.c_str());
63 if (!f.good())
65 std::cerr << "Cannot open file " << r_f_name.c_str() <<
66 std::endl;
68 throw __gnu_pbds::test::illegal_input_error();
71 size_t i = 0;
73 while (f.good()&& i < size)
75 f >> r_a_txt[i].first;
76 r_a_txt[i].second = 0;
78 ++i;
81 if (i < size)
83 std::cerr << "Read only " << static_cast<unsigned long>(i) <<
84 " words" << std::endl;
86 throw __gnu_pbds::test::illegal_input_error();
89 catch(...)
91 std::cerr << "Error reading from file" << std::endl;
93 throw;
97 template<typename Vec>
98 void
99 distinct_text_populate(const std::string& r_f_name, Vec& r_a_txt)
101 const size_t size = r_a_txt.size();
105 std::ifstream f(r_f_name.c_str());
107 if (!f.good())
109 std::cerr << "Cannot open file " << r_f_name.c_str() <<
110 std::endl;
112 throw __gnu_pbds::test::illegal_input_error();
115 typedef std::set< typename Vec::value_type::first_type> set_t;
117 set_t s;
119 while (f.good()&& s.size() < size)
121 typename Vec::value_type::first_type v;
123 f >> v;
125 if (s.find(v) == s.end())
127 r_a_txt[s.size()] = std::make_pair(v, 0);
129 s.insert(v);
133 if (s.size() < size)
135 std::cerr << "Read only " << static_cast<unsigned long>(s.size()) <<
136 " words" << std::endl;
138 throw __gnu_pbds::test::illegal_input_error();
141 catch(...)
143 std::cerr << "Error reading from file" << std::endl;
145 throw;
149 } // namespace test
151 } // namespace __gnu_pbds
153 #endif // #ifndef PB_DS_TEXT_POPULATE_HPP