mirror of
https://github.com/MidnightCommander/mc
synced 2025-03-02 22:21:39 +03:00
(get_random_hint): move to src/filemanager/midnight.c.
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
parent
1e302a3c63
commit
6f5fc5d528
@ -48,7 +48,6 @@
|
||||
#include <stdlib.h>
|
||||
#include <pwd.h>
|
||||
#include <grp.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "lib/global.h"
|
||||
|
||||
@ -1328,75 +1327,6 @@ user_file_menu_cmd (void)
|
||||
(void) user_menu_cmd (NULL, NULL, -1);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
/**
|
||||
* Return a random hint. If force is not 0, ignore the timeout.
|
||||
*/
|
||||
|
||||
char *
|
||||
get_random_hint (gboolean force)
|
||||
{
|
||||
char *data, *result = NULL, *eop;
|
||||
size_t len, start;
|
||||
static int last_sec;
|
||||
static struct timeval tv;
|
||||
GIConv conv;
|
||||
|
||||
/* Do not change hints more often than one minute */
|
||||
gettimeofday (&tv, NULL);
|
||||
if (!force && !(tv.tv_sec > last_sec + 60))
|
||||
return g_strdup ("");
|
||||
last_sec = tv.tv_sec;
|
||||
|
||||
data = load_mc_home_file (mc_global.share_data_dir, MC_HINT, NULL, &len);
|
||||
if (data == NULL)
|
||||
return NULL;
|
||||
|
||||
/* get a random entry */
|
||||
srand (tv.tv_sec);
|
||||
start = ((size_t) rand ()) % (len - 1);
|
||||
|
||||
/* Search the start of paragraph */
|
||||
for (; start != 0; start--)
|
||||
if (data[start] == '\n' && data[start + 1] == '\n')
|
||||
{
|
||||
start += 2;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Search the end of paragraph */
|
||||
for (eop = data + start; *eop != '\0'; eop++)
|
||||
{
|
||||
if (*eop == '\n' && *(eop + 1) == '\n')
|
||||
{
|
||||
*eop = '\0';
|
||||
break;
|
||||
}
|
||||
if (*eop == '\n')
|
||||
*eop = ' ';
|
||||
}
|
||||
|
||||
/* hint files are stored in utf-8 */
|
||||
/* try convert hint file from utf-8 to terminal encoding */
|
||||
conv = str_crt_conv_from ("UTF-8");
|
||||
if (conv != INVALID_CONV)
|
||||
{
|
||||
GString *buffer;
|
||||
|
||||
buffer = g_string_sized_new (len - start);
|
||||
if (str_convert (conv, &data[start], buffer) != ESTR_FAILURE)
|
||||
result = g_string_free (buffer, FALSE);
|
||||
else
|
||||
g_string_free (buffer, TRUE);
|
||||
str_close_conv (conv);
|
||||
}
|
||||
else
|
||||
result = g_strndup (data + start, len - start);
|
||||
|
||||
g_free (data);
|
||||
return result;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
|
||||
#ifdef ENABLE_VFS_FTP
|
||||
|
@ -83,7 +83,6 @@ void swap_cmd (void);
|
||||
void view_other_cmd (void);
|
||||
void quick_cd_cmd (void);
|
||||
void save_setup_cmd (void);
|
||||
char *get_random_hint (gboolean force);
|
||||
void user_file_menu_cmd (void);
|
||||
void info_cmd (void);
|
||||
void listing_cmd (void);
|
||||
|
@ -39,6 +39,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/time.h> /* gettimeofday() */
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/wait.h>
|
||||
@ -1655,6 +1656,76 @@ midnight_set_buttonbar (WButtonBar * b)
|
||||
buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), main_map, NULL);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
/**
|
||||
* Return a random hint. If force is TRUE, ignore the timeout.
|
||||
*/
|
||||
|
||||
char *
|
||||
get_random_hint (gboolean force)
|
||||
{
|
||||
char *data, *result = NULL, *eop;
|
||||
size_t len, start;
|
||||
static int last_sec;
|
||||
static struct timeval tv;
|
||||
GIConv conv;
|
||||
|
||||
/* Do not change hints more often than one minute */
|
||||
gettimeofday (&tv, NULL);
|
||||
if (!force && !(tv.tv_sec > last_sec + 60))
|
||||
return g_strdup ("");
|
||||
last_sec = tv.tv_sec;
|
||||
|
||||
data = load_mc_home_file (mc_global.share_data_dir, MC_HINT, NULL, &len);
|
||||
if (data == NULL)
|
||||
return NULL;
|
||||
|
||||
/* get a random entry */
|
||||
srand (tv.tv_sec);
|
||||
start = ((size_t) rand ()) % (len - 1);
|
||||
|
||||
/* Search the start of paragraph */
|
||||
for (; start != 0; start--)
|
||||
if (data[start] == '\n' && data[start + 1] == '\n')
|
||||
{
|
||||
start += 2;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Search the end of paragraph */
|
||||
for (eop = data + start; *eop != '\0'; eop++)
|
||||
{
|
||||
if (*eop == '\n' && *(eop + 1) == '\n')
|
||||
{
|
||||
*eop = '\0';
|
||||
break;
|
||||
}
|
||||
if (*eop == '\n')
|
||||
*eop = ' ';
|
||||
}
|
||||
|
||||
/* hint files are stored in utf-8 */
|
||||
/* try convert hint file from utf-8 to terminal encoding */
|
||||
conv = str_crt_conv_from ("UTF-8");
|
||||
if (conv != INVALID_CONV)
|
||||
{
|
||||
GString *buffer;
|
||||
|
||||
buffer = g_string_sized_new (len - start);
|
||||
if (str_convert (conv, &data[start], buffer) != ESTR_FAILURE)
|
||||
result = g_string_free (buffer, FALSE);
|
||||
else
|
||||
g_string_free (buffer, TRUE);
|
||||
str_close_conv (conv);
|
||||
}
|
||||
else
|
||||
result = g_strndup (data + start, len - start);
|
||||
|
||||
g_free (data);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
/**
|
||||
* Load new hint and display it.
|
||||
|
@ -41,6 +41,7 @@ extern const char *mc_prompt;
|
||||
|
||||
void update_menu (void);
|
||||
void midnight_set_buttonbar (WButtonBar * b);
|
||||
char *get_random_hint (gboolean force);
|
||||
void load_hint (gboolean force);
|
||||
void change_panel (void);
|
||||
void save_cwds_stat (void);
|
||||
|
@ -23,11 +23,11 @@ endif
|
||||
EXTRA_DIST = hints/mc.hint
|
||||
|
||||
TESTS = \
|
||||
cmd__get_random_hint \
|
||||
do_cd_command \
|
||||
examine_cd \
|
||||
exec_get_export_variables_ext \
|
||||
filegui_is_wildcarded
|
||||
filegui_is_wildcarded \
|
||||
get_random_hint
|
||||
|
||||
check_PROGRAMS = $(TESTS)
|
||||
|
||||
@ -40,8 +40,8 @@ examine_cd_SOURCES = \
|
||||
exec_get_export_variables_ext_SOURCES = \
|
||||
exec_get_export_variables_ext.c
|
||||
|
||||
cmd__get_random_hint_SOURCES = \
|
||||
cmd__get_random_hint.c
|
||||
get_random_hint_SOURCES = \
|
||||
get_random_hint.c
|
||||
|
||||
filegui_is_wildcarded_SOURCES = \
|
||||
filegui_is_wildcarded.c
|
||||
|
@ -31,7 +31,7 @@
|
||||
#include "lib/strutil.h"
|
||||
#include "lib/util.h"
|
||||
|
||||
#include "src/filemanager/cmd.h"
|
||||
#include "src/filemanager/midnight.h"
|
||||
|
||||
|
||||
/* --------------------------------------------------------------------------------------------- */
|
||||
@ -178,7 +178,7 @@ main (void)
|
||||
|
||||
suite_add_tcase (s, tc_core);
|
||||
sr = srunner_create (s);
|
||||
srunner_set_log (sr, "cmd__get_random_hint.log");
|
||||
srunner_set_log (sr, "get_random_hint.log");
|
||||
srunner_run_all (sr, CK_ENV);
|
||||
number_failed = srunner_ntests_failed (sr);
|
||||
srunner_free (sr);
|
Loading…
x
Reference in New Issue
Block a user