"Fossies" - the Fresh Open Source Software Archive

Member "regexxer-0.10/src/fileshared.cc" (6 Oct 2011, 3105 Bytes) of package /linux/privat/old/regexxer-0.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "fileshared.cc" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (c) 2002-2007  Daniel Elstner  <daniel.kitta@gmail.com>
    3  *
    4  * This file is part of regexxer.
    5  *
    6  * regexxer is free software; you can redistribute it and/or modify
    7  * it under the terms of the GNU General Public License as published by
    8  * the Free Software Foundation; either version 2 of the License, or
    9  * (at your option) any later version.
   10  *
   11  * regexxer is distributed in the hope that it will be useful,
   12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14  * GNU General Public License for more details.
   15  *
   16  * You should have received a copy of the GNU General Public License
   17  * along with regexxer; if not, write to the Free Software Foundation,
   18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   19  */
   20 
   21 #include "fileshared.h"
   22 
   23 #include <glib.h>
   24 #include <algorithm>
   25 
   26 
   27 namespace
   28 {
   29 
   30 static
   31 const Glib::Quark& file_buffer_match_quark()
   32 {
   33   // Regexxer::FileBuffer uses anonymous Gtk::TextMark objects to remember
   34   // the position of matches.  This quark is used to identify a match mark,
   35   // in order to be able to distinguish it from other anonymous marks.
   36 
   37   static Glib::Quark quark ("regexxer-file-buffer-match-quark");
   38   return quark;
   39 }
   40 
   41 static
   42 int calculate_match_length(const Glib::ustring& subject, const std::pair<int, int>& bounds)
   43 {
   44   const std::string::const_iterator begin = subject.begin().base();
   45 
   46   const Glib::ustring::const_iterator start (begin + bounds.first);
   47   const Glib::ustring::const_iterator stop  (begin + bounds.second);
   48 
   49   return std::distance(start, stop);
   50 }
   51 
   52 } // anonymous namespace
   53 
   54 
   55 namespace Regexxer
   56 {
   57 
   58 /**** Regexxer::MatchData **************************************************/
   59 
   60 MatchData::MatchData(int match_index, const Glib::ustring& line,
   61                      Glib::MatchInfo& match_info)
   62 :
   63   index   (match_index),
   64   subject (line)
   65 {
   66   int capture_count = match_info.get_match_count();
   67   captures.reserve(capture_count);
   68 
   69   for (int i = 0; i < capture_count; ++i)
   70   {
   71     std::pair<int, int> bounds;
   72     match_info.fetch_pos(i, bounds.first, bounds.second);
   73     captures.push_back(bounds);
   74   }
   75 
   76   length = calculate_match_length(subject, captures.front());
   77 }
   78 
   79 MatchData::~MatchData()
   80 {
   81   // We *should* be the only one holding a reference to the Mark apart from
   82   // the Gtk::TextBuffer itself.  Nonetheless, let's better be extra careful
   83   // and invalidate the reference.
   84   if (mark)
   85     mark->steal_data(file_buffer_match_quark());
   86 }
   87 
   88 void MatchData::install_mark(const Gtk::TextBuffer::iterator& pos)
   89 {
   90   g_return_if_fail(!mark);
   91 
   92   mark = pos.get_buffer()->create_mark(pos, false); // right gravity
   93 
   94   mark->set_data(file_buffer_match_quark(), this);
   95 }
   96 
   97 // static
   98 bool MatchData::is_match_mark(const Glib::RefPtr<Gtk::TextMark>& textmark)
   99 {
  100   return (textmark->get_data(file_buffer_match_quark()) != 0);
  101 }
  102 
  103 // static
  104 Util::SharedPtr<MatchData> MatchData::get_from_mark(const Glib::RefPtr<Gtk::TextMark>& textmark)
  105 {
  106   return MatchDataPtr(static_cast<MatchData*>(textmark->get_data(file_buffer_match_quark())));
  107 }
  108 
  109 } // namespace Regexxer