bpp-seq  2.2.0
NexusTools.cpp
Go to the documentation of this file.
1 //
2 // File: NexusTools.cpp
3 // Created by: Julien Dutheil
4 // Created on: Wed May 27 19:30 2009
5 //
6 
7 /*
8 Copyright or © or Copr. CNRS, (November 17, 2004)
9 
10 This software is a computer program whose purpose is to provide classes
11 for sequences analysis.
12 
13 This software is governed by the CeCILL license under French law and
14 abiding by the rules of distribution of free software. You can use,
15 modify and/ or redistribute the software under the terms of the CeCILL
16 license as circulated by CEA, CNRS and INRIA at the following URL
17 "http://www.cecill.info".
18 
19 As a counterpart to the access to the source code and rights to copy,
20 modify and redistribute granted by the license, users are provided only
21 with a limited warranty and the software's author, the holder of the
22 economic rights, and the successive licensors have only limited
23 liability.
24 
25 In this respect, the user's attention is drawn to the risks associated
26 with loading, using, modifying and/or developing or reproducing the
27 software by the user in light of its specific status of free software,
28 that may mean that it is complicated to manipulate, and that also
29 therefore means that it is reserved for developers and experienced
30 professionals having in-depth computer knowledge. Users are therefore
31 encouraged to load and test the software's suitability as regards their
32 requirements in conditions enabling the security of their systems and/or
33 data to be ensured and, more generally, to use and operate it in the
34 same conditions as regards security.
35 
36 The fact that you are presently reading this means that you have had
37 knowledge of the CeCILL license and that you accept its terms.
38 */
39 
40 #include "NexusTools.h"
41 #include <Bpp/Text/TextTools.h>
42 #include <Bpp/Io/FileTools.h>
43 
44 using namespace bpp;
45 using namespace std;
46 
47 std::string NexusTools::getNextNonCommentLine(std::istream& input)
48 {
49  string line = TextTools::removeSurroundingWhiteSpaces(FileTools::getNextLine(input));
50  bool test = true;
51  unsigned int countOpen = 0;
52  unsigned int countClosed = 0;
53  while(test)
54  {
55  if (line[0] == '[')
56  {
57  countOpen++;
58  }
59  if (line[line.size() - 1] == ']')
60  {
61  countClosed++;
62  }
63  if(countOpen > 0)
64  line = TextTools::removeSurroundingWhiteSpaces(FileTools::getNextLine(input));
65  if(countOpen == countClosed)
66  test = false;
67  }
68  return line;
69 }
70 
71 
72 bool NexusTools::getNextCommand(std::istream& input, std::string& name, std::string& arguments, bool lineBrk)
73  throw (IOException)
74 {
75  // Checking if the stream is readable
76  if (! input) { throw IOException ("NexusTools::getNextCommand(). Failed to read from stream"); }
77 
78  string line = TextTools::removeSurroundingWhiteSpaces(getNextNonCommentLine(input));
79  if (TextTools::startsWith(line, "BEGIN"))
80  {
81  return false;
82  }
83 
84  // Check if the command stands on one line:
85  bool commandComplete = TextTools::endsWith(line, ";");
86  if (commandComplete)
87  line = line.substr(0, line.size() - 1);
88  // Get the command name, as the first block:
89  string::size_type limit = line.find(" ");
90  if (limit == string::npos)
91  {
92  name = line;
93  arguments = "";
94  if (commandComplete)
95  {
96  //Command with no argument:
97  return true;
98  }
99  }
100  else
101  {
102  name = line.substr(0, limit);
103  arguments = line.substr(limit + 1);
104  }
105  //Then parse the next lines:
106  while(!commandComplete)
107  {
108  if (input.eof()) { throw IOException ("NexusTools::getNextCommand(). Reached end of file before the end of the command could be found"); }
109  line = TextTools::removeSurroundingWhiteSpaces(getNextNonCommentLine(input));
110  commandComplete = TextTools::endsWith(line, ";");
111  if (commandComplete)
112  line = line.substr(0, line.size() - 1);
113  if(lineBrk)
114  arguments += "\n";
115  arguments += line;
116  }
117  return true;
118 }
119 
This alphabet is used to deal NumericAlphabet.
STL namespace.
static bool getNextCommand(std::istream &input, std::string &name, std::string &arguments, bool lineBrk=true)
parse the next command name within a block.
Definition: NexusTools.cpp:72
static std::string getNextNonCommentLine(std::istream &input)
Definition: NexusTools.cpp:47