[glibmm] glibmm: Add a MatchInfo class for use with the existing Regex class.



commit 45ea6b0db7986a6bceca0f9494950434599ba072
Author: José Alburquerque <jaalburqu svn gnome org>
Date:   Sun Dec 12 23:04:45 2010 -0500

    	glibmm: Add a MatchInfo class for use with the existing Regex class.
    
    	* glib/src/regex.{ccg,hg}: Add a new (generic) class wrapping the
    	GMatchInfo functions.  Wrap the (commented out) methods with a
    	Glib::MatchInfo in Regex so that the new class is used.
    
    	Fixes Bug #636911 (Alexander Shaduri)

 ChangeLog          |   10 +++
 glib/src/regex.ccg |  140 +++++++++++++++++++++++++++++++++++++++++++-
 glib/src/regex.hg  |  165 +++++++++++++++++++++++++++++++++++++++++----------
 3 files changed, 280 insertions(+), 35 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 07150f6..f1f812c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2010-12-12  José Alburquerque  <jaalburqu svn gnome org>
+
+	glibmm: Add a MatchInfo class for use with the existing Regex class.
+
+	* glib/src/regex.{ccg,hg}: Add a new (generic) class wrapping the
+	GMatchInfo functions.  Wrap the (commented out) methods with a
+	Glib::MatchInfo in Regex so that the new class is used.
+
+	Fixes Bug #636911 (Alexander Shaduri)
+
 2010-12-09  Murray Cumming  <murrayc murrayc-desktop>
 
 	Added generic gmmproc conversions previously in convert_gtkmm.m4.
diff --git a/glib/src/regex.ccg b/glib/src/regex.ccg
index 1d71cd1..af91d4a 100644
--- a/glib/src/regex.ccg
+++ b/glib/src/regex.ccg
@@ -41,11 +41,66 @@ Glib::ustring Regex::escape_string(const Glib::ustring& string)
   return Glib::ustring(buf.get());
 }
 
+bool Regex::match(
+  const Glib::ustring& string,
+  Glib::MatchInfo& match_info,
+  RegexMatchFlags match_options
+)
+{
+  GMatchInfo* ginfo = 0;
+  bool const result = static_cast<bool>(g_regex_match(gobj(), string.c_str(),
+    static_cast<GRegexMatchFlags>(match_options), &ginfo));
+  match_info.set_gobject(ginfo);
+  return result;
+}
+
 bool Regex::match(const Glib::ustring& string, RegexMatchFlags match_options)
 {
   return g_regex_match(gobj(), string.c_str(), (GRegexMatchFlags)(match_options), 0);
 }
 
