source: trunk/lib/LineTypeParser.cc @ 687

Last change on this file since 687 was 687, checked in by Peter Johansson, 13 years ago

Merged patch release 0.6.6 to trunk. Delta 0.6.6 - 0.6.5

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