Stride Reference Manual  - generated for commit 9643b11
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ReaderFactory.cpp
Go to the documentation of this file.
1 /*
2  * This is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * any later version.
6  * The software is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with the software. If not, see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2018, Jan Broeckhove and Bistromatics group.
14  */
15 
16 #include "ReaderFactory.h"
17 
18 #include "CommutesCSVReader.h"
19 #include "HouseholdCSVReader.h"
20 #include "LocationsCSVReader.h"
21 #include "util/FileSys.h"
22 
23 #include <fstream>
24 #include <iostream>
25 #include <stdexcept>
26 
27 namespace geopop {
28 
29 using namespace std;
30 using namespace stride::util;
31 
32 shared_ptr<LocationsReader> ReaderFactory::CreateLocationsReader(const string &filename)
33 {
34  return CreateLocationsReader(FileSys::GetDataDir() / filesys::path(filename));
35 }
36 
37 shared_ptr<LocationsReader> ReaderFactory::CreateLocationsReader(const filesys::path &path)
38 {
39  return make_shared<LocationsCSVReader>(OpenFile(path));
40 }
41 
42 std::shared_ptr<CommutesReader> ReaderFactory::CreateCommutesReader(const std::string& filename)
43 {
44  return CreateCommutesReader(FileSys::GetDataDir() / filesys::path(filename));
45 }
46 
47 shared_ptr<CommutesReader> ReaderFactory::CreateCommutesReader(const filesys::path& path)
48 {
49  return make_shared<CommutesCSVReader>(OpenFile(path));
50 }
51 
52 std::shared_ptr<HouseholdReader> ReaderFactory::CreateHouseholdReader(const std::string& filename)
53 {
54  return CreateHouseholdReader(FileSys::GetDataDir() / filesys::path(filename));
55 }
56 
57 shared_ptr<HouseholdReader> ReaderFactory::CreateHouseholdReader(const filesys::path& path)
58 {
59  return make_shared<HouseholdCSVReader>(OpenFile(path));
60 }
61 
62 std::unique_ptr<std::istream> ReaderFactory::OpenFile(const filesys::path& path)
63 {
64  if (!filesys::exists(path)) {
65  throw runtime_error("File not found: " + path.string());
66  }
67 
68  if (path.extension().string() == ".csv") {
69  return make_unique<ifstream>(path.string());
70  } else {
71  throw runtime_error("Unsupported file extension: " + path.extension().string());
72  }
73 }
74 
75 } // namespace geopop
static filesys::path GetDataDir()
/// Return data dir (only relevant when use_install_dirs mode is active)
Definition: FileSys.h:88
Utilities for the project.
static std::shared_ptr< CommutesReader > CreateCommutesReader(const std::string &filename)
Create a CommutesReader based on the filename relative to the data directory.
STL namespace.
static std::unique_ptr< std::istream > OpenFile(const filesys::path &path)
Create an istream based on the filesystem path.
static std::shared_ptr< LocationsReader > CreateLocationsReader(const std::string &filename)
Create a Reader based on the filename relative to the data directory.
Namespace for the geographic and demograhic classes.
Definition: Coordinate.h:21
static std::shared_ptr< HouseholdReader > CreateHouseholdReader(const std::string &filename)
Create a HouseholdReader based on the filename relative to the data directory.
Interface for install directory queries.