1
0
mirror of https://github.com/moparisthebest/hexchat synced 2024-11-28 20:22:15 -05:00

Use Gio for random_line()

This commit is contained in:
TingPing 2014-08-26 22:27:40 -04:00
parent 29000e857f
commit 63a6f4a81f

View File

@ -78,40 +78,72 @@ notc_msg (struct session *sess)
static char * static char *
random_line (char *file_name) random_line (char *file_name)
{ {
FILE *fh; GFile *file;
char buf[512]; char *data, *p, *ret = NULL;
int lines, ran; int lines = 0, ran;
gsize len;
if (!file_name[0]) if (!file_name[0])
goto nofile; return g_strdup ("");
fh = hexchat_fopen_file (file_name, "r", 0); file = hexchat_open_gfile (file_name);
if (!fh)
if (!g_file_query_exists (file, NULL))
{ {
nofile:
/* reason is not a file, an actual reason! */ /* 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 */ /* count number of lines in file */
lines = 0; p = data;
while (fgets (buf, sizeof (buf), fh)) while (p != data + len)
lines++;
if (lines < 1)
goto nofile;
/* go down a random number */
rewind (fh);
ran = RAND_INT (lines);
do
{ {
fgets (buf, sizeof (buf), fh); if (*p == '\n')
lines--; lines++;
p++;
} }
while (lines > ran);
fclose (fh); if (!lines)
return strdup (buf); {
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 void
@ -121,7 +153,7 @@ server_sendpart (server * serv, char *channel, char *reason)
{ {
reason = random_line (prefs.hex_irc_part_reason); reason = random_line (prefs.hex_irc_part_reason);
serv->p_part (serv, channel, reason); serv->p_part (serv, channel, reason);
free (reason); g_free (reason);
} else } else
{ {
/* reason set by /quit, /close argument */ /* reason set by /quit, /close argument */
@ -139,7 +171,7 @@ server_sendquit (session * sess)
colrea = strdup (prefs.hex_irc_quit_reason); colrea = strdup (prefs.hex_irc_quit_reason);
check_special_chars (colrea, FALSE); check_special_chars (colrea, FALSE);
rea = random_line (colrea); rea = random_line (colrea);
free (colrea); g_free (colrea);
sess->server->p_quit (sess->server, rea); sess->server->p_quit (sess->server, rea);
free (rea); free (rea);
} else } 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 != reason)
{ {
if (sess->server->last_away_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]) if (reason == word_eol[2])
sess->server->last_away_reason = strdup (reason); sess->server->last_away_reason = g_strdup (reason);
else else
sess->server->last_away_reason = reason; sess->server->last_away_reason = reason;
} }