mirror of
https://github.com/moparisthebest/hexchat
synced 2024-11-28 12:12:16 -05:00
Use Gio for random_line()
This commit is contained in:
parent
29000e857f
commit
63a6f4a81f
@ -78,40 +78,72 @@ notc_msg (struct session *sess)
|
||||
static char *
|
||||
random_line (char *file_name)
|
||||
{
|
||||
FILE *fh;
|
||||
char buf[512];
|
||||
int lines, ran;
|
||||
GFile *file;
|
||||
char *data, *p, *ret = NULL;
|
||||
int lines = 0, ran;
|
||||
gsize len;
|
||||
|
||||
if (!file_name[0])
|
||||
goto nofile;
|
||||
return g_strdup ("");
|
||||
|
||||
fh = hexchat_fopen_file (file_name, "r", 0);
|
||||
if (!fh)
|
||||
file = hexchat_open_gfile (file_name);
|
||||
|
||||
if (!g_file_query_exists (file, NULL))
|
||||
{
|
||||
nofile:
|
||||
/* reason is not a file, an actual reason! */
|
||||
return strdup (file_name);
|
||||
g_object_unref (file);
|
||||
return g_strdup (file_name);
|
||||
}
|
||||
|
||||
if (!g_file_load_contents (file, NULL, &data, &len, NULL, NULL))
|
||||
{
|
||||
g_object_unref (file);
|
||||
return g_strdup (file_name);
|
||||
}
|
||||
|
||||
g_object_unref (file);
|
||||
|
||||
/* count number of lines in file */
|
||||
lines = 0;
|
||||
while (fgets (buf, sizeof (buf), fh))
|
||||
lines++;
|
||||
|
||||
if (lines < 1)
|
||||
goto nofile;
|
||||
|
||||
/* go down a random number */
|
||||
rewind (fh);
|
||||
ran = RAND_INT (lines);
|
||||
do
|
||||
p = data;
|
||||
while (p != data + len)
|
||||
{
|
||||
fgets (buf, sizeof (buf), fh);
|
||||
lines--;
|
||||
if (*p == '\n')
|
||||
lines++;
|
||||
p++;
|
||||
}
|
||||
while (lines > ran);
|
||||
fclose (fh);
|
||||
return strdup (buf);
|
||||
|
||||
if (!lines)
|
||||
{
|
||||
g_free (data);
|
||||
return g_strdup (file_name);
|
||||
}
|
||||
|
||||
/* create random number */
|
||||
ran = RAND_INT (lines);
|
||||
|
||||
/* get that random line */
|
||||
p = data;
|
||||
while (p != data + len)
|
||||
{
|
||||
if (*p == '\n')
|
||||
{
|
||||
if (!--ran)
|
||||
{
|
||||
char *end;
|
||||
end = strchr (++p, '\n');
|
||||
end = '\0';
|
||||
ret = g_strdup (p);
|
||||
break;
|
||||
}
|
||||
}
|
||||
p++;
|
||||
}
|
||||
|
||||
g_free (data);
|
||||
if (ret)
|
||||
return ret;
|
||||
else
|
||||
return g_strdup (file_name);
|
||||
}
|
||||
|
||||
void
|
||||
@ -121,7 +153,7 @@ server_sendpart (server * serv, char *channel, char *reason)
|
||||
{
|
||||
reason = random_line (prefs.hex_irc_part_reason);
|
||||
serv->p_part (serv, channel, reason);
|
||||
free (reason);
|
||||
g_free (reason);
|
||||
} else
|
||||
{
|
||||
/* reason set by /quit, /close argument */
|
||||
@ -139,7 +171,7 @@ server_sendquit (session * sess)
|
||||
colrea = strdup (prefs.hex_irc_quit_reason);
|
||||
check_special_chars (colrea, FALSE);
|
||||
rea = random_line (colrea);
|
||||
free (colrea);
|
||||
g_free (colrea);
|
||||
sess->server->p_quit (sess->server, rea);
|
||||
free (rea);
|
||||
} else
|
||||
@ -380,10 +412,10 @@ cmd_away (struct session *sess, char *tbuf, char *word[], char *word_eol[])
|
||||
if (sess->server->last_away_reason != reason)
|
||||
{
|
||||
if (sess->server->last_away_reason)
|
||||
free (sess->server->last_away_reason);
|
||||
g_free (sess->server->last_away_reason);
|
||||
|
||||
if (reason == word_eol[2])
|
||||
sess->server->last_away_reason = strdup (reason);
|
||||
sess->server->last_away_reason = g_strdup (reason);
|
||||
else
|
||||
sess->server->last_away_reason = reason;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user