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
|
/*--------------------------------------------------------------------*//*:Ignore this sentence.
Copyright (C) 1999, 2001 SIL International. All rights reserved.
Distributable under the terms of either the Common Public License or the
GNU Lesser General Public License, as specified in the LICENSING.txt file.
File: GrpLineAndFile.cpp
Responsibility: Sharon Correll
Last reviewed: Not yet.
Description:
A simple class to hold the line number and file name for tokens and objects.
-------------------------------------------------------------------------------*//*:End Ignore*/
#ifdef _MSC_VER
#pragma once
#endif
#ifndef LINENFILE_INCLUDED
#define LINENFILE_INCLUDED
#include <iostream>
class GrpLineAndFile // hungarian: lnf (line-n-file)
{
public:
// Constructors:
GrpLineAndFile()
: m_nLinePre(-1),
m_nLineOrig(-1)
{
}
GrpLineAndFile(int nPre, int nOrig, std::string sta)
: m_nLinePre(nPre),
m_nLineOrig(nOrig),
m_staFile(sta)
{
}
// copy constructor
GrpLineAndFile(const GrpLineAndFile & lnf)
{
m_nLinePre = lnf.m_nLinePre;
m_nLineOrig = lnf.m_nLineOrig;
m_staFile = lnf.m_staFile;
}
~GrpLineAndFile()
{
}
GrpLineAndFile & operator=(const GrpLineAndFile & lnf)
{
m_nLinePre = lnf.m_nLinePre;
m_nLineOrig = lnf.m_nLineOrig;
m_staFile = lnf.m_staFile;
return *this;
}
// Getters and setters:
int PreProcessedLine() const { return m_nLinePre; };
int OriginalLine() const { return m_nLineOrig; }
std::string File() const { return m_staFile; }
void SetOriginalLine(int n) { m_nLineOrig = n; }
void SetPreProcessedLine(int n) { m_nLinePre = n; }
void SetFile(std::string sta) { m_staFile = sta; }
void CopyLineAndFile(GrpLineAndFile & lnf)
{
m_nLinePre = lnf.m_nLinePre;
m_nLineOrig = lnf.m_nLineOrig;
m_staFile = lnf.m_staFile;
}
bool NotSet()
{
return m_nLinePre == -1;
}
bool operator==(GrpLineAndFile & lnf)
{
if (m_nLinePre == 0 && lnf.m_nLinePre == 0)
return (m_nLineOrig == lnf.m_nLineOrig); // eg, preprocessor errors
else
return m_nLinePre == lnf.m_nLinePre;
}
bool operator<(GrpLineAndFile & lnf)
{
if (m_nLinePre == 0 && lnf.m_nLinePre == 0)
return (m_nLineOrig < lnf.m_nLineOrig); // eg, preprocessor errors
else
return m_nLinePre < lnf.m_nLinePre;
}
void WriteToStream(std::ostream & strmOut, bool fNoPath)
{
size_t ich = m_staFile.length();
// Strip off the file path.
while (fNoPath && ich > 0 && m_staFile[ich - 1] != '\\')
ich--;
std::string staStripped = m_staFile.substr(ich, m_staFile.length() - ich);
strmOut << staStripped << "(" << m_nLineOrig << ")";
}
std::string FileWithPath(std::string staPath) // append the given path unless the file has an absolute path
{
std::string staResult;
if (m_staFile == "")
staResult = m_staFile;
else if (m_staFile[0] == '/' || m_staFile[1] == ':') // / is Linux, C: is Windows
{
// Absolute path
staResult = m_staFile;
}
else
{
// Relative path
staResult = staPath;
staResult.append(m_staFile);
}
// Make all the slashes consistent.
#ifdef _WIN32
char chSlash = '\\';
#else
char chSlash = '/';
#endif // _WIN32
for (size_t ich = 0; ich < staResult.length(); ich++)
{
if (staResult[ich] == '\\' || staResult[ich] == '/')
staResult[ich] = chSlash;
}
return staResult;
}
protected:
// instance variables:
int m_nLinePre; // line in pre-processed file
int m_nLineOrig; // actual line in original file
std::string m_staFile; // original file name
public:
// for test procedures:
void test_SetLineNumbers(int n)
{
m_nLinePre = n;
m_nLineOrig = n;
}
};
#endif // !LINENFILE_INCLUDED
|