1 ///###////////////////////////////////////////////////////////////////////////
2 //
3 // Burton Computer Corporation
4 // http://www.burton-computer.com
5 // http://www.cooldevtools.com
6 // $Id: HeaderParser.cc 272 2007-01-06 19:37:27Z brian $
7 //
8 // Copyright (C) 2007 Burton Computer Corporation
9 // ALL RIGHTS RESERVED
10 //
11 // This program is open source software; you can redistribute it
12 // and/or modify it under the terms of the Q Public License (QPL)
13 // version 1.0. Use of this software in whole or in part, including
14 // linking it (modified or unmodified) into other programs is
15 // subject to the terms of the QPL.
16 //
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 // Q Public License for more details.
21 //
22 // You should have received a copy of the Q Public License
23 // along with this program; see the file LICENSE.txt.  If not, visit
24 // the Burton Computer Corporation or CoolDevTools web site
25 // QPL pages at:
26 //
27 //    http://www.burton-computer.com/qpl.html
28 //    http://www.cooldevtools.com/qpl.html
29 //
30 
31 #include "AbstractMultiLineString.h"
32 #include "MultiLineString.h"
33 #include "MessageHeader.h"
34 #include "MessageHeaderList.h"
35 #include "HeaderParser.h"
36 
parseHeader(const CRef<AbstractMultiLineString> & headerText)37 Ref<MessageHeaderList> HeaderParser::parseHeader(const CRef<AbstractMultiLineString> &headerText)
38 {
39     m_headerText = headerText;
40     m_headerList = make_ref(new MessageHeaderList());
41     m_currentName.erase();
42     m_currentLines.clear();
43     parse();
44     return m_headerList;
45 }
46 
isContinuationLine(const string & line)47 bool HeaderParser::isContinuationLine(const string &line)
48 {
49     return is_space(line[0]);
50 }
51 
addHeader()52 void HeaderParser::addHeader()
53 {
54     if (m_currentName.length() > 0 && m_currentLines.isNotNull()) {
55         m_headerList->addHeader(new MessageHeader(m_currentName, m_currentLines));
56     }
57     m_currentName.erase();
58     m_currentLines.clear();
59 }
60 
addLineToCurrent(const string & line)61 void HeaderParser::addLineToCurrent(const string &line)
62 {
63     if (m_currentLines.isNotNull()) {
64         m_currentLines->addLine(line);
65     }
66 }
67 
startHeader(const string & line)68 void HeaderParser::startHeader(const string &line)
69 {
70     string::size_type colon_index = line.find(':');
71     if (colon_index == string::npos) {
72         return;
73     }
74 
75     m_currentName.assign(line, 0, colon_index);
76     m_currentLines = make_ref(new MultiLineString());
77     m_currentLines->addLine(trim_copy(line, colon_index + 1));
78 }
79 
parse()80 void HeaderParser::parse()
81 {
82     for (int i = 0; i < m_headerText->lineCount(); ++i) {
83         const string &line(m_headerText->line(i));
84         if (line.length() > 0) {
85             if (isContinuationLine(line)) {
86                 addLineToCurrent(line);
87             } else {
88                 addHeader();
89                 startHeader(line);
90             }
91         }
92     }
93     addHeader();
94 }
95