source: trunk/lib/LineTypeParser.cc @ 1513

Last change on this file since 1513 was 1513, checked in by Peter Johansson, 9 years ago

remove trailing whitespace

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.9 KB
Line 
1// $Id: LineTypeParser.cc 1513 2012-09-23 04:09:08Z peter $
2
3/*
4  Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2009, 2010 Peter Johansson
6
7  This file is part of svndigest, http://dev.thep.lu.se/svndigest
8
9  svndigest is free software; you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 3 of the License, or
12  (at your option) any later version.
13
14  svndigest is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "LineTypeParser.h"
24#include "Configuration.h"
25#include "utility.h"
26
27#include <algorithm>
28#include <cassert>
29#include <functional>
30#include <fstream>
31#include <iostream>
32#include <string>
33#include <utility>
34#include <vector>
35
36namespace theplu{
37namespace svndigest{
38
39
40  LineTypeParser::LineTypeParser(std::string path)
41    : mode_(0), post_copyright_(false), copyright_found_(false)
42  {
43    codon_ = Configuration::instance().codon(file_name(path));
44  }
45
46
47  LineTypeParser::line_type LineTypeParser::parse(std::string line)
48  {
49    if (!post_copyright_) {
50      if (copyright_found_) {
51        // check if line is end of copyright statement, i.e. contains
52        // no alphanumerical character (except in copyright_prefix).
53        post_copyright_ = true;
54        for (size_t i=0; i<line.size()&&post_copyright_; ++i)
55          if (isalnum(line[i]) &&
56              !(i<copyright_prefix_.size() && copyright_prefix_[i]==line[i])){
57            post_copyright_ = false;
58            block_ += line + "\n";
59          }
60        if (post_copyright_)
61          end_line_ = type_.size()+1;
62      }
63      else {
64        // check whether copyright starts on this line
65        std::string::iterator i =
66          search(line.begin(), line.end(),
67                 Configuration::instance().copyright_string());
68        if (i!=line.end()) {
69          start_line_ = type_.size()+1;
70          copyright_prefix_ = line.substr(0, distance(line.begin(), i));
71          copyright_found_ = true;
72          block_ = line+"\n";
73        }
74      }
75    }
76    // we are in copyright block
77    if (copyright_found_ && !post_copyright_)
78      type_.push_back(LineTypeParser::copyright);
79
80    else if (codon_)
81      type_.push_back(code_mode(line));
82    else
83      type_.push_back(text_mode(line));
84    return type_.back();
85
86  }
87
88
89  LineTypeParser::line_type LineTypeParser::code_mode(const std::string& str)
90  {
91    // mode zero means we are currently not in a comment
92    // if mode!=0 comment is closed by codon[mode-1].second -> mode=0
93    // if codon[x-1].start is found and x >= mode -> mode=x
94    line_type lt=other;
95    {
96      for (std::string::const_iterator iter=str.begin();iter!=str.end();++iter){
97        for (size_t i=mode_; i<codon_->size(); ++i) {
98          if ( iter==str.begin() && (*codon_)[i].first[0] == '\n' &&
99               match_begin(iter, str.end(), (*codon_)[i].first.substr(1)) ) {
100            iter += (*codon_)[i].first.size()-1;
101            mode_ = i+1;
102            break;
103          }
104          if (match_begin(iter, str.end(), (*codon_)[i].first)) {
105            iter += (*codon_)[i].first.size();
106            mode_ = i+1;
107            break;
108          }
109        }
110        if (iter==str.end())
111          break;
112        assert(mode_==0 || mode_-1<(*codon_).size());
113        if (mode_ && match_begin(iter,str.end(), (*codon_)[mode_-1].second)){
114          iter += (*codon_)[mode_-1].second.size();
115          mode_=0;
116          if (iter==str.end())
117            break;
118        }
119        else if (!mode_ && isgraph(*iter))
120          lt=code;
121        else if (mode_ && lt!=code && isalnum(*iter))
122          lt=comment;
123      }
124      if (mode_ && (*codon_)[mode_-1].second==std::string("\n"))
125        mode_=0;
126    }
127    return lt;
128  }
129
130
131  LineTypeParser::line_type LineTypeParser::text_mode(const std::string& str)
132  {
133    for (std::string::const_iterator iter=str.begin(); iter!=str.end(); ++iter)
134      if (isalnum(*iter))
135        return comment;
136    return other;
137  }
138
139
140}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.