source: trunk/lib/LineTypeParser.cc @ 978

Last change on this file since 978 was 978, checked in by Peter Johansson, 12 years ago

refs #429. Convert copyright statements to UTF-8

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.9 KB
Line 
1// $Id: LineTypeParser.cc 978 2009-12-12 20:09:41Z peter $
2
3/*
4  Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2009 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=search(line.begin(),line.end(),"Copyright (C)");
66        if (i!=line.end()) {
67          start_line_ = type_.size()+1;
68          copyright_prefix_ = line.substr(0, distance(line.begin(), i));
69          copyright_found_ = true;
70          block_ = line+"\n";
71        }
72      }
73    }
74    // we are in copyright block
75    if (copyright_found_ && !post_copyright_)
76      type_.push_back(LineTypeParser::copyright);
77
78    else if (codon_)
79      type_.push_back(code_mode(line));
80    else
81      type_.push_back(text_mode(line));
82    return type_.back();
83                     
84  }
85
86
87  LineTypeParser::line_type LineTypeParser::code_mode(const std::string& str)
88  {
89    // mode zero means we are currently not in a comment
90    // if mode!=0 comment is closed by codon[mode-1].second -> mode=0
91    // if codon[x-1].start is found and x >= mode -> mode=x
92    line_type lt=other;
93    {
94      for (std::string::const_iterator iter=str.begin();iter!=str.end();++iter){
95        for (size_t i=mode_; i<codon_->size(); ++i) {
96          if ( iter==str.begin() && (*codon_)[i].first[0] == '\n' && 
97               match_begin(iter, str.end(), (*codon_)[i].first.substr(1)) ) {
98            iter += (*codon_)[i].first.size()-1;
99            mode_ = i+1;
100            break;
101          }
102          if (match_begin(iter, str.end(), (*codon_)[i].first)) {
103            iter += (*codon_)[i].first.size();
104            mode_ = i+1;
105            break;
106          }
107        }
108        if (iter==str.end())
109          break;
110        assert(mode_==0 || mode_-1<(*codon_).size());
111        if (mode_ && match_begin(iter,str.end(), (*codon_)[mode_-1].second)){
112          iter += (*codon_)[mode_-1].second.size();
113          mode_=0;
114          if (iter==str.end())
115            break;
116        }
117        else if (!mode_ && isgraph(*iter))
118          lt=code;
119        else if (mode_ && lt!=code && isalnum(*iter))
120          lt=comment;
121      }
122      if (mode_ && (*codon_)[mode_-1].second==std::string("\n"))
123        mode_=0;
124    }
125    return lt;
126  }
127
128
129  LineTypeParser::line_type LineTypeParser::text_mode(const std::string& str)
130  {
131    for (std::string::const_iterator iter=str.begin(); iter!=str.end(); ++iter)
132      if (isalnum(*iter))
133        return comment;
134    return other;
135  }
136
137
138}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.