bpp-seq3  3.0.0
NexusTools.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include <Bpp/Io/FileTools.h>
6 #include <Bpp/Text/TextTools.h>
7 
8 #include "NexusTools.h"
9 
10 using namespace bpp;
11 using namespace std;
12 
13 std::string NexusTools::getNextNonCommentLine(std::istream& input)
14 {
16  bool test = true;
17  unsigned int countOpen = 0;
18  unsigned int countClosed = 0;
19  while (test)
20  {
21  if (line[0] == '[')
22  {
23  countOpen++;
24  }
25  if (line[line.size() - 1] == ']')
26  {
27  countClosed++;
28  }
29  if (countOpen > 0)
31  if (countOpen == countClosed)
32  test = false;
33  }
34  return line;
35 }
36 
37 
38 bool NexusTools::getNextCommand(std::istream& input, std::string& name, std::string& arguments, bool lineBrk)
39 {
40  // Checking if the stream is readable
41  if (!input)
42  {
43  throw IOException ("NexusTools::getNextCommand(). Failed to read from stream");
44  }
45 
46  string line = TextTools::removeSurroundingWhiteSpaces(getNextNonCommentLine(input));
47  if (TextTools::startsWith(line, "BEGIN"))
48  {
49  return false;
50  }
51 
52  // Check if the command stands on one line:
53  bool commandComplete = TextTools::endsWith(line, ";");
54  if (commandComplete)
55  line = line.substr(0, line.size() - 1);
56  // Get the command name, as the first block:
57  string::size_type limit = line.find(" ");
58  if (limit == string::npos)
59  {
60  name = line;
61  arguments = "";
62  if (commandComplete)
63  {
64  // Command with no argument:
65  return true;
66  }
67  }
68  else
69  {
70  name = line.substr(0, limit);
71  arguments = line.substr(limit + 1);
72  }
73  // Then parse the next lines:
74  while (!commandComplete)
75  {
76  if (input.eof())
77  {
78  throw IOException ("NexusTools::getNextCommand(). Reached end of file before the end of the command could be found");
79  }
80  line = TextTools::removeSurroundingWhiteSpaces(getNextNonCommentLine(input));
81  commandComplete = TextTools::endsWith(line, ";");
82  if (commandComplete)
83  line = line.substr(0, line.size() - 1);
84  if (lineBrk)
85  arguments += "\n";
86  arguments += line;
87  }
88  return true;
89 }
static std::string getNextLine(std::istream &in)
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:38
static std::string getNextNonCommentLine(std::istream &input)
Definition: NexusTools.cpp:13
std::string removeSurroundingWhiteSpaces(const std::string &s)
bool startsWith(const std::string &s, const std::string &pattern)
bool endsWith(const std::string &s, const std::string &pattern)
This alphabet is used to deal NumericAlphabet.