+bool Regex::match(
+  const Glib::ustring& string,
+  int start_position,
+  Glib::MatchInfo& match_info,
+  RegexMatchFlags match_options
+)
+{
+  GError* gerror = 0;
+  GMatchInfo* ginfo = 0;
+
+  bool const result = static_cast<bool>(g_regex_match_full(gobj(),
+    string.c_str(), -1, start_position,
+    static_cast<GRegexMatchFlags>(match_options), &ginfo, &gerror));
+
+  if(gerror)
+    ::Glib::Error::throw_exception(gerror);
+
+  match_info.set_gobject(ginfo);
+  return result;
+}
+
+bool Regex::match(
+  const Glib::ustring& string,
+  gssize string_len,
+  int start_position,
+  Glib::MatchInfo& match_info,
+  RegexMatchFlags match_options
+)
+{
+  GError* gerror = 0;
+  GMatchInfo* ginfo = 0;
+
+  bool const result = static_cast<bool>(g_regex_match_full(gobj(),
+    string.c_str(), string_len, start_position,
+    static_cast<GRegexMatchFlags>(match_options), &ginfo, &gerror));
+
+  if(gerror)
+    ::Glib::Error::throw_exception(gerror);
+
+  match_info.set_gobject(ginfo);
+  return result;
+}
 
 bool Regex::match(const Glib::ustring& string, int start_position, RegexMatchFlags match_options)
 {
@@ -67,12 +122,67 @@ bool Regex::match(const Glib::ustring& string, gssize string_len, int start_posi
   return retvalue;
 }
 
+bool Regex::match_all(
+  const Glib::ustring& string,
+  Glib::MatchInfo& match_info,
+  RegexMatchFlags match_options
+)
+{
+  GMatchInfo* ginfo = 0;
+  bool const result = static_cast<bool>(g_regex_match_all(gobj(),
+    string.c_str(), static_cast<GRegexMatchFlags>(match_options),
+    &ginfo));
+  match_info.set_gobject(ginfo);
+  return result;
+}
 
 bool Regex::match_all(const Glib::ustring& string, RegexMatchFlags match_options)
 {
   return g_regex_match_all(gobj(), string.c_str(), ((GRegexMatchFlags)(match_options)), 0);
 }
 
+bool Regex::match_all(
+  const Glib::ustring& string,
+  int start_position,
+  Glib::MatchInfo& match_info,
+  RegexMatchFlags match_options
+)
+{
+  GError* gerror = 0;
+  GMatchInfo* ginfo = 0;
+
+  bool const retvalue = static_cast<bool>(g_regex_match_all_full(gobj(),
+    string.c_str(), -1, start_position,
+    static_cast<GRegexMatchFlags>(match_options), &ginfo, &gerror));
+
+  if(gerror)
+    ::Glib::Error::throw_exception(gerror);
+
+  match_info.set_gobject(ginfo);
+  return retvalue;
+}
+
+bool Regex::match_all(
+  const Glib::ustring& string,
+  gssize string_len,
+  int start_position,
+  Glib::MatchInfo& match_info,
+  RegexMatchFlags match_options
+)
+{
+  GError* gerror = 0;
+  GMatchInfo* ginfo = 0;
+
+  bool const retvalue = static_cast<bool>(g_regex_match_all_full(gobj(),
+    string.c_str(), string_len, start_position,
+    static_cast<GRegexMatchFlags>(match_options), &ginfo, &gerror));
+
+  if(gerror)
+    ::Glib::Error::throw_exception(gerror);
+
+  match_info.set_gobject(ginfo);
+  return retvalue;
+}
 
 bool Regex::match_all(const Glib::ustring& string, int start_position, RegexMatchFlags match_options)
 {
@@ -94,8 +204,6 @@ bool Regex::match_all(const Glib::ustring& string, gssize string_len, int start_
   return retvalue;
 }
 
-
-
 Glib::ustring Regex::replace(const Glib::ustring& string, int start_position, const Glib::ustring& replacement, RegexMatchFlags match_options)
 {
   GError* gerror = 0;
@@ -127,4 +235,32 @@ Glib::StringArrayHandle Regex::split(const Glib::ustring& string, int start_posi
   return retvalue;
 }
 
+
+MatchInfo::MatchInfo()
+: gobject_(0),
+  take_ownership(false)
+{
+}
+
+MatchInfo::MatchInfo(GMatchInfo* castitem, bool take_ownership)
+: gobject_(castitem),
+  take_ownership(take_ownership)
+{
+}
+
+void MatchInfo::set_gobject(GMatchInfo* castitem, bool take_ownership)
+{
+  if(gobject_ && take_ownership)
+    g_match_info_free(gobject_);
+
+  gobject_ = castitem;
+  this->take_ownership = take_ownership;
+}
+
+MatchInfo::~MatchInfo()
+{
+  if(take_ownership && gobject_)
+    g_match_info_free(gobject_);
+}
+
 } // namespace Glib
diff --git a/glib/src/regex.hg b/glib/src/regex.hg
index 15ba60e..1ce730b 100644
--- a/glib/src/regex.hg
+++ b/glib/src/regex.hg
@@ -39,6 +39,8 @@ _WRAP_ENUM(RegexMatchFlags, GRegexMatchFlags, NO_GTYPE)
 _WRAP_GERROR(RegexError, GRegexError, G_REGEX_ERROR, NO_GTYPE)
 
 
+class MatchInfo;
+
 /** Perl-compatible regular expressions - matches strings against regular expressions.
  *
  * The Glib::Regex functions implement regular expression pattern matching using 
@@ -110,23 +112,77 @@ public:
 
   _WRAP_METHOD(static bool match_simple(const Glib::ustring& pattern, const Glib::ustring& string, RegexCompileFlags compile_options = static_cast<RegexCompileFlags>(0), RegexMatchFlags match_options = static_cast<RegexMatchFlags>(0)), g_regex_match_simple)
 
-  //TODO: _WRAP_METHOD(bool match(const Glib::ustring& string, RegexMatchFlags match_options = (RegexMatchFlags)0, GMatchInfo **match_info = 0), g_regex_match)
+  _WRAP_METHOD_DOCS_ONLY(g_regex_match)
+  bool match(
+    const Glib::ustring& string,
+    Glib::MatchInfo& match_info,
+    RegexMatchFlags match_options = static_cast<RegexMatchFlags>(0)
+  );
+
+  /// A match() method not requiring a Glib::MatchInfo.
   bool match(const Glib::ustring& string, RegexMatchFlags match_options = static_cast<RegexMatchFlags>(0));
 
-  //TODO: Wrap GMatchInfo as an iterator:
-  //_WRAP_METHOD(bool match_full(const gchar* string, gssize string_len, int start_position, RegexMatchFlags match_options = (RegexMatchFlags)0, GMatchInfo** match_info = 0), g_regex_match_full, errthrow)
+  /// A match() method with a start position and a Glib::MatchInfo.
+  bool match(
+    const Glib::ustring& string,
+    int start_position,
+    Glib::MatchInfo& match_info,
+    RegexMatchFlags match_options = static_cast<RegexMatchFlags>(0)
+  );
 
+  _WRAP_METHOD_DOCS_ONLY(g_regex_match_full)
+  bool match(
+    const Glib::ustring& string,
+    gssize string_len,
+    int start_position,
+    Glib::MatchInfo& match_info,
+    RegexMatchFlags match_options = static_cast<RegexMatchFlags>(0)
+  );
+
+  /// A match() method with a start position not requiring a Glib::MatchInfo.
   bool match(const Glib::ustring& string, int start_position, RegexMatchFlags match_options);
 
+  /** A match() method with a string length and start position not requiring a
+   * Glib::MatchInfo.
+   */
   bool match(const Glib::ustring& string, gssize string_len, int start_position, RegexMatchFlags match_options);
 
-  //TODO: _WRAP_METHOD(bool match_all(const Glib::ustring& string, RegexMatchFlags match_options = (RegexMatchFlags)0, GMatchInfo ** match_info = 0), g_regex_match_all)
+  _WRAP_METHOD_DOCS_ONLY(g_regex_match_all)
+  bool match_all(
+    const Glib::ustring& string,
+    Glib::MatchInfo& match_info,
+    RegexMatchFlags match_options = static_cast<RegexMatchFlags>(0)
+  );
+
+  /// A match_all() method not requiring a Glib::MatchInfo.
   bool match_all(const Glib::ustring& string, RegexMatchFlags match_options = static_cast<RegexMatchFlags>(0));
 
-  //TODO: _WRAP_METHOD(bool match_all_full(const gchar* string, gssize string_len, int start_position, RegexMatchFlags match_options = (RegexMatchFlags)0, GMatchInfo** match_info = 0), g_regex_match_all_full, errthrow)
+  /// A match_all() method with a start positon and a Glib::MatchInfo.
+  bool match_all(
+    const Glib::ustring& string,
+    int start_position,
+    Glib::MatchInfo& match_info,
+    RegexMatchFlags match_options = static_cast<RegexMatchFlags>(0)
+  );
 
+  _WRAP_METHOD_DOCS_ONLY(g_regex_match_all_full, errthrow)
+  /// @throw Glib::Error.
+  bool match_all(
+    const Glib::ustring& string,
+    gssize string_len,
+    int start_position,
+    Glib::MatchInfo& match_info,
+    RegexMatchFlags match_options = static_cast<RegexMatchFlags>(0)
+  );
+
+  /** A match_all() method with a start position not requiring a
+   * Glib::MatchInfo.
+   */
   bool match_all(const Glib::ustring& string, int start_position, RegexMatchFlags match_options);
 
+  /** A match_all() method with a start position and a string length not
+   * requiring a Glib::MatchInfo.
+   */
   bool match_all(const Glib::ustring& string, gssize string_len, int start_position, RegexMatchFlags match_options);
 
 #m4 _CONVERSION(`gchar**',`Glib::StringArrayHandle',`Glib::StringArrayHandle($3, Glib::OWNERSHIP_DEEP)')
@@ -146,35 +202,78 @@ public:
 
   _WRAP_METHOD(Glib::ustring replace_eval(const Glib::ustring& string, gssize string_len, int start_position, RegexMatchFlags match_options, GRegexEvalCallback eval,  gpointer user_data), g_regex_replace_eval, errthrow)
   _WRAP_METHOD(static bool check_replacement(const Glib::ustring& replacement, gboolean* has_references), g_regex_check_replacement, errthrow)
+};
+
+//TODO: Add C++ iterator like functionality for this class.
+/** MatchInfo - MatchInfo is used to retrieve information about the regular
+ * expression match which created it.
+ * @newin{2,28}
+ */
+class MatchInfo
+{
+  _CLASS_GENERIC(MatchInfo, GMatchInfo)
+
+public:
+  /// Default constructor.
+  MatchInfo();
+
+  /** C object constructor.
+   * @param castitem The C object.
+   * @param take_ownership Whether to destroy the C object with the wrapper or
+   * not.
+   */
+  explicit MatchInfo(GMatchInfo* castitem, bool take_ownership = true);
+
+  /// Destructor.
+  virtual ~MatchInfo();
+
+  /// Provides access to the underlying C object.
+  GMatchInfo* gobj()
+    { return reinterpret_cast<GMatchInfo*>(gobject_); }
+
+  /// Provides access to the underlying C object.
+  const GMatchInfo* gobj() const
+    { return reinterpret_cast<GMatchInfo*>(gobject_); }
+
+private:
+// noncopyable
+  MatchInfo(const MatchInfo& other);
+  MatchInfo& operator=(const MatchInfo& other);
+
+  friend class Regex;
+
+public:
+
+  _WRAP_METHOD(Glib::RefPtr<Regex> get_regex(), g_match_info_get_regex)
+  _WRAP_METHOD(Glib::RefPtr<const Regex> get_regex() const, g_match_info_get_regex, constversion)
+
+  _WRAP_METHOD(Glib::ustring get_string() const, g_match_info_get_string)
+  _WRAP_METHOD(bool matches() const, g_match_info_matches)
+
+  _WRAP_METHOD(bool next(), g_match_info_next, errthrow)
+
+  _WRAP_METHOD(int get_match_count() const, g_match_info_get_match_count)
+  _WRAP_METHOD(bool is_partial_match() const, g_match_info_is_partial_match)
+
+  _WRAP_METHOD(Glib::ustring expand_references(const Glib::ustring& string_to_expand), g_match_info_expand_references, errthrow)
+
+  _WRAP_METHOD(Glib::ustring fetch(int match_num), g_match_info_fetch)
+
+  _WRAP_METHOD(bool fetch_pos(int match_num, int& start_pos, int& end_pos), g_match_info_fetch_pos)
+
+  _WRAP_METHOD(Glib::ustring fetch_named(const Glib::ustring& name), g_match_info_fetch_named)
+
+  _WRAP_METHOD(bool fetch_named_pos(const Glib::ustring& name, int& start_pos, int& end_pos), g_match_info_fetch_named_pos)
+
+  _WRAP_METHOD(Glib::StringArrayHandle fetch_all(), g_match_info_fetch_all)
+
+protected:
+  GMatchInfo* gobject_;      // The C object.
+  bool take_ownership;       // Bool signaling ownership.
 
-/* Match info */
-/*
-GRegex		 *g_match_info_get_regex	(const GMatchInfo    *match_info);
-const gchar      *g_match_info_get_string       (const GMatchInfo    *match_info);
-
-void		  g_match_info_free		(GMatchInfo          *match_info);
-  _WRAP_METHOD(bool g_match_info_next		(GMatchInfo          *match_info,
-						 GError             **error);
-  _WRAP_METHOD(bool g_match_info_matches		(const GMatchInfo    *match_info);
-  _WRAP_METHOD(int g_match_info_get_match_count	(const GMatchInfo    *match_info);
-  _WRAP_METHOD(bool g_match_info_is_partial_match	(const GMatchInfo    *match_info);
-Glib::ustring g_match_info_expand_references(const GMatchInfo    *match_info,
-						 Glib::ustring& string_to_expand,
-						 GError             **error);
-Glib::ustring g_match_info_fetch		(const GMatchInfo    *match_info,
-						 int match_num);
-  _WRAP_METHOD(bool g_match_info_fetch_pos	(const GMatchInfo    *match_info,
-						 int match_num,
-						 int                 *start_pos,
-						 int                 *end_pos);
-Glib::ustring g_match_info_fetch_named	(const GMatchInfo    *match_info,
-						 Glib::ustring& name);
-  _WRAP_METHOD(bool g_match_info_fetch_named_pos	(const GMatchInfo    *match_info,
-						 Glib::ustring& name,
-						 int                 *start_pos,
-						 int                 *end_pos);
-gchar		**g_match_info_fetch_all	(const GMatchInfo    *match_info);
-*/
+protected:
+  // So that Glib::Regex::match() can set the C object.
+  void set_gobject(GMatchInfo* castitem, bool take_ownership = true);
 };
 
 } // namespace Glib



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]