Przeglądaj źródła

Add new squilu extensions

mingodad 7 lat temu
rodzic
commit
685169abd3
2 zmienionych plików z 210 dodań i 0 usunięć
  1. 88 0
      SquiLu-ext/sq_fuzzy_hash.cpp
  2. 122 0
      SquiLu-ext/sq_snowball.cpp

+ 88 - 0
SquiLu-ext/sq_fuzzy_hash.cpp

@@ -0,0 +1,88 @@
+#ifdef SQ_USE_FUZZY_HASH
+
+#include "squirrel.h"
+#include "sqstdblobimpl.h"
+
+#define MixInteger SQInteger
+
+#include "fuzzy.h"
+
+static SQRESULT sq_fuzzy_hash_buf0(HSQUIRRELVM v, SQBool isSmall) {
+	SQ_FUNC_VARS_NO_TOP(v);
+	SQ_GET_STRING(v, 2, buf);
+
+    SQChar *result = sq_getscratchpad(v, FUZZY_MAX_RESULT);
+    int rc = fuzzy_hash_buf((const unsigned char*)buf, buf_size, result, isSmall);
+    if(rc) sq_pushnull(v);
+    else sq_pushstring(v, result, -1);
+    return 1;
+}
+
+static SQRESULT sq_fuzzy_hash_buf(HSQUIRRELVM v) {
+    return sq_fuzzy_hash_buf0(v, SQFalse);
+}
+
+static SQRESULT sq_fuzzy_hash_buf_small(HSQUIRRELVM v) {
+    return sq_fuzzy_hash_buf0(v, SQTrue);
+}
+
+static SQRESULT sq_fuzzy_hash_filename(HSQUIRRELVM v) {
+	SQ_FUNC_VARS_NO_TOP(v);
+	SQ_GET_STRING(v, 2, fname);
+
+    SQChar *result = sq_getscratchpad(v, FUZZY_MAX_RESULT);
+    int rc = fuzzy_hash_filename(fname, result);
+    if(rc) sq_pushnull(v);
+    else sq_pushstring(v, result, -1);
+    return 1;
+}
+
+static SQRESULT sq_fuzzy_compare(HSQUIRRELVM v) {
+	SQ_FUNC_VARS_NO_TOP(v);
+	SQ_GET_STRING(v, 2, sig1);
+	SQ_GET_STRING(v, 3, sig2);
+
+    sq_pushinteger(v, fuzzy_compare(sig1, sig2));
+    return 1;
+}
+
+static SQRESULT sq_fuzzy_compare_small(HSQUIRRELVM v) {
+	SQ_FUNC_VARS_NO_TOP(v);
+	SQ_GET_STRING(v, 2, sig1);
+	SQ_GET_STRING(v, 3, sig2);
+
+    sq_pushinteger(v, fuzzy_compare_small(sig1, sig2));
+    return 1;
+}
+
+#define _DECL_FUNC(name,nparams,pmask) {_SC(#name), sq_##name,nparams,pmask}
+static SQRegFunction fuzzy_hash_obj_funcs[]={
+	_DECL_FUNC(fuzzy_hash_buf,2, _SC(".s")),
+	_DECL_FUNC(fuzzy_hash_buf_small,2, _SC(".s")),
+	_DECL_FUNC(fuzzy_hash_filename,2, _SC(".s")),
+	_DECL_FUNC(fuzzy_compare,3, _SC(".ss")),
+	_DECL_FUNC(fuzzy_compare_small,3, _SC(".ss")),
+	{0,0}
+};
+#undef _DECL_FUNC
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+/* This defines a function that opens up your library. */
+SQRESULT sqext_register_fuzzy_hash (HSQUIRRELVM sqvm) {
+    //add a namespace sqmix
+	sq_pushstring(sqvm,_SC("sqfuzzy_hash"),-1);
+	sq_newclass(sqvm,SQFalse);
+    sq_insert_reg_funcs(sqvm, fuzzy_hash_obj_funcs);
+	sq_newslot(sqvm,-3,SQTrue); //add sq_fossil table to the root table
+
+	return SQ_OK;
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+

+ 122 - 0
SquiLu-ext/sq_snowball.cpp

@@ -0,0 +1,122 @@
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef USE_SNOWBALL
+
+#include "squirrel.h"
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>  /* for malloc */
+#include <assert.h>  /* for a few sanity tests */
+
+SQ_OPT_STRING_STRLEN();
+
+#include "libstemmer.h"
+
+
+static const SQChar SQ_LIBNAME[] = _SC("Snowball");
+
+
+static const SQChar SNOWBALL_Tag[]   = _SC("sq_Snowball");
+#define GET_Snowball_INSTANCE() SQ_GET_INSTANCE(v, 1, sb_stemmer, SNOWBALL_Tag) \
+	if(self == NULL) return sq_throwerror(v, _SC("Snowball object already closed"));
+
+
+static SQRESULT Snowball_release_hook(SQUserPointer p, SQInteger size, void */*ep*/)
+{
+	sb_stemmer *self = (sb_stemmer*)p;
+	if(self) sb_stemmer_delete(self);
+	return 0;
+}
+
+static SQRESULT sq_Snowball_constructor(HSQUIRRELVM v)
+{
+	SQ_FUNC_VARS(v);
+	SQ_GET_STRING(v, 2, algorithm);
+	SQ_OPT_STRING(v, 3, charenc, NULL);
+
+	sb_stemmer *Snowball = sb_stemmer_new(algorithm, charenc);
+    if(!Snowball)
+        return sq_throwerror(v, _SC("Could'nt create an Snowball object."));
+
+    SQInteger rc = sq_setinstanceup(v, 1, Snowball);
+    sq_setreleasehook(v, 1, Snowball_release_hook);
+    return rc;
+}
+
+
+//	int  sb_stemmer_length(struct sb_stemmer * stemmer)
+static SQRESULT sq_Snowball_length(HSQUIRRELVM v){
+	SQ_FUNC_VARS_NO_TOP(v);
+	GET_Snowball_INSTANCE();
+
+	sq_pushinteger(v, sb_stemmer_length(self));
+	return 1;
+}
+
+//	const sb_symbol *   sb_stemmer_stem(struct sb_stemmer * stemmer, const sb_symbol * word, int size)
+static SQRESULT sq_Snowball_stem(HSQUIRRELVM v){
+	SQ_FUNC_VARS_NO_TOP(v);
+	GET_Snowball_INSTANCE();
+	SQ_GET_STRING(v, 2, str_word);
+
+	const sb_symbol *res =  sb_stemmer_stem(self,
+        (const sb_symbol*)str_word, str_word_size);
+	if(res) sq_pushstring(v, (const SQChar*)res, -1);
+	else sq_pushnull(v);
+	return 1;
+}
+
+//	const char ** sb_stemmer_list(void)
+static SQRESULT sq_Snowball_list(HSQUIRRELVM v){
+    const char ** list = sb_stemmer_list();
+    if (*list == 0) sq_pushnull(v);
+    else
+    {
+        SQInteger lcount = 0;
+        const char **ptr = list;
+        while(*(ptr++)) ++lcount;
+        ptr = list;
+        sq_newarray(v, lcount);
+        for(SQInteger i=0; i < lcount; ++i)
+        {
+            sq_pushstring(v, *(ptr++), -1);
+            sq_arrayset(v, -2, i);
+        }
+    }
+    return 1;
+}
+
+#define _DECL_SNOWBALL_FUNC(name,nparams,pmask) {_SC(#name),sq_Snowball_##name,nparams,pmask}
+static SQRegFunction Snowball_obj_funcs[]={
+
+	_DECL_SNOWBALL_FUNC(constructor, -2, _SC("xss")),
+	_DECL_SNOWBALL_FUNC(length, 1, _SC("x")),
+	_DECL_SNOWBALL_FUNC(stem, 2, _SC("xs")),
+	_DECL_SNOWBALL_FUNC(list, 1, _SC(".")),
+	{0,0}
+};
+#undef _DECL_SNOWBALL_FUNC
+
+
+/* This defines a function that opens up your library. */
+SQRESULT sqext_register_Snowball (HSQUIRRELVM v) {
+	//add a namespace Snowball
+	sq_pushstring(v, SQ_LIBNAME, -1);
+    sq_newclass(v,SQFalse);
+    sq_settypetag(v,-1,(SQUserPointer)SNOWBALL_Tag);
+
+	sq_insert_reg_funcs(v, Snowball_obj_funcs);
+
+	sq_newslot(v,-3,SQFalse); //add Snowball table to the root table
+
+	return SQ_OK;
+}
+
+#ifdef __cplusplus
+}
+
+#endif //USE_SNOWBALL
+
+#endif