[go: up one dir, main page]

File: strmanip.cc

package info (click to toggle)
imms 3.1.0~svn301-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,320 kB
  • sloc: cpp: 7,810; ansic: 1,923; xml: 252; sh: 189; makefile: 142
file content (331 lines) | stat: -rw-r--r-- 9,129 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
 IMMS: Intelligent Multimedia Management System
 Copyright (C) 2001-2009 Michael Grigoriev

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>

#include <iostream>

#include "strmanip.h"
#include "immsutil.h"

using std::list;

Regexx rex;

string extradelims;

#define DELIM "[-\\s_\\.'\\(\\)\\[\\]]"
#define NUMRE "(^|" DELIM "+)(\\d+)($|" DELIM "+)"

string escape_char(char c)
{
    string s(1, c);
    switch (c)
    {
        case '[':
        case ']':
        case '(':
        case ')':
        case '.':
            s = "\\" + s;
        default:
            break;
    }
    return s;
}

void string_split(list<string> &store, const string &s, const string &delims)
{
    string escaped;
    for (unsigned i = 0; i < delims.length(); ++i)
        escaped += escape_char(delims[i]);
    string expr("(?>[^" + escaped + "]+)");
    rex.exec(s, expr, Regexx::global);
    store.insert(store.end(), rex.match.begin(), rex.match.end());
}

string trim(const string &s)
{
    size_t p = s.find_last_not_of(" ");
    return p == string::npos ? s : s.substr(0, p + 1);
}

bool imms_magic_preprocess_filename(string &filename)
{
    filename = rex.replace(filename, "[-\\s_\\.]{2,}", "/");

    bool confident = rex.matches();

    if (confident)
        return true;

    if (extradelims != "")
    {
        filename = rex.replace(filename, "[" + extradelims + "]",
                "/", Regexx::global);
        confident = rex.matches();
    }

    if (!confident)
    {
        int spaces = rex.exec(filename, " ", Regexx::global);
        int dashes = rex.exec(filename, "-", Regexx::global);
        int scores = rex.exec(filename, "_", Regexx::global);

        if ((!spaces || !scores) && dashes && dashes < 3
                && (spaces >= dashes || scores >= dashes))
            filename = rex.replace(filename, "-", "/", Regexx::global);
    }

    return confident;
}

void imms_magic_preprocess_path(string &path)
{
    path = string_tolower(path);
    path = rex.replace(path, "[-\\s_\\.]{2,}", "/", Regexx::global);
    path = rex.replace(path, "(/|^)[\\(\\[]", "/", Regexx::global);
    path = rex.replace(path, "[\\(\\[][^/]+[\\)\\]]/", "/", Regexx::global);
    path = rex.replace(path, "[-\\s_\\./][iv]{2}i?[/$]", "/", Regexx::global);
    path = rex.replace(path, "[^a-z/]", "", Regexx::global);
}

bool imms_magic_parse_filename(list<string> &store, string filename)
{
	bool result = imms_magic_preprocess_filename(filename);
	imms_magic_preprocess_path(filename);
    string_split(store, filename, "/");
    return result;
}

void imms_magic_parse_path(list<string> &store, string path)
{
    path = rex.replace(path, "/+$", "", Regexx::global);

    string lastdir = path_get_filename(path);
    path = path_get_dirname(path);

    imms_magic_preprocess_path(path);
    string_split(store, path, "/");

    imms_magic_preprocess_filename(lastdir);
    imms_magic_preprocess_path(lastdir);
    string_split(store, lastdir, "/");
}

string string_normalize(string s)
{
    s = string_brfilter(string_tolower(s));
    s = rex.replace(s, "[^a-z]", "", Regexx::global);
    return s;
}

bool string_like(const string &s1, const string &s2, int slack)
{
    int len1 = s1.length();
    int len2 = s2.length();

    int distance = lev_edit_distance(len1, s1.c_str(), len2, s2.c_str(), 0);
    return (len1 + len2) / (13 - slack) >= distance;
}

LevMatchingBlock *get_matching_blocks(const string &s1, const string &s2,
        size_t &n)
{
    int len1 = s1.length(), len2 = s2.length();
    size_t num_editops;
    LevEditOp *editops = lev_editops_find(len1, s1.c_str(), len2, s2.c_str(),
            &num_editops);

    LevMatchingBlock *blocks =
        lev_editops_matching_blocks(len1, len2, num_editops, editops, &n);

    free(editops);

    return blocks;
}

string filename_cleanup(const string &s)
{
    return string_tolower(rex.replace(s, "(\\d)", "#", Regexx::global));
}

