libsaria: Created a generic format_text() function

This is used by the track tags for creating word lists.  I also use the
function to format filter search text.
This commit is contained in:
Bryan Schumaker 2011-11-13 10:47:55 -05:00
parent 1c5b230434
commit 1c0084b2e9
6 changed files with 83 additions and 49 deletions

15
include/libsaria/format.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef LIBSARIA_FORMAT_H
#define LIBSARIA_FORMAT_H
#include <list>
#include <string>
using namespace std;
namespace libsaria
{
void format_text(string &, list<string> &);
}
#endif /* LIBSARIA_FORMAT_H */

View File

@ -32,6 +32,7 @@ namespace libsaria
void for_each_path(void (*)(struct PathInfo &));
unsigned int size();
void filter(string &text);
}
}

View File

@ -30,7 +30,6 @@ class TrackTag
list<string> album_words;
void make_lenstr();
void format_tag(string &, list<string> &);
void format_tags();
public:

52
libsaria/format.cpp Normal file
View File

@ -0,0 +1,52 @@
#include <libsaria/format.h>
namespace libsaria
{
void format_text(string &text, list<string> &word_list)
{
string word;
char c, diff = 'a' - 'A';
for (unsigned int i = 0; i < text.size(); i++) {
c = text[i];
// Character already lower case
if ( (c >= 'a') && (c <= 'z') )
word += c;
// Convert uppercase to lowercase
else if ( (c >= 'A') && (c <= 'Z') )
word += (c + diff);
// Keep numbers
else if ( (c >= '0') && (c <= '9') )
word += c;
else {
// These characters indicate a new word
switch (c) {
case '-':
case '\\':
case '/':
case ',':
case ';':
case '(':
case ')':
case '_':
case '~':
case '+':
case '"':
case ' ':
if (word != "")
word_list.push_back(word);
word = "";
break;
default:
break;
};
}
}
if (word != "")
word_list.push_back(word);
}
} /* Namespace: libsaria */

View File

@ -3,6 +3,7 @@
#include <set>
using namespace std;
#include <libsaria/format.h>
#include "library.h"
static map<string, set<ino_t> > substr_index;
@ -93,4 +94,14 @@ namespace libsaria
reindex_path(it->get_list());
}
void library::filter(string &text)
{
list<string> key_list;
list<string>::iterator it;
format_text(text, key_list);
for (it = key_list.begin(); it != key_list.end(); it++)
println(*it);
}
}; /* Namespace: libsaria */

View File

@ -1,5 +1,6 @@
#include <libsaria/tags.h>
#include <libsaria/format.h>
#include <libsaria/print.h>
#include <taglib/tag.h>
@ -125,56 +126,11 @@ void TrackTag::make_lenstr()
lenstr = stream.str();
}
void TrackTag::format_tag(string &tag, list<string> &tag_list)
{
string word;
char c, diff = 'a' - 'A';
for (unsigned int i = 0; i < tag.size(); i++) {
c = tag[i];
// Character already lower case
if ( (c >= 'a') && (c <= 'z') )
word += c;
// Convert uppercase to lowercase
else if ( (c >= 'A') && (c <= 'Z') )
word += (c + diff);
// Keep numbers
else if ( (c >= '0') && (c <= '9') )
word += c;
else {
// These characters indicate a new word
switch (c) {
case '-':
case '\\':
case '/':
case ',':
case ';':
case '(':
case ')':
case '_':
case '~':
case '+':
case '"':
case ' ':
if (word != "")
tag_list.push_back(word);
word = "";
break;
default:
break;
};
}
}
if (word != "")
tag_list.push_back(word);
}
void TrackTag::format_tags()
{
format_tag(title, title_words);
format_tag(artist, artist_words);
format_tag(album, album_words);
libsaria::format_text(title, title_words);
libsaria::format_text(artist, artist_words);
libsaria::format_text(album, album_words);
}
ino_t TrackTag::get_inode()