IMP logo
IMP Reference Guide  2.10.1
The Integrative Modeling Platform
filenames_manipulation.h
Go to the documentation of this file.
1 /**
2  * \file IMP/em2d/filenames_manipulation.h
3  * \brief Generation of projections using the central section theorem
4  * Copyright 2007-2018 IMP Inventors. All rights reserved.
5  */
6 
7 #ifndef IMPEM2D_FILENAMES_MANIPULATION_H
8 #define IMPEM2D_FILENAMES_MANIPULATION_H
9 
10 #include "IMP/exception.h"
11 #include "IMP/base_types.h"
12 #include <iostream>
13 #include <fstream>
14 
15 #include <boost/filesystem/path.hpp>
16 #include <boost/filesystem/exception.hpp>
17 #include <boost/version.hpp>
18 
19 IMPEM2D_BEGIN_NAMESPACE
20 
21 //! Reads a selection file
22 /*!
23  \param[in] fn Name of the selection file. First column is are file names
24  and second are 1 (file is selected) or 0 (file ignored)
25  \return List of the selected names.
26 */
28  String name;
29  Strings names;
30  std::ifstream in;
31  int not_ignored;
32  in.open(fn.c_str(), std::ios::in);
33  if (!in) {
34  IMP_THROW("Unable to read file " << fn, IOException);
35  }
36 
37  while (in >> name >> not_ignored) {
38  if (not_ignored) {
39  names.push_back(name);
40  }
41  }
42  in.close();
43  return names;
44 }
45 
46 //! generates consecutive filenames: basic_name-i.extension
47 //! Adds zeros at the front of the number when necessary
48 inline Strings create_filenames(unsigned long number, String basic_name,
49  String extension) {
50  // Number of digits to use
51  int digits = 0;
52  unsigned long count = 1;
53  while (count <= number) {
54  digits += 1;
55  count *= 10;
56  }
57  Strings proj_names(number);
58 
59  for (unsigned int i = 0; i < number; ++i) {
60  std::ostringstream strm;
61  strm << basic_name << "-";
62  strm.fill('0');
63  strm.width(digits);
64  strm << i;
65  strm << "." << extension;
66  proj_names[i] = strm.str();
67  }
68  return proj_names;
69 }
70 
71 IMPEM2D_END_NAMESPACE
72 
73 #endif /* IMPEM2D_FILENAMES_MANIPULATION_H */
Basic types used by IMP.
IMPEM2D_BEGIN_NAMESPACE Strings read_selection_file(String fn)
Reads a selection file.
Strings create_filenames(unsigned long number, String basic_name, String extension)
IMP::Vector< String > Strings
Standard way to pass a bunch of String values.
Definition: types.h:51
Exception definitions and assertions.
#define IMP_THROW(message, exception_name)
Throw an exception with a message.
Definition: check_macros.h:50
std::string String
Basic string value.
Definition: types.h:44