string get_filename_mask(const string& path)
{
    string dirname = path_get_dirname(path);
    string filename = filename_cleanup(path_get_filename(path));
    string extension = path_get_extension(path);

    vector<string> files;
    if (listdir(dirname, files))
        return "";

    char *mask = new char[filename.length() + 1];
    memset(mask, 0, filename.length() + 1);

    int count = 0;
    for (vector<string>::iterator i = files.begin(); i != files.end(); i++)
    {
        if ((*i)[0] == '.' || path_get_extension(*i) == extension)
            continue;

        count++;
        size_t num_blocks;
        LevMatchingBlock *blocks = get_matching_blocks(filename,
                filename_cleanup(path_get_filename(*i)), num_blocks);

        for (size_t j = 0; j < num_blocks; j++)
        {
            for (size_t k = 0; k < blocks[j].len; k++)
                mask[blocks[j].spos + k]++;
        }

        free(blocks);
        if (count > 20)
            break;
    }

    string strmask = "";
    for (size_t i = 0; i < filename.length(); i++)
    {
        strmask += mask[i] > count * 0.7 ? filename[i] : '*';
    }

    delete[] mask;

    return strmask;
}

struct H
{
    static string filename;
    static string mask;
    static string double_erase(const regexx::RegexxMatch& _match)
    {
        mask.erase(_match.start(), _match.length());
        filename.erase(_match.start(), _match.length());

        return "";
    }
    static string numerals(const regexx::RegexxMatch& _match)
    {
        extradelims = "";
        string replacement = "/";
        int l1 = _match.atom[0].length(), l2 = _match.atom[2].length();

        if (l1 < 2 && l2 < 2)
        {
            if (_match.atom[0].str() != " " && _match.atom[0].str() != "_")
            {
                replacement = _match.atom[0].str();
                if (_match.atom[0].length() == 1)
                    extradelims += escape_char(_match.atom[0].str()[0]);
            }
            if (_match.atom[2].str() != " " && _match.atom[2].str() != "_")
            {
                replacement = _match.atom[2].str();
                if (_match.atom[2].length() == 1)
                    extradelims += escape_char(_match.atom[2].str()[0]);
            }
        }
        else
        {
            replacement = _match.atom[(!!(l1 < l2)) * 2].str();
        }

        mask.replace(_match.start(), _match.length(), replacement);
        filename.replace(_match.start(), _match.length(), replacement);
        return "";
    }
};

string H::filename, H::mask;

pair<string, string> get_simplified_filename_mask(const string &path)
{
    H::filename = string_tolower(path_get_filename(path));
    H::mask = get_filename_mask(path);

    if (rex.exec(H::mask, "(\\)|\\]|\\*[a-z]{0,3})-[a-z0-9]{3,4}$"))
        rex.replacef(H::mask, "-[a-z]{3,4}$", H::double_erase, Regexx::global);

    rex.replacef(H::filename,
            "[-\\s_\\.]*[\\(\\[][^\\]\\)]{0,60}[\\]\\)]?$",
            H::double_erase, Regexx::global);

    do
    {
        rex.replacef(H::filename, NUMRE, H::numerals, Regexx::global);
    } while (rex.matches());

    rex.replacef(H::filename, "^[-\\s_\\.']+|[-\\s_\\.']+$",
            H::double_erase, Regexx::global);

    return pair<string, string>(H::filename, H::mask);
}

string album_filter(const string &album)
{
    return string_normalize(rex.replace(string_tolower(album),
            "(lp|ep|cmd|promo|demo|maxi)$", "", Regexx::global));
}

string title_filter(const string &title)
{
    string normtitle = string_normalize(title);
    size_t p = title.rfind("- ");
    if (p == string::npos)
        return normtitle;
    return string_normalize(title.substr(p));
}

string path_get_dirname(const string &path)
{
    size_t last_slash = path.find_last_of("/") + 1;
    return path.substr(0, last_slash);
}

string path_get_filename(const string &path)
{
    size_t last_slash = path.find_last_of("/") + 1;
    size_t last_dot = path.find_last_of(".");

    if (last_dot == string::npos || last_dot < path.length() - 4)
        last_dot = path.length();

    return path.substr(last_slash, last_dot - last_slash);
}

string path_get_extension(const string &path)
{
    size_t last_dot = path.find_last_of(".");

    if (last_dot == string::npos)
        last_dot = path.length();
    else
        last_dot++;

    return path.substr(last_dot);
}

string string_delete(const string &haystack, const string &needle)
{
    return rex.replace(haystack, needle, "", Regexx::global);
}