api: add function string_eval_path_home()

v2.8-utf8proc
Sébastien Helleu 2015-06-24 07:54:42 +02:00
parent 6668b9869b
commit 2bd2d74a07
23 changed files with 676 additions and 8 deletions

View File

@ -21,6 +21,7 @@ https://weechat.org/files/releasenotes/ReleaseNotes-devel.html[release notes]
* core: allow pointer as list name in evaluation of hdata (closes #450)
* core: add signal "signal_sighup"
* api: add function string_eval_path_home()
* logger: evaluate content of option logger.file.path (issue #388)
* relay: display value of HTTP header "X-Real-IP" for websocket connections
(closes #440)

View File

@ -404,7 +404,7 @@ Liste der Skript API Funktionen:
charset_set, iconv_to_internal, iconv_from_internal, gettext, ngettext, +
strlen_screen, string_match, string_has_highlight, string_has_highlight_regex,
string_mask_to_regex, string_remove_color, string_is_command_char,
string_input_for_buffer, string_eval_expression
string_input_for_buffer, string_eval_expression, string_eval_path_home
| Verzeichnisse |
mkdir_home, mkdir, mkdir_parents
| sortierte Listen |

View File

@ -917,6 +917,64 @@ free (str);
[NOTE]
This function is not available in scripting API.
==== string_eval_path_home
_WeeChat ≥ 1.3._
Evaluate a path in 3 steps:
. replace leading `%h` by WeeChat home directory,
. replace leading `~` by user home directory (call to
<<_string_expand_home,weechat_string_expand_home>>),
. evaluate variables
(see <<_string_eval_expression,weechat_string_eval_expression>>).
Prototype:
[source,C]
----
char *weechat_string_eval_path_home (const char *path,
struct t_hashtable *pointers,
struct t_hashtable *extra_vars,
struct t_hashtable *options);
----
Arguments:
* 'path': path
* 'pointers': hashtable for call to function
<<_string_eval_expression,weechat_string_eval_expression>>
* 'extra_vars': hashtable for call to function
<<_string_eval_expression,weechat_string_eval_expression>>
* 'options': hashtable for call to function
<<_string_eval_expression,weechat_string_eval_expression>>
Return value:
* evaluated path (must be freed by calling "free" after use)
C example:
[source,C]
----
char *str = weechat_string_expand_home ("%h/test/file.txt");
/* result: "/home/xxx/.weechat/test/file.txt" */
/* ... */
free (str);
----
Script (Python):
[source,python]
----
# prototype
path = weechat.string_eval_path_home(path, pointers, extra_vars, options)
# example
path = weechat.string_eval_path_home("%h/test/file.txt", "", "", "")
# path == "/home/xxx/.weechat/test/file.txt"
----
==== string_remove_quotes
Remove quotes at beginning and end of string (ignore spaces if there are before

View File

@ -392,7 +392,7 @@ List of functions in script API:
charset_set, iconv_to_internal, iconv_from_internal, gettext, ngettext, +
strlen_screen, string_match, string_has_highlight, string_has_highlight_regex,
string_mask_to_regex, string_remove_color, string_is_command_char,
string_input_for_buffer, string_eval_expression
string_input_for_buffer, string_eval_expression, string_eval_path_home
| directories |
mkdir_home, mkdir, mkdir_parents
| sorted lists |

View File

@ -927,6 +927,64 @@ free (str);
[NOTE]
Cette fonction n'est pas disponible dans l'API script.
==== string_eval_path_home
_WeeChat ≥ 1.3._
Évaluer un chemin en 3 étapes :
. remplacer le `%h` du début par le répertoire maison de WeeChat,
. remplacer le `~` du début par le répertoire maison de l'utilisateur (appel à
<<_string_expand_home,weechat_string_expand_home>>),
. évaluer les variables
(voir <<_string_eval_expression,weechat_string_eval_expression>>).
Prototype :
[source,C]
----
char *weechat_string_eval_path_home (const char *path,
struct t_hashtable *pointers,
struct t_hashtable *extra_vars,
struct t_hashtable *options);
----
Paramètres :
* 'path' : chemin
* 'pointers' : table de hachage pour l'appel à la fonction
<<_string_eval_expression,weechat_string_eval_expression>>
* 'extra_vars' : table de hachage pour l'appel à la fonction
<<_string_eval_expression,weechat_string_eval_expression>>
* 'options' : table de hachage pour l'appel à la fonction
<<_string_eval_expression,weechat_string_eval_expression>>
Valeur de retour :
* chemin évalué (doit être supprimé par un appel à "free" après utilisation)
Exemple en C :
[source,C]
----
char *str = weechat_string_expand_home ("%h/test/file.txt");
/* result: "/home/xxx/.weechat/test/file.txt" */
/* ... */
free (str);
----
Script (Python):
[source,python]
----
# prototype
path = weechat.string_eval_path_home(path, pointers, extra_vars, options)
# exemple
path = weechat.string_eval_path_home("%h/test/file.txt", "", "", "")
# path == "/home/xxx/.weechat/test/file.txt"
----
==== string_remove_quotes
Supprimer les apostrophes/guillemets au début et à la fin d'une chaîne (les

View File

@ -402,7 +402,7 @@ Liste des fonctions de l'API script :
charset_set, iconv_to_internal, iconv_from_internal, gettext, ngettext, +
strlen_screen, string_match, string_has_highlight, string_has_highlight_regex,
string_mask_to_regex, string_remove_color, string_is_command_char,
string_input_for_buffer, string_eval_expression
string_input_for_buffer, string_eval_expression, string_eval_path_home
| répertoires |
mkdir_home, mkdir, mkdir_parents
| listes triées |

View File

@ -955,6 +955,67 @@ free (str);
[NOTE]
Questa funzione non è disponibile nelle API per lo scripting.
==== string_eval_path_home
_WeeChat ≥ 1.3._
// TRANSLATION MISSING
Evaluate a path in 3 steps:
. replace leading `%h` by WeeChat home directory,
. replace leading `~` by user home directory (call to
<<_string_expand_home,weechat_string_expand_home>>),
. evaluate variables
(see <<_string_eval_expression,weechat_string_eval_expression>>).
Prototipo:
[source,C]
----
char *weechat_string_eval_path_home (const char *path,
struct t_hashtable *pointers,
struct t_hashtable *extra_vars,
struct t_hashtable *options);
----
// TRANSLATION MISSING
Argomenti:
* 'path': path
* 'pointers': hashtable for call to function
<<_string_eval_expression,weechat_string_eval_expression>>
* 'extra_vars': hashtable for call to function
<<_string_eval_expression,weechat_string_eval_expression>>
* 'options': hashtable for call to function
<<_string_eval_expression,weechat_string_eval_expression>>
// TRANSLATION MISSING
Valore restituito:
* evaluated path (must be freed by calling "free" after use)
Esempio in C:
[source,C]
----
char *str = weechat_string_expand_home ("%h/test/file.txt");
/* result: "/home/xxx/.weechat/test/file.txt" */
/* ... */
free (str);
----
Script (Python):
[source,python]
----
# prototipo
path = weechat.string_eval_path_home(path, pointers, extra_vars, options)
# esempio
path = weechat.string_eval_path_home("%h/test/file.txt", "", "", "")
# path == "/home/xxx/.weechat/test/file.txt"
----
==== string_remove_quotes
Rimuove le virgolette all'inizio e alla fine della stringa (ignora gli

View File

@ -406,7 +406,7 @@ Elenco di funzioni nelle API per gli script:
charset_set, iconv_to_internal, iconv_from_internal, gettext, ngettext, +
strlen_screen, string_match, string_has_highlight, string_has_highlight_regex,
string_mask_to_regex, string_remove_color, string_is_command_char,
string_input_for_buffer, string_eval_expression
string_input_for_buffer, string_eval_expression, string_eval_path_home
| directory |
mkdir_home, mkdir, mkdir_parents
| liste ordinate |

View File

@ -921,6 +921,67 @@ free (str);
[NOTE]
スクリプト API ではこの関数を利用できません。
==== string_eval_path_home
_WeeChat バージョン 0.3.3 以上で利用可。_
// TRANSLATION MISSING
Evaluate a path in 3 steps:
. replace leading `%h` by WeeChat home directory,
. replace leading `~` by user home directory (call to
<<_string_expand_home,weechat_string_expand_home>>),
. evaluate variables
(see <<_string_eval_expression,weechat_string_eval_expression>>).
プロトタイプ:
[source,C]
----
char *weechat_string_eval_path_home (const char *path,
struct t_hashtable *pointers,
struct t_hashtable *extra_vars,
struct t_hashtable *options);
----
// TRANSLATION MISSING
引数:
* 'path': path
* 'pointers': hashtable for call to function
<<_string_eval_expression,weechat_string_eval_expression>>
* 'extra_vars': hashtable for call to function
<<_string_eval_expression,weechat_string_eval_expression>>
* 'options': hashtable for call to function
<<_string_eval_expression,weechat_string_eval_expression>>
// TRANSLATION MISSING
戻り値:
* evaluated path (must be freed by calling "free" after use)
C 言語での使用例:
[source,C]
----
char *str = weechat_string_expand_home ("%h/test/file.txt");
/* result: "/home/xxx/.weechat/test/file.txt" */
/* ... */
free (str);
----
スクリプト (Python) での使用例:
[source,python]
----
# プロトタイプ
path = weechat.string_eval_path_home(path, pointers, extra_vars, options)
# 例
path = weechat.string_eval_path_home("%h/test/file.txt", "", "", "")
# path == "/home/xxx/.weechat/test/file.txt"
----
==== string_remove_quotes
文字列の最初と最後から引用符号を削除

View File

@ -396,7 +396,7 @@ API に含まれる関数の詳しい情報は
charset_set, iconv_to_internal, iconv_from_internal, gettext, ngettext, +
strlen_screen, string_match, string_has_highlight, string_has_highlight_regex,
string_mask_to_regex, string_remove_color, string_is_command_char,
string_input_for_buffer, string_eval_expression
string_input_for_buffer, string_eval_expression, string_eval_path_home
| ディレクトリ操作 |
mkdir_home, mkdir, mkdir_parents
| ソート済みリスト |

View File

@ -395,7 +395,7 @@ Lista funkcji w API skryptów:
charset_set, iconv_to_internal, iconv_from_internal, gettext, ngettext, +
strlen_screen, string_match, string_has_highlight, string_has_highlight_regex,
string_mask_to_regex, string_remove_color, string_is_command_char,
string_input_for_buffer, string_eval_expression
string_input_for_buffer, string_eval_expression, string_eval_path_home
| katalogi |
mkdir_home, mkdir, mkdir_parents
| przechowywane listy |

View File

@ -48,6 +48,7 @@
#include "weechat.h"
#include "wee-string.h"
#include "wee-config.h"
#include "wee-eval.h"
#include "wee-hashtable.h"
#include "wee-utf8.h"
#include "../gui/gui-color.h"
@ -499,6 +500,63 @@ string_expand_home (const char *path)
return str;
}
/*
* Evaluate a path by replacing (in this order):
* 1. "%h" (at beginning of string) by WeeChat home directory.
* 2. "~" by user home directory (call to string_expand_home)
* 3. evaluated variables (see /help eval)
*
* Returns the evaluated path, NULL if error.
*
* Note: result must be freed after use.
*/
char *
string_eval_path_home (const char *path,
struct t_hashtable *pointers,
struct t_hashtable *extra_vars,
struct t_hashtable *options)
{
char *path1, *path2, *path3;
int length;
if (!path)
return NULL;
path1 = NULL;
path2 = NULL;
path3 = NULL;
/* replace "%h" by Weechat home */
if (strncmp (path, "%h", 2) == 0)
{
length = strlen (weechat_home) + strlen (path + 2) + 1;
path1 = malloc (length);
if (path1)
snprintf (path1, length, "%s%s", weechat_home, path + 2);
}
else
path1 = strdup (path);
if (!path1)
goto end;
/* replace "~" by user home */
path2 = string_expand_home (path1);
if (!path2)
goto end;
/* evaluate content of path */
path3 = eval_expression (path2, pointers, extra_vars, options);
end:
if (path1)
free (path1);
if (path2)
free (path2);
return path3;
}
/*
* Removes quotes at beginning/end of string (ignores spaces if there are before
* first quote or after last quote).

View File

@ -44,6 +44,10 @@ extern int string_match (const char *string, const char *mask,
extern char *string_replace (const char *string, const char *search,
const char *replace);
extern char *string_expand_home (const char *path);
extern char *string_eval_path_home (const char *path,
struct t_hashtable *pointers,
struct t_hashtable *extra_vars,
struct t_hashtable *options);
extern char *string_remove_quotes (const char *string, const char *quotes);
extern char *string_strip (const char *string, int left, int right,
const char *chars);

View File

@ -479,6 +479,49 @@ weechat_guile_api_string_eval_expression (SCM expr, SCM pointers,
API_RETURN_STRING_FREE(result);
}
SCM
weechat_guile_api_string_eval_path_home (SCM path, SCM pointers,
SCM extra_vars, SCM options)
{
char *result;
SCM return_value;
struct t_hashtable *c_pointers, *c_extra_vars, *c_options;
API_INIT_FUNC(1, "string_eval_path_home", API_RETURN_EMPTY);
if (!scm_is_string (path) || !scm_list_p (pointers)
|| !scm_list_p (extra_vars) || !scm_list_p (options))
API_WRONG_ARGS(API_RETURN_EMPTY);
c_pointers = weechat_guile_alist_to_hashtable (
pointers,
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_POINTER);
c_extra_vars = weechat_guile_alist_to_hashtable (
extra_vars,
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING);
c_options = weechat_guile_alist_to_hashtable (
options,
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING);
result = weechat_string_eval_path_home (API_SCM_TO_STRING(path),
c_pointers, c_extra_vars,
c_options);
if (c_pointers)
weechat_hashtable_free (c_pointers);
if (c_extra_vars)
weechat_hashtable_free (c_extra_vars);
if (c_options)
weechat_hashtable_free (c_options);
API_RETURN_STRING_FREE(result);
}
SCM
weechat_guile_api_mkdir_home (SCM directory, SCM mode)
{
@ -4660,6 +4703,7 @@ weechat_guile_api_module_init (void *data)
API_DEF_FUNC(string_is_command_char, 1);
API_DEF_FUNC(string_input_for_buffer, 1);
API_DEF_FUNC(string_eval_expression, 4);
API_DEF_FUNC(string_eval_path_home, 4);
API_DEF_FUNC(mkdir_home, 2);
API_DEF_FUNC(mkdir, 2);
API_DEF_FUNC(mkdir_parents, 2);

View File

@ -421,6 +421,43 @@ API_FUNC(string_eval_expression)
API_RETURN_STRING_FREE(result);
}
API_FUNC(string_eval_path_home)
{
struct t_hashtable *pointers, *extra_vars, *options;
char *result;
API_INIT_FUNC(1, "string_eval_path_home", "shhh", API_RETURN_EMPTY);
v8::String::Utf8Value path(args[0]);
pointers = weechat_js_object_to_hashtable (
args[1]->ToObject(),
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_POINTER);
extra_vars = weechat_js_object_to_hashtable (
args[2]->ToObject(),
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING);
options = weechat_js_object_to_hashtable (
args[3]->ToObject(),
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING);
result = weechat_string_eval_path_home (*path, pointers, extra_vars,
options);
if (pointers)
weechat_hashtable_free (pointers);
if (extra_vars)
weechat_hashtable_free (extra_vars);
if (options)
weechat_hashtable_free (options);
API_RETURN_STRING_FREE(result);
}
API_FUNC(mkdir_home)
{
int mode;
@ -4681,6 +4718,7 @@ WeechatJsV8::loadLibs()
API_DEF_FUNC(string_is_command_char);
API_DEF_FUNC(string_input_for_buffer);
API_DEF_FUNC(string_eval_expression);
API_DEF_FUNC(string_eval_path_home);
API_DEF_FUNC(mkdir_home);
API_DEF_FUNC(mkdir);
API_DEF_FUNC(mkdir_parents);

View File

@ -441,6 +441,43 @@ API_FUNC(string_eval_expression)
API_RETURN_STRING_FREE(result);
}
API_FUNC(string_eval_path_home)
{
const char *path;
struct t_hashtable *pointers, *extra_vars, *options;
char *result;
API_INIT_FUNC(1, "string_eval_path_home", API_RETURN_EMPTY);
if (lua_gettop (L) < 4)
API_WRONG_ARGS(API_RETURN_EMPTY);
path = lua_tostring (L, -4);
pointers = weechat_lua_tohashtable (L, -3,
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_POINTER);
extra_vars = weechat_lua_tohashtable (L, -2,
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING);
options = weechat_lua_tohashtable (L, -1,
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING);
result = weechat_string_eval_path_home (path, pointers, extra_vars,
options);
if (pointers)
weechat_hashtable_free (pointers);
if (extra_vars)
weechat_hashtable_free (extra_vars);
if (options)
weechat_hashtable_free (options);
API_RETURN_STRING_FREE(result);
}
API_FUNC(mkdir_home)
{
const char *directory;
@ -4957,6 +4994,7 @@ const struct luaL_Reg weechat_lua_api_funcs[] = {
API_DEF_FUNC(string_is_command_char),
API_DEF_FUNC(string_input_for_buffer),
API_DEF_FUNC(string_eval_expression),
API_DEF_FUNC(string_eval_path_home),
API_DEF_FUNC(mkdir_home),
API_DEF_FUNC(mkdir),
API_DEF_FUNC(mkdir_parents),

View File

@ -437,6 +437,43 @@ API_FUNC(string_eval_expression)
API_RETURN_STRING_FREE(result);
}
API_FUNC(string_eval_path_home)
{
char *path, *result;
struct t_hashtable *pointers, *extra_vars, *options;
dXSARGS;
API_INIT_FUNC(1, "string_eval_path_home", API_RETURN_EMPTY);
if (items < 4)
API_WRONG_ARGS(API_RETURN_EMPTY);
path = SvPV_nolen (ST (0));
pointers = weechat_perl_hash_to_hashtable (ST (1),
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_POINTER);
extra_vars = weechat_perl_hash_to_hashtable (ST (2),
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING);
options = weechat_perl_hash_to_hashtable (ST (3),
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING);
result = weechat_string_eval_path_home (path, pointers, extra_vars,
options);
if (pointers)
weechat_hashtable_free (pointers);
if (extra_vars)
weechat_hashtable_free (extra_vars);
if (options)
weechat_hashtable_free (options);
API_RETURN_STRING_FREE(result);
}
API_FUNC(mkdir_home)
{
dXSARGS;
@ -4899,6 +4936,7 @@ weechat_perl_api_init (pTHX)
API_DEF_FUNC(string_is_command_char);
API_DEF_FUNC(string_input_for_buffer);
API_DEF_FUNC(string_eval_expression);
API_DEF_FUNC(string_eval_path_home);
API_DEF_FUNC(mkdir_home);
API_DEF_FUNC(mkdir);
API_DEF_FUNC(mkdir_parents);

View File

@ -614,6 +614,7 @@ plugin_load (const char *filename, int init_plugin, int argc, char **argv)
new_plugin->string_match = &string_match;
new_plugin->string_replace = &string_replace;
new_plugin->string_expand_home = &string_expand_home;
new_plugin->string_eval_path_home = &string_eval_path_home;
new_plugin->string_remove_quotes = &string_remove_quotes;
new_plugin->string_strip = &string_strip;
new_plugin->string_convert_escaped_chars = &string_convert_escaped_chars;

View File

@ -419,6 +419,45 @@ API_FUNC(string_eval_expression)
API_RETURN_STRING_FREE(result);
}
API_FUNC(string_eval_path_home)
{
char *path, *result;
struct t_hashtable *pointers, *extra_vars, *options;
PyObject *dict, *dict2, *dict3, *return_value;
API_INIT_FUNC(1, "string_eval_path_home", API_RETURN_EMPTY);
path = NULL;
pointers = NULL;
extra_vars = NULL;
options = NULL;
if (!PyArg_ParseTuple (args, "sOOO", &path, &dict, &dict2, &dict3))
API_WRONG_ARGS(API_RETURN_EMPTY);
pointers = weechat_python_dict_to_hashtable (dict,
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_POINTER);
extra_vars = weechat_python_dict_to_hashtable (dict2,
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING);
options = weechat_python_dict_to_hashtable (dict3,
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING);
result = weechat_string_eval_path_home (path, pointers, extra_vars,
options);
if (pointers)
weechat_hashtable_free (pointers);
if (extra_vars)
weechat_hashtable_free (extra_vars);
if (options)
weechat_hashtable_free (options);
API_RETURN_STRING_FREE(result);
}
API_FUNC(mkdir_home)
{
char *directory;
@ -4880,6 +4919,7 @@ PyMethodDef weechat_python_funcs[] =
API_DEF_FUNC(string_is_command_char),
API_DEF_FUNC(string_input_for_buffer),
API_DEF_FUNC(string_eval_expression),
API_DEF_FUNC(string_eval_path_home),
API_DEF_FUNC(mkdir_home),
API_DEF_FUNC(mkdir),
API_DEF_FUNC(mkdir_parents),

View File

@ -507,6 +507,55 @@ weechat_ruby_api_string_eval_expression (VALUE class, VALUE expr,
API_RETURN_STRING_FREE(result);
}
static VALUE
weechat_ruby_api_string_eval_path_home (VALUE class, VALUE path,
VALUE pointers, VALUE extra_vars,
VALUE options)
{
char *c_path, *result;
struct t_hashtable *c_pointers, *c_extra_vars, *c_options;
VALUE return_value;
API_INIT_FUNC(1, "string_eval_path_home", API_RETURN_EMPTY);
if (NIL_P (path) || NIL_P (pointers) || NIL_P (extra_vars)
|| NIL_P (options))
API_WRONG_ARGS(API_RETURN_EMPTY);
Check_Type (path, T_STRING);
Check_Type (pointers, T_HASH);
Check_Type (extra_vars, T_HASH);
Check_Type (options, T_HASH);
c_path = StringValuePtr (path);
c_pointers = weechat_ruby_hash_to_hashtable (
pointers,
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_POINTER);
c_extra_vars = weechat_ruby_hash_to_hashtable (
extra_vars,
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING);
c_options = weechat_ruby_hash_to_hashtable (
options,
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING);
result = weechat_string_eval_path_home (c_path, c_pointers, c_extra_vars,
c_options);
if (c_pointers)
weechat_hashtable_free (c_pointers);
if (c_extra_vars)
weechat_hashtable_free (c_extra_vars);
if (c_options)
weechat_hashtable_free (c_options);
API_RETURN_STRING_FREE(result);
}
static VALUE
weechat_ruby_api_mkdir_home (VALUE class, VALUE directory, VALUE mode)
{
@ -6005,6 +6054,7 @@ weechat_ruby_api_init (VALUE ruby_mWeechat)
API_DEF_FUNC(string_is_command_char, 1);
API_DEF_FUNC(string_input_for_buffer, 1);
API_DEF_FUNC(string_eval_expression, 4);
API_DEF_FUNC(string_eval_path_home, 4);
API_DEF_FUNC(mkdir_home, 2);
API_DEF_FUNC(mkdir, 2);
API_DEF_FUNC(mkdir_parents, 2);

View File

@ -574,6 +574,47 @@ API_FUNC(string_eval_expression)
API_RETURN_STRING_FREE(result);
}
API_FUNC(string_eval_path_home)
{
Tcl_Obj *objp;
char *path, *result;
struct t_hashtable *pointers, *extra_vars, *options;
int i;
API_INIT_FUNC(1, "string_eval_path_home", API_RETURN_EMPTY);
if (objc < 5)
API_WRONG_ARGS(API_RETURN_EMPTY);
path = Tcl_GetStringFromObj (objv[1], &i);
pointers = weechat_tcl_dict_to_hashtable (
interp, objv[2],
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_POINTER);
extra_vars = weechat_tcl_dict_to_hashtable (
interp, objv[3],
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING);
options = weechat_tcl_dict_to_hashtable (
interp, objv[4],
WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING);
result = weechat_string_eval_path_home (path, pointers, extra_vars,
options);
if (pointers)
weechat_hashtable_free (pointers);
if (extra_vars)
weechat_hashtable_free (extra_vars);
if (options)
weechat_hashtable_free (options);
API_RETURN_STRING_FREE(result);
}
API_FUNC(mkdir_home)
{
Tcl_Obj *objp;
@ -5356,6 +5397,7 @@ void weechat_tcl_api_init (Tcl_Interp *interp)
API_DEF_FUNC(string_is_command_char);
API_DEF_FUNC(string_input_for_buffer);
API_DEF_FUNC(string_eval_expression);
API_DEF_FUNC(string_eval_path_home);
API_DEF_FUNC(mkdir_home);
API_DEF_FUNC(mkdir);
API_DEF_FUNC(mkdir_parents);

View File

@ -57,7 +57,7 @@ struct timeval;
* please change the date with current one; for a second change at same
* date, increment the 01, otherwise please keep 01.
*/
#define WEECHAT_PLUGIN_API_VERSION "20150114-01"
#define WEECHAT_PLUGIN_API_VERSION "20150624-01"
/* macros for defining plugin infos */
#define WEECHAT_PLUGIN_NAME(__name) \
@ -276,6 +276,10 @@ struct t_weechat_plugin
char *(*string_replace) (const char *string, const char *search,
const char *replace);
char *(*string_expand_home) (const char *path);
char *(*string_eval_path_home) (const char *path,
struct t_hashtable *pointers,
struct t_hashtable *extra_vars,
struct t_hashtable *options);
char *(*string_remove_quotes) (const char *string, const char *quotes);
char *(*string_strip) (const char *string, int left, int right,
const char *chars);
@ -1040,6 +1044,10 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin);
(weechat_plugin->string_replace)(__string, __search, __replace)
#define weechat_string_expand_home(__path) \
(weechat_plugin->string_expand_home)(__path)
#define weechat_string_eval_path_home(__path, __pointers, \
__extra_vars, __options) \
(weechat_plugin->string_eval_path_home)(__path, __pointers, \
__extra_vars, __options)
#define weechat_string_remove_quotes(__string, __quotes) \
(weechat_plugin->string_remove_quotes)(__string, __quotes)
#define weechat_string_strip(__string, __left, __right, __chars) \

