[Debtags-commits] [svn] r1752 - in tagcoll/2.0: . tagcoll
tagcoll/tests
Enrico Zini
enrico at costa.debian.org
Wed May 10 01:58:05 UTC 2006
Author: enrico
Date: Wed May 10 01:58:02 2006
New Revision: 1752
Removed:
tagcoll/2.0/tagcoll/test-utils.cc
Modified:
tagcoll/2.0/ (props changed)
tagcoll/2.0/tagcoll/Makefile.am
tagcoll/2.0/tagcoll/TextFormat.cc
tagcoll/2.0/tagcoll/TextFormat.h
tagcoll/2.0/tagcoll/TextFormat.tcc
tagcoll/2.0/tagcoll/tests/test-utils.h
Log:
r2685 at viaza: enrico | 2006-05-09 20:51:01 -0500
Refactored TextFormat as well.
Modified: tagcoll/2.0/tagcoll/Makefile.am
==============================================================================
--- tagcoll/2.0/tagcoll/Makefile.am (original)
+++ tagcoll/2.0/tagcoll/Makefile.am Wed May 10 01:58:02 2006
@@ -89,9 +89,7 @@
\
DerivedTags.cc \
\
- SmartHierarchy.cc \
- \
- test-utils.cc
+ SmartHierarchy.cc
# BasicStringDiskIndex.cc \
# \
Modified: tagcoll/2.0/tagcoll/TextFormat.cc
==============================================================================
--- tagcoll/2.0/tagcoll/TextFormat.cc (original)
+++ tagcoll/2.0/tagcoll/TextFormat.cc Wed May 10 01:58:02 2006
@@ -19,6 +19,7 @@
*/
#include <tagcoll/TextFormat.h>
+#include <tagcoll/Patches.h>
using namespace std;
using namespace wibble;
@@ -152,6 +153,60 @@
return input::Input::Eof;
}
+
+void outputPatch(const PatchList<std::string, std::string>& patch, FILE* out)
+{
+ for (PatchList<std::string, std::string>::const_iterator i = patch.begin();
+ i != patch.end(); i++)
+ {
+ if (fprintf(out, "%s: ", i->first.c_str()) < 0)
+ throw wibble::exception::System("writing item");
+
+ bool start = true;
+
+ std::set<string> stags;
+ for (std::set<std::string>::const_iterator j = i->second.getAdded().begin();
+ j != i->second.getAdded().end(); j++)
+ {
+ if (start)
+ {
+ if (fprintf(out, "+%s", j->c_str()) < 0)
+ throw wibble::exception::System("writing patched tag");
+ start = false;
+ } else
+ if (fprintf(out, ", +%s", j->c_str()) < 0)
+ throw wibble::exception::System("writing patched tag");
+ }
+ for (std::set<std::string>::const_iterator j = i->second.getRemoved().begin();
+ j != i->second.getRemoved().end(); j++)
+ {
+ if (start)
+ {
+ if (fprintf(out, "-%s", j->c_str()) < 0)
+ throw wibble::exception::System("writing patched tag");
+ start = false;
+ } else
+ if (fprintf(out, ", -%s", j->c_str()) < 0)
+ throw wibble::exception::System("writing patched tag");
+ }
+
+ if (fprintf(out, "\n") < 0)
+ throw wibble::exception::System("writing newline after tagset");
+ }
+}
+
+struct Pass
+{
+ std::string operator()(const std::string& str) const { return str; }
+};
+
+PatchList<std::string, std::string> parsePatch(input::Input& in)
+{
+ PatchList<string, string> patch;
+ parse(in, patchBuilder(patch, Pass(), Pass()));
+ return patch;
+}
+
}
}
@@ -198,8 +253,7 @@
"d: +c::D, -e::F, -f::g\n"
);
- TrivialConverter<string, string> a;
- PatchList<string, string> plist(textformat::parsePatch(a, a, coll));
+ PatchList<string, string> plist = textformat::parsePatch(coll);
/*
cerr << "Patchlist[" << plist.size() << "]:" << endl;
Modified: tagcoll/2.0/tagcoll/TextFormat.h
==============================================================================
--- tagcoll/2.0/tagcoll/TextFormat.h (original)
+++ tagcoll/2.0/tagcoll/TextFormat.h Wed May 10 01:58:02 2006
@@ -6,7 +6,7 @@
*/
/*
- * Copyright (C) 2003,2004,2005 Enrico Zini <enrico at debian.org>
+ * Copyright (C) 2003--2006 Enrico Zini <enrico at debian.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -23,10 +23,9 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
+#include <wibble/mixin.h>
#include <wibble/empty.h>
#include <wibble/singleton.h>
-#include <tagcoll/Consumer.h>
-#include <tagcoll/Serializer.h>
#include <tagcoll/input/base.h>
#include <stdio.h>
@@ -37,6 +36,8 @@
{
template<class ITEM, class TAG>
class PatchList;
+template<class ITEM, class TAG>
+class Patch;
namespace textformat
{
@@ -84,13 +85,18 @@
/**
* Serialize a patch
*/
-template<typename ITEM, typename TAG>
+template<typename ITEM, typename TAG, typename ITEMSER, typename TAGSER>
void outputPatch(
- Converter<ITEM, std::string>& itemconv,
- Converter<TAG, std::string>& tagconv,
+ ITEMSER& itemconv,
+ TAGSER& tagconv,
const PatchList<ITEM, TAG>& patch,
FILE* out);
+void outputPatch(
+ const PatchList<std::string, std::string>& patch,
+ FILE* out);
+
+
/*
* Parse a tagged collection, sending the results to out.
*
@@ -103,52 +109,47 @@
/**
* Parse a tagcoll patch
*/
-template<typename ITEM, typename TAG>
+template<typename ITEM, typename TAG, typename ITEMSER, typename TAGSER>
PatchList<ITEM, TAG> parsePatch(
- Converter<std::string, ITEM>& itemconv,
- Converter<std::string, TAG>& tagconv,
+ ITEMSER& itemconv,
+ TAGSER& tagconv,
input::Input& in);
-}
+PatchList<std::string, std::string> parsePatch(input::Input& in);
-// This is outside of the textformat namespace for backwards compatibility.
-// New code should just use StdioWriter or an analogous new class
-template<class ITEM, class TAG>
-class TextFormat : public Consumer<ITEM, TAG>
+
+
+template<typename ITEM, typename TAG, typename ITEMSER, typename TAGSER>
+class PatchBuilder : public wibble::mixin::OutputIterator< PatchBuilder<ITEM, TAG, ITEMSER, TAGSER> >
{
protected:
- const Converter<ITEM, std::string>& itemconv;
- const Converter<TAG, std::string>& tagconv;
- textformat::StdioWriter writer;
-
- static int parseElement(input::Input& in, std::string& item) throw (tagcoll::exception::Parser);
-
- virtual void consumeItemUntagged(const ITEM& item)
- {
- writer = make_pair(wibble::singleton(itemconv(item)), wibble::Empty<std::string>());
- ++writer;
- }
- virtual void consumeItem(const ITEM& item, const std::set<TAG>& tags)
- {
- writer = make_pair(wibble::singleton(itemconv(item)), tagconv(tags));
- ++writer;
- }
- virtual void consumeItemsUntagged(const std::set<ITEM>& items)
- {
- writer = make_pair(itemconv(items), wibble::Empty<std::string>());
- ++writer;
- }
- virtual void consumeItems(const std::set<ITEM>& items, const std::set<TAG>& tags)
- {
- writer = make_pair(itemconv(items), tagconv(tags));
- ++writer;
- }
+ PatchList<ITEM, TAG>& patch;
+ const ITEMSER& itemconv;
+ const TAGSER& tagconv;
public:
- TextFormat(const Converter<ITEM, std::string>& itemconv, const Converter<TAG, std::string>& tagconv, FILE* out)
- : itemconv(itemconv), tagconv(tagconv), writer(out) {}
+ PatchBuilder(
+ PatchList<ITEM, TAG>& patch,
+ const ITEMSER& itemconv,
+ const TAGSER& tagconv)
+ : patch(patch), itemconv(itemconv), tagconv(tagconv) {}
+
+ template<typename ITEMS, typename TAGS>
+ PatchBuilder<ITEM, TAG, ITEMSER, TAGSER>& operator=(const std::pair<ITEMS, TAGS>& data);
+
+ const PatchList<ITEM, TAG>& getPatch() const throw () { return patch; }
};
+template<typename ITEM, typename TAG, typename ITEMSER, typename TAGSER>
+PatchBuilder<ITEM, TAG, ITEMSER, TAGSER> patchBuilder(
+ PatchList<ITEM, TAG>& patch,
+ const ITEMSER& itemconv,
+ const TAGSER& tagconv)
+{
+ return PatchBuilder<ITEM, TAG, ITEMSER, TAGSER>(patch, itemconv, tagconv);
+}
+
+}
}
// vim:set ts=4 sw=4:
Modified: tagcoll/2.0/tagcoll/TextFormat.tcc
==============================================================================
--- tagcoll/2.0/tagcoll/TextFormat.tcc (original)
+++ tagcoll/2.0/tagcoll/TextFormat.tcc Wed May 10 01:58:02 2006
@@ -1,7 +1,7 @@
/*
* Serialize a tagged collection to a text file
*
- * Copyright (C) 2003,2004,2005 Enrico Zini <enrico at debian.org>
+ * Copyright (C) 2003--2006 Enrico Zini <enrico at debian.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -18,6 +18,9 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
+#ifndef TAGCOLL_TEXTFORMAT_TCC
+#define TAGCOLL_TEXTFORMAT_TCC
+
#include <tagcoll/TextFormat.h>
#include <tagcoll/Patches.h>
@@ -142,10 +145,10 @@
-template<class ITEM, class TAG>
+template<typename ITEM, typename TAG, typename ITEMSER, typename TAGSER>
void outputPatch(
- Converter<ITEM, std::string>& itemconv,
- Converter<TAG, std::string>& tagconv,
+ ITEMSER& itemconv,
+ TAGSER& tagconv,
const PatchList<ITEM, TAG>& patch,
FILE* out)
{
@@ -170,97 +173,49 @@
}
}
-
-#if 0
-template<class ITEM, class TAG>
-void TextFormat<ITEM, TAG>::parse(
- Converter<std::string, ITEM>& itemconv,
- Converter<std::string, TAG>& tagconv,
- input::Input& in,
- Consumer<ITEM, TAG>& consumer)
-{
- ConversionFilter<string, string, ITEM, TAG> conv(itemconv, tagconv, consumer);
- parseTextFormat(in, conv);
-}
-#endif
-
-template<class ITEM, class TAG>
-class PatchBuilder : public Consumer<string, string>
-{
-protected:
- PatchList<ITEM, TAG> patch;
- const Converter<std::string, ITEM>& itemconv;
- const Converter<std::string, TAG>& tagconv;
-
- virtual void consumeItemUntagged(const string&) {}
-
- virtual void consumeItem(const string& item, const std::set<string>& tags)
- {
- ITEM it = itemconv(item);
- if (it == ITEM())
- return;
-
- Patch<ITEM, TAG> p(it);
- for (std::set<string>::const_iterator i = tags.begin(); i != tags.end(); i++)
- {
- TAG tag = tagconv(i->substr(1));
- if (tag != TAG())
- if ((*i)[0] == '-')
- p.remove(tag);
- else if ((*i)[0] == '+')
- p.add(tag);
- }
- patch.addPatch(p);
+template<typename ITEM, typename TAG, typename ITEMSER, typename TAGSER>
+template<typename ITEMS, typename TAGS>
+PatchBuilder<ITEM, TAG, ITEMSER, TAGSER>& PatchBuilder<ITEM, TAG, ITEMSER, TAGSER>::operator=(const std::pair<ITEMS, TAGS>& data)
+{
+ std::set<TAG> added;
+ std::set<TAG> removed;
+
+ for (typename TAGS::const_iterator i = data.second.begin();
+ i != data.second.end(); ++i)
+ {
+ TAG tag = tagconv(i->substr(1));
+ if (tag != TAG())
+ if ((*i)[0] == '-')
+ removed.insert(tag);
+ else if ((*i)[0] == '+')
+ added.insert(tag);
}
- virtual void consumeItemsUntagged(const std::set<string>&) {}
-
- virtual void consumeItems(const std::set<string>& items, const std::set<string>& tags)
+ for (typename ITEMS::const_iterator i = data.first.begin();
+ i != data.first.end(); ++i)
{
- std::set<TAG> added;
- std::set<TAG> removed;
-
- for (std::set<string>::const_iterator i = tags.begin(); i != tags.end(); i++)
- {
- TAG tag = tagconv(i->substr(1));
- if (tag != TAG())
- if ((*i)[0] == '-')
- removed |= tag;
- else if ((*i)[0] == '+')
- added |= tag;
- }
-
- for (std::set<string>::const_iterator i = items.begin(); i != items.end(); i++)
- {
- ITEM it = itemconv(*i);
- if (it != ITEM())
- patch.addPatch(Patch<ITEM, TAG>(it, added, removed));
- }
+ ITEM it = itemconv(*i);
+ if (it != ITEM())
+ patch.addPatch(Patch<ITEM, TAG>(it, added, removed));
}
+ return *this;
+}
-public:
- PatchBuilder(
- const Converter<std::string, ITEM>& itemconv,
- const Converter<std::string, TAG>& tagconv)
- : itemconv(itemconv), tagconv(tagconv) {}
- virtual ~PatchBuilder() {}
-
- const PatchList<ITEM, TAG>& getPatch() const throw () { return patch; }
-};
-
-template<class ITEM, class TAG>
+template<typename ITEM, typename TAG, typename ITEMSER, typename TAGSER>
PatchList<ITEM, TAG> parsePatch(
- Converter<std::string, ITEM>& itemconv,
- Converter<std::string, TAG>& tagconv,
+ ITEMSER& itemconv,
+ TAGSER& tagconv,
input::Input& in)
{
- PatchBuilder<ITEM, TAG> builder(itemconv, tagconv);
- parse(in, consumer(builder));
- return builder.getPatch();
+ PatchList<ITEM, TAG> patch;
+ parse(in, patchBuilder(patch, itemconv, tagconv));
+ return patch;
}
}
}
+#endif
+
// vim:set ts=4 sw=4:
Modified: tagcoll/2.0/tagcoll/tests/test-utils.h
==============================================================================
--- tagcoll/2.0/tagcoll/tests/test-utils.h (original)
+++ tagcoll/2.0/tagcoll/tests/test-utils.h Wed May 10 01:58:02 2006
@@ -9,6 +9,7 @@
#include <wibble/tests.h>
#include <string>
+#include <set>
#define TEST_TAGCOLL
More information about the Debtags-commits
mailing list