/usr/include/ncl/nxstoken.h is in libncl-dev 2.1.18+dfsg-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 | // Copyright (C) 1999-2003 Paul O. Lewis
//
// This file is part of NCL (Nexus Class Library) version 2.0.
//
// NCL is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// NCL is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with NCL; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
#ifndef NCL_NXSTOKEN_H
#define NCL_NXSTOKEN_H
#include "ncl/nxsexception.h"
class NxsToken;
class NxsX_UnexpectedEOF: public NxsException
{
public:
NxsX_UnexpectedEOF(NxsToken &);
};
/*!
General notes on NexusTokenizing
File position information (pos, line and column) refer to the end of the token.
Note 1: the GetEmbeddedComments methods of ProcessedNxsToken and NxsToken can be tricky to use if detailed
position location of the comment is required. A vector of "embedded comments" in the NCL context is a collection of
all comments that were encountered during a GetNextToken operation. The behavior depends on whether the tokenizer
can tell if a section of text is has the potential to span comment. Potentially comment-spanning tokens have to be
read until a token-breaker is found. Thus they include trailing comments. Thus it is not always easy (or possible)
for client code to determine whether a specifie comment belongs "with" a particular NEXUS token rather than the
previous or next token.
For example:
Text Result as (token, {"embedded comment"}) pairs Explanation
============================ ============================================= =====================================
;a[1]b; (;, {}), (ab, {1}), (;, {}) ab is a comment-spanning token
;[1]a[2]b; (;, {}), (ab, {1, 2}), (;, {}) tokenizer realizes that ; is always a single token
so [1] is not encountered until the second GetNextToken() call.
a[1];[2]b; (a, {1}), (;, {}) (b, {2}), (;, {}) First GetNextToken() call reads "a" token until ; (thus reading "[1]")
; is a single character token, so no comments are read, thus making
[2] part of the third GetNextToken call().
In some cases the comment position information and token position information may reveal the exact location of the
comments. Fortunately the relative order of comments is retained and the exact position is rarely needed.\
Note 2: Using the NxsToken class with the saveCommandComments LabileFlag causes [&comment text here] comments to be
returned as tokens ONLY if they are not preceded by potentially comment-spanning tokens. This "feature" is new to
NCL v2.1 and is the result of a bug-fix (previous versions of NCL incorrectly broke tokens at the start of any comment).
Text Result as in saveCommandComments mode Explanation
========================= ============================================= =====================================
=[&R](1, ("=",{}) ("&R", {}), ("(",{}), ("1",{}), (",",{}) [&R] is not in the middle of potentially-comment-spanning token.
a[&R]b, ("ab",{"&R"}), (",",{}) [&R] is in the middle of comment-spanning token "ab"
a[&R], ("a",{"&R"}), (",",{}) [&R] is in on the trailing end of a potentially-comment-spanning token "a"
the tokenizer
This wart makes it more tedious to deal with command comments. However it is tolerable becuase the only supported use of command
comments in NCL is after single-character tokens (for example after = in a tree descpription).
The NHX command comments are not processed by NCL, but they occur in contexts in which it will be possible to determine
the correct location of the comment (though it is necessary to check the embedded comments when processing NHX trees):
Text Result as NOT IN saveCommandComments mode Explanation
========================= =================================================== =====================================
):3.5[&&NHXtext], (")",{}) (":", {}), ("3.5",{"&&NHXtext"}), (",",{}) "3.5" is potentially-comment-spanning, but the comment still
is stored with other metadata for the same edge.
)[&&NHXtext], (")",{}) (",", {"&&NHXtext"}) NHX comment is parsed with the second token, but
because , is NOT potentially-comment-spanning
know that [&&NHXtext] must have preceded the comma (the
token and comment column numbers would also make this clear.
*/
/*!
New in 2.1
- See Note 2 above (bug-fix, but wart introduced). This could lead to loss of backward compatibility if client
code relies of saveCommandComments in contexts in which command comments occur within potentially comment-spanning
tokens.
- NxsComment class.
- Comments are stored in tokenization (the GetNextToken() call will trash the previous comments, so client code
must store comments if they are needed permanently).
- NxsToken::SetEOFAllowed method added and SetEOFAllowed(false) is called when entering a block. This means that
when parsing block contents the NxsToken tokenizer will raise an NxsException if it runs out of file (thus
Block reader code no longer needs to check for atEOF() constantly to issue an appropriate error).
- ProcessedNxsToken class, NxsToken:ProcessAsSimpleKeyValuePairs, and NxsToken:ProcessAsCommand methods. This makes
it easier to parse commands (by allowing random access to the tokens in a command). These methods are not appropriate
for very long commands (such as MATRIX) or commands that require fiddling with the tokenizing rules (such as disabling
the hyphen as a token breaker)
- lazy NxsToken::GetFilePosition() and low level io operations dramatically speed up tokenization (~10-20 times faster).
- some other utility functions were added, and some refactoring (delegation to NxsString) was done to clean up
*/
/*!
Storage for a comment text and (end of the comment) file position information
*/
class NxsComment
{
public:
NxsComment(const std::string & s, long lineNumber, long colNumber)
:body(s),
line(lineNumber),
col(colNumber)
{}
long GetLineNumber() const
{
return line;
}
long GetColumnNumber() const
{
return col;
}
const std::string & GetText() const
{
return body;
}
void WriteAsNexus(std::ostream &out) const
{
out << '[' << body << ']';
}
private:
std::string body;
long line;
long col;
};
/*!
Storage for a file position, line number and column number.
*/
class NxsTokenPosInfo
{
public:
NxsTokenPosInfo()
:pos(0),
line(-1),
col(-1)
{}
NxsTokenPosInfo(file_pos position, long lineno, long columnno)
:pos(position),
line(lineno),
col(columnno)
{}
NxsTokenPosInfo(const NxsToken &);
file_pos GetFilePosition() const
{
return pos;
}
long GetLineNumber() const
{
return line;
}
long GetColumnNumber() const
{
return col;
}
file_pos pos; /* current file position */
long line; /* current line in file */
long col; /* column of current line */
};
/*!
A structure for storing the name of a command and to maps of option names
to value strings.
Produced by ProcessedNxsToken::ParseSimpleCmd (see that commands comments for rules on how it parses a NEXUS
command into a NxsSimpleCommandStrings struct).
*/
class NxsSimpleCommandStrings
{
public:
typedef std::vector<std::string> VecString;
typedef std::list<VecString> MatString;
typedef std::pair<NxsTokenPosInfo, std::string> SingleValFromFile;
typedef std::pair<NxsTokenPosInfo, VecString > MultiValFromFile;
typedef std::pair<NxsTokenPosInfo, MatString > MatFromFile;
typedef std::map<std::string, SingleValFromFile> StringToValFromFile;
typedef std::map<std::string, MultiValFromFile> StringToMultiValFromFile;
typedef std::map<std::string, MatFromFile> StringToMatFromFile;
// Looks for k in opts and multiOpts. Returns all of the values
// for the command option (will be an empty vector of strings if the option was not found).
// Case-sensitive!
// If an option is in multiOpts and opts, then only the value from opts will be returned!
MultiValFromFile GetOptValue(const std::string &k) const
{
MultiValFromFile mvff;
StringToValFromFile::const_iterator s = this->opts.find(k);
if (s != this->opts.end())
{
const SingleValFromFile & v(s->second);
mvff.first = v.first;
mvff.second.push_back(v.second);
}
else
{
StringToMultiValFromFile::const_iterator m = this->multiOpts.find(k);
if (m != this->multiOpts.end())
{
const MultiValFromFile & mv(m->second);
mvff.first = mv.first;
mvff.second = mv.second;
}
}
return mvff;
}
MatFromFile GetMatOptValue(const std::string & k) const
{
StringToMatFromFile::const_iterator mIt = this->matOpts.find(k);
if (mIt == this->matOpts.end())
return MatFromFile();
return mIt->second;
}
bool HasKey(const std::string k) const
{
if (this->opts.find(k) != this->opts.end())
return true;
return ((this->multiOpts.find(k) != this->multiOpts.end()) || (this->matOpts.find(k) != this->matOpts.end()));
}
std::string cmdName;
NxsTokenPosInfo cmdPos;
StringToValFromFile opts;
StringToMultiValFromFile multiOpts;
StringToMatFromFile matOpts;
};
/*!
Storage for a single NEXUS token, and embedded comments, along with end-of-the-token file position information.
*/
class ProcessedNxsToken
{
public:
static void IncrementNotLast(std::vector<ProcessedNxsToken>::const_iterator & it,
const std::vector<ProcessedNxsToken>::const_iterator &endIt,
const char * context);
static NxsSimpleCommandStrings ParseSimpleCmd(const std::vector<ProcessedNxsToken> &, bool convertToLower);
ProcessedNxsToken(const NxsToken &t);
ProcessedNxsToken(std::string &s)
:token(s)
{}
ProcessedNxsToken(std::string &s, file_pos position,long lineno, long columnno)
:token(s),
posInfo(position, lineno, columnno)
{}
std::string GetToken() const
{
return token;
}
const std::vector<NxsComment> & GetEmbeddedComments() const
{
return embeddedComments;
}
NxsTokenPosInfo GetFilePosInfo() const
{
return posInfo;
}
const NxsTokenPosInfo & GetFilePosInfoConstRef() const
{
return posInfo;
}
file_pos GetFilePosition() const
{
return posInfo.GetFilePosition();
}
long GetLineNumber() const
{
return posInfo.GetLineNumber();
}
long GetColumnNumber() const
{
return posInfo.GetColumnNumber();
}
bool Equals(const char *c) const
{
return NxsString::case_insensitive_equals(token.c_str(), c);
}
bool EqualsCaseSensitive(const char *c) const
{
return (strcmp(token.c_str(), c) == 0);
}
void SetEmbeddedComments(const std::vector<NxsComment> & c)
{
embeddedComments = c;
}
void WriteAsNexus(std::ostream &out) const
{
for(std::vector<NxsComment>::const_iterator cIt = embeddedComments.begin(); cIt != embeddedComments.end(); ++cIt)
cIt->WriteAsNexus(out);
out << NxsString::GetEscaped(token);
}
private:
std::string token;
NxsTokenPosInfo posInfo;
std::vector<NxsComment> embeddedComments; /* comments that were processed in the same GetToken operation that created this token. */
};
/*!
ProcessedNxsCommand is merely of a collection of ProcessedNxsToken objects. The NxsToken object can use a ; as a
separator to parse of its input stream until the next ";" and return a ProcessedNxsCommand.
See NxsToken::ProcessAsCommand method.
*/
typedef std::vector<ProcessedNxsToken> ProcessedNxsCommand;
bool WriteCommandAsNexus(std::ostream &, const ProcessedNxsCommand &);
/**---------------------------------------------------------------------------------------------------------------------
NxsToken objects are used by NxsReader to extract words (tokens) from a NEXUS data file. NxsToken objects know to
correctly skip NEXUS comments and understand NEXUS punctuation, making reading a NEXUS file as simple as repeatedly
calling the GetNextToken() function and then interpreting the token returned. If the token object is not attached
to an input stream, calls to GetNextToken() will have no effect. If the token object is not attached to an output
stream, output comments will be discarded (i.e., not output anywhere) and calls to Write or Writeln will be
ineffective. If input and output streams have been attached to the token object, however, tokens are read one at a
time from the input stream, and comments are correctly read and either written to the output stream (if an output
comment) or ignored (if not an output comment). Sequences of characters surrounded by single quotes are read in as
single tokens. A pair of adjacent single quotes are stored as a single quote, and underscore characters are stored
as blanks.
*/
class NxsToken
{
public:
static std::string EscapeString(const std::string &);
static bool NeedsQuotes(const std::string &);
static std::string GetQuoted(const std::string &);
static void DemandEndSemicolon(NxsToken &token, NxsString & errormsg, const char *contextString);
static unsigned DemandPositiveInt(NxsToken &token, NxsString & errormsg, const char *contextString);
static std::map<std::string, std::string> ParseAsSimpleKeyValuePairs(const ProcessedNxsCommand & tv, const char *cmdName);
static std::vector<ProcessedNxsToken> Tokenize(const std::string & );
enum NxsTokenFlags /* For use with the variable labileFlags */
{
saveCommandComments = 0x0001, /* if set, command comments of the form [&X] are not ignored but are instead saved as regular tokens (without the square brackets, however) */
parentheticalToken = 0x0002, /* if set, and if next character encountered is a left parenthesis, token will include everything up to the matching right parenthesis */
curlyBracketedToken = 0x0004, /* if set, and if next character encountered is a left curly bracket, token will include everything up to the matching right curly bracket */
doubleQuotedToken = 0x0008, /* if set, grabs entire phrase surrounded by double quotes */
singleCharacterToken = 0x0010, /* if set, next non-whitespace character returned as token */
newlineIsToken = 0x0020, /* if set, newline character treated as a token and atEOL set if newline encountered */
tildeIsPunctuation = 0x0040, /* if set, tilde character treated as punctuation and returned as a separate token */
useSpecialPunctuation = 0x0080, /* if set, character specified by the data member special is treated as punctuation and returned as a separate token */
hyphenNotPunctuation = 0x0100, /* if set, the hyphen character is not treated as punctutation (it is normally returned as a separate token) */
preserveUnderscores = 0x0200, /* if set, underscore characters inside tokens are not converted to blank spaces (normally, all underscores are automatically converted to blanks) */
ignorePunctuation = 0x0400 /* if set, the normal punctuation symbols are treated the same as any other darkspace characters */
};
NxsString errormsg;
NxsToken(std::istream &i);
virtual ~NxsToken();
bool AtEOF();
bool AtEOL();
bool Abbreviation(NxsString s);
bool Begins(NxsString s, bool respect_case = false);
void BlanksToUnderscores();
bool Equals(NxsString s, bool respect_case = false) const;
bool EqualsCaseSensitive(const char *c) const
{
return (strcmp(token.c_str(), c) == 0);
}
long GetFileColumn() const;
file_pos GetFilePosition() const;
long GetFileLine() const;
void GetNextToken();
NxsString GetToken(bool respect_case = true);
const char *GetTokenAsCStr(bool respect_case = true);
const NxsString &GetTokenReference() const;
int GetTokenLength() const;
bool IsPlusMinusToken();
bool IsPunctuationToken();
bool IsWhitespaceToken();
bool IsPlusMinusToken(const std::string & t);
bool IsPunctuationToken(const std::string & t);
bool IsWhitespaceToken(const std::string & t);
std::map<std::string, std::string> ProcessAsSimpleKeyValuePairs(const char *cmdName);
void ProcessAsCommand(ProcessedNxsCommand *tokenVec);
void ReplaceToken(const NxsString s);
void ResetToken();
void SetSpecialPunctuationCharacter(char c);
void SetLabileFlagBit(int bit);
bool StoppedOn(char ch);
void StripWhitespace();
void ToUpper();
void Write(std::ostream &out);
void Writeln(std::ostream &out);
virtual void OutputComment(const NxsString &msg);
void SetEOFAllowed(bool e)
{
eofAllowed = e;
}
bool GetEOFAllowed() const
{
return eofAllowed;
}
void SetBlockName(const char *);
std::string GetBlockName();
const std::vector<NxsComment> & GetEmbeddedComments() const
{
return embeddedComments;
}
char PeekAtNextChar() const;
/// Calling with `true` will force the NxsToken to only consider newick's
// punctuation characters to be punctuation (newick's punctuation
// chars are ()[]':;, this is a subset of NEXUS punctuation.
// Calling with `false` will restore NEXUS punctuation rules.
void UseNewickTokenization(bool v);
protected:
void AppendToComment(char ch);
void AppendToToken(char ch);
bool GetComment();
void GetCurlyBracketedToken();
void GetDoubleQuotedToken();
void GetQuoted();
void GetParentheticalToken();
bool IsPunctuation(char ch);
bool IsWhitespace(char ch);
private:
void AdvanceToNextCharInStream();
char GetNextChar();
//char ReadNextChar();
std::istream &inputStream; /* reference to input stream from which tokens will be read */
signed char nextCharInStream;
file_pos posOffBy; /* offset of the file pos (according to the stream) and the tokenizer (which is usually a character or two behind, due to saved chars */
file_pos usualPosOffBy; /* default of posOffBy. Usually this is -1, but it can be positive if a tokenizer is created from a substring of the file */
long fileLine; /* current file line */
long fileColumn; /* current column in current line (refers to column immediately following token just read) */
NxsString token; /* the character buffer used to store the current token */
NxsString comment; /* temporary buffer used to store output comments while they are being built */
bool eofAllowed;
signed char saved; /* either '\0' or is last character read from input stream */
bool atEOF; /* true if end of file has been encountered */
bool atEOL; /* true if newline encountered while newlineIsToken labile flag set */
char special; /* ad hoc punctuation character; default value is '\0' */
int labileFlags; /* storage for flags in the NxsTokenFlags enum */
char whitespace[4]; /* stores the 3 whitespace characters: blank space, tab and newline */
std::string currBlock;
std::vector<NxsComment> embeddedComments;
typedef bool (* CharPredFunc)(const char);
CharPredFunc isPunctuationFn;
};
typedef NxsToken NexusToken;
inline ProcessedNxsToken::ProcessedNxsToken(const NxsToken &t)
:token(t.GetTokenReference()),
posInfo(t)
{}
inline NxsTokenPosInfo::NxsTokenPosInfo(const NxsToken &t)
:pos(t.GetFilePosition()),
line(t.GetFileLine()),
col(t.GetFileColumn())
{}
/*!
Stores the current block name (for better error reporting only). Use NULL to clear the currBlock name.
*/
inline void NxsToken::SetBlockName(const char *c)
{
if (c == 0L)
currBlock.clear();
else
currBlock.assign(c);
}
/*!
Returns the token's block name (for better error reporting)
*/
inline std::string NxsToken::GetBlockName()
{
return currBlock;
}
/*!
Returns copy of s but with quoting according to the NEXUS Standard iff s needs to be quoted.
*/
inline std::string NxsToken::EscapeString(const std::string &s)
{
return NxsString::GetEscaped(s);
}
/*!
Returns the token for functions that only need read only access - faster than GetToken.
*/
inline const NxsString &NxsToken::GetTokenReference() const
{
return token;
}
/**
This function is called whenever an output comment (i.e., a comment beginning with an exclamation point) is found
in the data file.
This base-class version of OutputComment suppresses these messages. You can override this virtual function to display
the output comment in the most appropriate way for application platform you are supporting.
*/
inline void NxsToken::OutputComment(
const NxsString &) /* the contents of the printable comment discovered in the NEXUS data file */
{
}
/*!
Adds `ch' to end of comment NxsString.
*/
inline void NxsToken::AppendToComment(
char ch) /* character to be appended to comment */
{
comment += ch;
}
/*!
Adds `ch' to end of current token.
*/
inline void NxsToken::AppendToToken(
char ch) /* character to be appended to token */
{
token.push_back(ch);
}
/*!
Returns true if character supplied is considered a whitespace character. Note: treats '\n' as darkspace if labile
flag newlineIsToken is in effect.
*/
inline bool NxsToken::IsWhitespace(
char ch) /* the character in question */
{
bool ws = false;
// If ch is found in the whitespace array, it's whitespace
//
if (strchr(whitespace, ch) != NULL)
ws = true;
// Unless of course ch is the newline character and we're currently
// treating newlines as darkspace!
//
if (labileFlags & newlineIsToken && ch == '\n')
ws = false;
return ws;
}
/*!
Returns true if and only if last call to GetNextToken encountered the end-of-file character (or for some reason the
input stream is now out of commission).
*/
inline bool NxsToken::AtEOF()
{
return atEOF;
}
/*!
Returns true if and only if last call to GetNextToken encountered the newline character while the newlineIsToken
labile flag was in effect.
*/
inline bool NxsToken::AtEOL()
{
return atEOL;
}
/*!
Converts all blanks in token to underscore characters. Normally, underscores found in the tokens read from a NEXUS
file are converted to blanks automatically as they are read; this function reverts the blanks back to underscores.
*/
inline void NxsToken::BlanksToUnderscores()
{
token.BlanksToUnderscores();
}
/*!
Returns value stored in `filecol', which keeps track of the current column in the data file (i.e., number of
characters since the last new line was encountered).
*/
inline long NxsToken::GetFileColumn() const
{
return fileColumn;
}
/*!
Returns value stored in filepos, which keeps track of the current position in the data file (i.e., number of
characters since the beginning of the file). Note: for Metrowerks compiler, you must use the offset() method of
the streampos class to use the value returned.
*/
inline file_pos NxsToken::GetFilePosition() const
{
return inputStream.rdbuf()->pubseekoff(0,std::ios::cur, std::ios::in) + posOffBy;
}
/*!
Returns value stored in `fileline', which keeps track of the current line in the data file (i.e., number of new
lines encountered thus far).
*/
inline long NxsToken::GetFileLine() const
{
return fileLine;
}
/*!
Returns the data member `token'. Specifying false for`respect_case' parameter causes all characters in `token'
to be converted to upper case before `token' is returned. Specifying true results in GetToken returning exactly
what it read from the file.
*/
inline NxsString NxsToken::GetToken(
bool respect_case) /* determines whether token is converted to upper case before being returned */
{
if (!respect_case)
ToUpper();
return token;
}
/*!
Returns the data member `token' as a C-style string. Specifying false for`respect_case' parameter causes all
characters in `token' to be converted to upper case before the `token' C-string is returned. Specifying true
results in GetTokenAsCStr returning exactly what it read from the file.
*/
inline const char *NxsToken::GetTokenAsCStr(
bool respect_case) /* determines whether token is converted to upper case before being returned */
{
if (!respect_case)
ToUpper();
return token.c_str();
}
/*!
Returns token.size().
*/
inline int NxsToken::GetTokenLength() const
{
return (int)token.size();
}
/*!
Returns true if current token is a single character and this character is either '+' or '-'.
*/
inline bool NxsToken::IsPlusMinusToken()
{
return IsPlusMinusToken(token);
}
/*!
Returns true if t is a single character and this character is either '+' or '-'.
*/
inline bool NxsToken::IsPlusMinusToken(const std::string &t)
{
return (t.size() == 1 && ( t[0] == '+' || t[0] == '-') );
}
/*!
Returns true if character supplied is considered a punctuation character. The following twenty characters are
considered punctuation characters:
>
()[]{}/\,;:=*'"`+-<>
>
Exceptions:
~
o The tilde character ('~') is also considered punctuation if the tildeIsPunctuation labile flag is set
o The special punctuation character (specified using the SetSpecialPunctuationCharacter) is also considered
punctuation if the useSpecialPunctuation labile flag is set
o The hyphen (i.e., minus sign) character ('-') is not considered punctuation if the hyphenNotPunctuation
labile flag is set
~
Use the SetLabileFlagBit method to set one or more NxsLabileFlags flags in `labileFlags'
*/
inline bool NxsToken::IsPunctuation(
char ch) /* the character in question */
{
// PAUP 4.0b10
// o allows ]`<> inside taxon names
// o allows `<> inside taxset names
//
if (isPunctuationFn(ch))
{
if (labileFlags & hyphenNotPunctuation)
# if defined(NCL_VERSION_2_STYLE_HYPHEN) && NCL_VERSION_2_STYLE_HYPHEN
return (ch != '-');
# else
return (ch != '-' && ch != '+');
# endif
return true;
}
if (labileFlags & tildeIsPunctuation && ch == '~')
return true;
return (labileFlags & useSpecialPunctuation && ch == special);
}
/*!
Returns true if current token is a single character and this character is a punctuation character (as defined in
IsPunctuation function).
*/
inline bool NxsToken::IsPunctuationToken()
{
return IsPunctuationToken(token);
}
/*!
Returns true if t is a single character and this character is a punctuation character (as defined in
IsPunctuation function).
*/
inline bool NxsToken::IsPunctuationToken(const std::string &t)
{
return (t.size() == 1 && IsPunctuation(t[0]));
}
/*!
Returns true if current token is a single character and this character is a whitespace character (as defined in
IsWhitespace function).
*/
inline bool NxsToken::IsWhitespaceToken()
{
return IsWhitespaceToken(token);
}
/*!
Returns true if t is a single character and this character is a whitespace character (as defined in IsWhitespace function).
*/
inline bool NxsToken::IsWhitespaceToken(const std::string &t)
{
return (t.size() == 1 && IsWhitespace( t[0]));
}
/*!
Replaces current token NxsString with s.
*/
inline void NxsToken::ReplaceToken(
const NxsString s) /* NxsString to replace current token NxsString */
{
token = s;
}
/*!
Sets token to the empty NxsString ("").
*/
inline void NxsToken::ResetToken()
{
token.clear();
embeddedComments.clear();
}
/*!
Sets the special punctuation character to `c'. If the labile bit useSpecialPunctuation is set, this character will
be added to the standard list of punctuation symbols, and will be returned as a separate token like the other
punctuation characters.
*/
inline void NxsToken::SetSpecialPunctuationCharacter(
char c) /* the character to which `special' is set */
{
special = c;
}
/*!
Sets the bit specified in the variable `labileFlags'. The available bits are specified in the NxsTokenFlags enum.
All bits in `labileFlags' are cleared after each token is read.
*/
inline void NxsToken::SetLabileFlagBit(
int bit) /* the bit (see NxsTokenFlags enum) to set in `labileFlags' */
{
labileFlags |= bit;
}
/*!
Checks character stored in the variable saved to see if it matches supplied character `ch'. Good for checking such
things as whether token stopped reading characters because it encountered a newline (and labileFlags bit
newlineIsToken was set):
>
StoppedOn('\n');
>
or whether token stopped reading characters because of a punctuation character such as a comma:
>
StoppedOn(',');
>
*/
inline bool NxsToken::StoppedOn(
char ch) /* the character to compare with saved character */
{
if (saved == ch)
return true;
else
return false;
}
inline char NxsToken::PeekAtNextChar() const
{
return nextCharInStream;
}
/*!
Simply outputs the current NxsString stored in `token' to the output stream `out'. Does not send a newline to the
output stream afterwards.
*/
inline void NxsToken::Write(
std::ostream &out) /* the output stream to which to write token NxsString */
{
out << token;
}
/*!
Simply outputs the current NxsString stored in `token' to the output stream `out'. Sends a newline to the output
stream afterwards.
*/
inline void NxsToken::Writeln(
std::ostream &out) /* the output stream to which to write `token' */
{
out << token << std::endl;
}
inline std::map<std::string, std::string> NxsToken::ProcessAsSimpleKeyValuePairs(const char *cmdName)
{
ProcessedNxsCommand tokenVec;
ProcessAsCommand(&tokenVec);
return ParseAsSimpleKeyValuePairs(tokenVec, cmdName);
}
/*!
Returns true if token NxsString exactly equals `s'. If abbreviations are to be allowed, either Begins or
Abbreviation should be used instead of Equals.
*/
inline bool NxsToken::Equals(
NxsString s, /* the string for comparison to the string currently stored in this token */
bool respect_case) const /* if true, comparison will be case-sensitive */
{
if (respect_case)
return (strcmp(token.c_str(), s.c_str()) == 0);
return NxsString::case_insensitive_equals(token.c_str(), s.c_str());
}
#endif
|