2010-06-16 14:27:24 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2012-2014 Mnemosyne LLC
|
2010-06-16 14:27:24 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2010-06-16 14:27:24 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <stdio.h> /* fprintf() */
|
|
|
|
#include <string.h> /* strlen(), strstr(), strcmp() */
|
2011-03-16 18:04:23 +00:00
|
|
|
#include <stdlib.h> /* EXIT_FAILURE */
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
#include <event2/buffer.h>
|
2010-06-16 14:27:24 +00:00
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
2015-04-11 10:51:59 +00:00
|
|
|
#include <libtransmission/error.h>
|
2010-06-16 14:27:24 +00:00
|
|
|
#include <libtransmission/tr-getopt.h>
|
|
|
|
#include <libtransmission/utils.h>
|
2012-12-14 04:34:42 +00:00
|
|
|
#include <libtransmission/variant.h>
|
2010-08-08 22:53:24 +00:00
|
|
|
#include <libtransmission/version.h>
|
2010-06-16 14:27:24 +00:00
|
|
|
|
|
|
|
#define MY_NAME "transmission-edit"
|
|
|
|
|
2010-08-08 22:53:24 +00:00
|
|
|
static int fileCount = 0;
|
2011-03-22 15:19:54 +00:00
|
|
|
static bool showVersion = false;
|
2021-10-06 16:32:17 +00:00
|
|
|
static char const** files = nullptr;
|
|
|
|
static char const* add = nullptr;
|
|
|
|
static char const* deleteme = nullptr;
|
|
|
|
static char const* replace[2] = { nullptr, nullptr };
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static tr_option options[] = {
|
2019-07-14 12:40:41 +00:00
|
|
|
{ 'a', "add", "Add a tracker's announce URL", "a", true, "<url>" },
|
|
|
|
{ 'd', "delete", "Delete a tracker's announce URL", "d", true, "<url>" },
|
|
|
|
{ 'r', "replace", "Search and replace a substring in the announce URLs", "r", true, "<old> <new>" },
|
2021-10-06 16:32:17 +00:00
|
|
|
{ 'V', "version", "Show version number and exit", "V", false, nullptr },
|
|
|
|
{ 0, nullptr, nullptr, nullptr, false, nullptr }
|
2010-06-16 14:27:24 +00:00
|
|
|
};
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* getUsage(void)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return "Usage: " MY_NAME " [options] torrent-file(s)";
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static int parseCommandLine(int argc, char const* const* argv)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int c;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* optarg;
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
while ((c = tr_getopt(getUsage(), argc, argv, options, &optarg)) != TR_OPT_DONE)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (c)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'a':
|
2012-12-05 17:29:46 +00:00
|
|
|
add = optarg;
|
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'd':
|
2012-12-05 17:29:46 +00:00
|
|
|
deleteme = optarg;
|
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'r':
|
2012-12-05 17:29:46 +00:00
|
|
|
replace[0] = optarg;
|
2017-04-19 12:04:45 +00:00
|
|
|
c = tr_getopt(getUsage(), argc, argv, options, &optarg);
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (c != TR_OPT_UNK)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
replace[1] = optarg;
|
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'V':
|
2012-12-05 17:29:46 +00:00
|
|
|
showVersion = true;
|
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_OPT_UNK:
|
2012-12-05 17:29:46 +00:00
|
|
|
files[fileCount++] = optarg;
|
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
2012-12-05 17:29:46 +00:00
|
|
|
return 1;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return 0;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool removeURL(tr_variant* metainfo, char const* url)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* str;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* announce_list;
|
|
|
|
bool changed = false;
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (tr_variantDictFindStr(metainfo, TR_KEY_announce, &str, nullptr) && strcmp(str, url) == 0)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
printf("\tRemoved \"%s\" from \"announce\"\n", str);
|
|
|
|
tr_variantDictRemove(metainfo, TR_KEY_announce);
|
|
|
|
changed = true;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindList(metainfo, TR_KEY_announce_list, &announce_list))
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* tier;
|
|
|
|
int tierIndex = 0;
|
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
while ((tier = tr_variantListChild(announce_list, tierIndex)) != nullptr)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int nodeIndex = 0;
|
2020-11-09 03:31:02 +00:00
|
|
|
tr_variant const* node;
|
2021-10-06 16:32:17 +00:00
|
|
|
while ((node = tr_variantListChild(tier, nodeIndex)) != nullptr)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
if (tr_variantGetStr(node, &str, nullptr) && strcmp(str, url) == 0)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-30 16:30:03 +00:00
|
|
|
printf("\tRemoved \"%s\" from \"announce-list\" tier #%d\n", str, tierIndex + 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantListRemove(tier, nodeIndex);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++nodeIndex;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantListSize(tier) == 0)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-30 16:30:03 +00:00
|
|
|
printf("\tNo URLs left in tier #%d... removing tier\n", tierIndex + 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantListRemove(announce_list, tierIndex);
|
2012-12-05 17:29:46 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
++tierIndex;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantListSize(announce_list) == 0)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
printf("\tNo tiers left... removing announce-list\n");
|
|
|
|
tr_variantDictRemove(metainfo, TR_KEY_announce_list);
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* if we removed the "announce" field and there's still another track left,
|
|
|
|
* use it as the "announce" field */
|
2021-10-06 16:32:17 +00:00
|
|
|
if (changed && !tr_variantDictFindStr(metainfo, TR_KEY_announce, &str, nullptr))
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2020-11-01 21:47:57 +00:00
|
|
|
tr_variant* const tier = tr_variantListChild(announce_list, 0);
|
2021-10-06 16:32:17 +00:00
|
|
|
if (tier != nullptr)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2020-11-01 21:47:57 +00:00
|
|
|
tr_variant const* const node = tr_variantListChild(tier, 0);
|
2021-10-06 16:32:17 +00:00
|
|
|
if ((node != nullptr) && tr_variantGetStr(node, &str, nullptr))
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2020-11-01 21:47:57 +00:00
|
|
|
tr_variantDictAddStr(metainfo, TR_KEY_announce, str);
|
|
|
|
printf("\tAdded \"%s\" to announce\n", str);
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return changed;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char* replaceSubstr(char const* str, char const* in, char const* out)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2020-11-09 03:31:02 +00:00
|
|
|
char const* walk;
|
|
|
|
struct evbuffer* const buf = evbuffer_new();
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const inlen = strlen(in);
|
|
|
|
size_t const outlen = strlen(out);
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
while ((walk = strstr(str, in)) != nullptr)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add(buf, str, walk - str);
|
|
|
|
evbuffer_add(buf, out, outlen);
|
|
|
|
str = walk + inlen;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add(buf, str, strlen(str));
|
2011-01-30 16:40:11 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
return evbuffer_free_to_str(buf, nullptr);
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool replaceURL(tr_variant* metainfo, char const* in, char const* out)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* str;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* announce_list;
|
|
|
|
bool changed = false;
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (tr_variantDictFindStr(metainfo, TR_KEY_announce, &str, nullptr) && strstr(str, in) != nullptr)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* newstr = replaceSubstr(str, in, out);
|
|
|
|
printf("\tReplaced in \"announce\": \"%s\" --> \"%s\"\n", str, newstr);
|
|
|
|
tr_variantDictAddStr(metainfo, TR_KEY_announce, newstr);
|
|
|
|
tr_free(newstr);
|
|
|
|
changed = true;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindList(metainfo, TR_KEY_announce_list, &announce_list))
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* tier;
|
|
|
|
int tierCount = 0;
|
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
while ((tier = tr_variantListChild(announce_list, tierCount)) != nullptr)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* node;
|
|
|
|
int nodeCount = 0;
|
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
while ((node = tr_variantListChild(tier, nodeCount)) != nullptr)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
if (tr_variantGetStr(node, &str, nullptr) && strstr(str, in) != nullptr)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char* newstr = replaceSubstr(str, in, out);
|
2019-03-17 05:00:15 +00:00
|
|
|
printf("\tReplaced in \"announce-list\" tier %d: \"%s\" --> \"%s\"\n", tierCount + 1, str, newstr);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantFree(node);
|
|
|
|
tr_variantInitStr(node, newstr, TR_BAD_SIZE);
|
|
|
|
tr_free(newstr);
|
|
|
|
changed = true;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
2019-03-17 05:00:15 +00:00
|
|
|
|
|
|
|
++nodeCount;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
2019-03-17 05:00:15 +00:00
|
|
|
|
|
|
|
++tierCount;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return changed;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool announce_list_has_url(tr_variant* announce_list, char const* url)
|
2011-09-07 04:21:45 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int tierCount = 0;
|
2020-11-09 03:31:02 +00:00
|
|
|
tr_variant* tier;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
while ((tier = tr_variantListChild(announce_list, tierCount)) != nullptr)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int nodeCount = 0;
|
2020-11-09 03:31:02 +00:00
|
|
|
tr_variant const* node;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
while ((node = tr_variantListChild(tier, nodeCount)) != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
char const* str = nullptr;
|
|
|
|
if (tr_variantGetStr(node, &str, nullptr) && strcmp(str, url) == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2019-03-17 05:00:15 +00:00
|
|
|
|
|
|
|
++nodeCount;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2019-03-17 05:00:15 +00:00
|
|
|
|
|
|
|
++tierCount;
|
2011-09-07 04:21:45 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return false;
|
2011-09-07 04:21:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static bool addURL(tr_variant* metainfo, char const* url)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2021-10-06 16:32:17 +00:00
|
|
|
char const* announce = nullptr;
|
|
|
|
tr_variant* announce_list = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
bool changed = false;
|
2021-10-06 16:32:17 +00:00
|
|
|
bool const had_announce = tr_variantDictFindStr(metainfo, TR_KEY_announce, &announce, nullptr);
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const had_announce_list = tr_variantDictFindList(metainfo, TR_KEY_announce_list, &announce_list);
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!had_announce && !had_announce_list)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* this new tracker is the only one, so add it to "announce"... */
|
|
|
|
printf("\tAdded \"%s\" in \"announce\"\n", url);
|
|
|
|
tr_variantDictAddStr(metainfo, TR_KEY_announce, url);
|
|
|
|
changed = true;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!had_announce_list)
|
2011-09-07 04:21:45 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
announce_list = tr_variantDictAddList(metainfo, TR_KEY_announce_list, 2);
|
2012-02-03 16:44:07 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (had_announce)
|
2011-09-07 04:21:45 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/* we're moving from an 'announce' to an 'announce-list',
|
|
|
|
* so copy the old announce URL to the list */
|
|
|
|
tr_variant* tier = tr_variantListAddList(announce_list, 1);
|
|
|
|
tr_variantListAddStr(tier, announce);
|
|
|
|
changed = true;
|
2011-09-07 04:21:45 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* If the user-specified URL isn't in the announce list yet, add it */
|
|
|
|
if (!announce_list_has_url(announce_list, url))
|
2011-09-07 04:21:45 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* tier = tr_variantListAddList(announce_list, 1);
|
|
|
|
tr_variantListAddStr(tier, url);
|
|
|
|
printf("\tAdded \"%s\" to \"announce-list\" tier %zu\n", url, tr_variantListSize(announce_list));
|
|
|
|
changed = true;
|
2011-09-07 04:21:45 +00:00
|
|
|
}
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return changed;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int tr_main(int argc, char* argv[])
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int changedCount = 0;
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
files = tr_new0(char const*, argc);
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logSetLevel(TR_LOG_ERROR);
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (parseCommandLine(argc, (char const* const*)argv) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (showVersion)
|
2010-08-08 22:53:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, MY_NAME " " LONG_VERSION_STRING "\n");
|
|
|
|
return EXIT_SUCCESS;
|
2010-08-08 22:53:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (fileCount < 1)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "ERROR: No torrent files specified.\n");
|
|
|
|
tr_getopt_usage(MY_NAME, getUsage(), options);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
return EXIT_FAILURE;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (add == nullptr && deleteme == nullptr && replace[0] == 0)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "ERROR: Must specify -a, -d or -r\n");
|
|
|
|
tr_getopt_usage(MY_NAME, getUsage(), options);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
return EXIT_FAILURE;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < fileCount; ++i)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant top;
|
|
|
|
bool changed = false;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* filename = files[i];
|
2021-10-06 16:32:17 +00:00
|
|
|
tr_error* error = nullptr;
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
printf("%s\n", filename);
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tr_variantFromFile(&top, TR_VARIANT_FMT_BENC, filename, &error))
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
printf("\tError reading file: %s\n", error->message);
|
|
|
|
tr_error_free(error);
|
|
|
|
continue;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (deleteme != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
changed |= removeURL(&top, deleteme);
|
|
|
|
}
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (add != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
changed = addURL(&top, add);
|
|
|
|
}
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-10-06 16:32:17 +00:00
|
|
|
if (replace[0] != nullptr && replace[1] != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
changed |= replaceURL(&top, replace[0], replace[1]);
|
|
|
|
}
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (changed)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
++changedCount;
|
|
|
|
tr_variantToFile(&top, TR_VARIANT_FMT_BENC, filename);
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantFree(&top);
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
printf("Changed %d files\n", changedCount);
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
tr_free((void*)files);
|
2017-04-19 12:04:45 +00:00
|
|
|
return EXIT_SUCCESS;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|