View File

@ -23,12 +23,18 @@
extern "C"
{
#ifndef HAVE_CONFIG_H
#define HAVE_CONFIG_H
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <regex.h>
#include "tests/tests.h"
#include "src/core/weechat.h"
#include "src/core/wee-string.h"
#include "src/core/wee-hashtable.h"
#include "src/plugins/plugin.h"
}
#define ONE_KB 1000ULL
@ -331,13 +337,75 @@ TEST(String, ExpandHome)
home = getenv ("HOME");
length_home = strlen (home);
result = string_expand_home("~/abc.txt");
POINTERS_EQUAL(NULL, string_expand_home (NULL));
result = string_expand_home ("~/abc.txt");
CHECK(strncmp (result, home, length_home) == 0);
LONGS_EQUAL(length_home + 8, strlen (result));
STRCMP_EQUAL(result + length_home, "/abc.txt");
free (result);
}
/*
* Tests functions:
* string_eval_path_home
*/
TEST(String, EvalPathHome)
{
char *home, *result;
int length_home, length_weechat_home;
struct t_hashtable *extra_vars;
home = getenv ("HOME");
length_home = strlen (home);
length_weechat_home = strlen (weechat_home);
POINTERS_EQUAL(NULL, string_eval_path_home (NULL, NULL, NULL, NULL));
result = string_eval_path_home ("/tmp/test", NULL, NULL, NULL);
STRCMP_EQUAL(result, "/tmp/test");
free (result);
result = string_eval_path_home ("~/test", NULL, NULL, NULL);
CHECK(strncmp (result, home, length_home) == 0);
LONGS_EQUAL(length_home + 5, strlen (result));
STRCMP_EQUAL(result + length_home, "/test");
free (result);
result = string_eval_path_home ("%h/test", NULL, NULL, NULL);
CHECK(strncmp (result, weechat_home, length_weechat_home) == 0);
LONGS_EQUAL(length_weechat_home + 5, strlen (result));
STRCMP_EQUAL(result + length_weechat_home, "/test");
free (result);
setenv ("WEECHAT_TEST_PATH", "path1", 1);
result = string_eval_path_home ("%h/${env:WEECHAT_TEST_PATH}/path2",
NULL, NULL, NULL);
CHECK(strncmp (result, weechat_home, length_weechat_home) == 0);
LONGS_EQUAL(length_weechat_home + 12, strlen (result));
STRCMP_EQUAL(result + length_weechat_home, "/path1/path2");
free (result);
extra_vars = hashtable_new (32,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING,
NULL, NULL);
CHECK(extra_vars);
hashtable_set (extra_vars, "path2", "value");
result = string_eval_path_home ("%h/${env:WEECHAT_TEST_PATH}/${path2}",
NULL, extra_vars, NULL);
CHECK(strncmp (result, weechat_home, length_weechat_home) == 0);
LONGS_EQUAL(length_weechat_home + 12, strlen (result));
STRCMP_EQUAL(result + length_weechat_home, "/path1/value");
free (result);
hashtable_free (extra_vars);
}
/*
* Tests functions:
* string_remove_quotes