sq_fuzzy_hash.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #ifdef SQ_USE_FUZZY_HASH
  2. #include "squirrel.h"
  3. #include "sqstdblobimpl.h"
  4. #define MixInteger SQInteger
  5. #include "fuzzy.h"
  6. static SQRESULT sq_fuzzy_hash_buf0(HSQUIRRELVM v, SQBool isSmall) {
  7. SQ_FUNC_VARS_NO_TOP(v);
  8. SQ_GET_STRING(v, 2, buf);
  9. SQChar *result = sq_getscratchpad(v, FUZZY_MAX_RESULT);
  10. int rc = fuzzy_hash_buf((const unsigned char*)buf, buf_size, result, isSmall);
  11. if(rc) sq_pushnull(v);
  12. else sq_pushstring(v, result, -1);
  13. return 1;
  14. }
  15. static SQRESULT sq_fuzzy_hash_buf(HSQUIRRELVM v) {
  16. return sq_fuzzy_hash_buf0(v, SQFalse);
  17. }
  18. static SQRESULT sq_fuzzy_hash_buf_small(HSQUIRRELVM v) {
  19. return sq_fuzzy_hash_buf0(v, SQTrue);
  20. }
  21. static SQRESULT sq_fuzzy_hash_filename(HSQUIRRELVM v) {
  22. SQ_FUNC_VARS_NO_TOP(v);
  23. SQ_GET_STRING(v, 2, fname);
  24. SQChar *result = sq_getscratchpad(v, FUZZY_MAX_RESULT);
  25. int rc = fuzzy_hash_filename(fname, result);
  26. if(rc) sq_pushnull(v);
  27. else sq_pushstring(v, result, -1);
  28. return 1;
  29. }
  30. static SQRESULT sq_fuzzy_compare(HSQUIRRELVM v) {
  31. SQ_FUNC_VARS_NO_TOP(v);
  32. SQ_GET_STRING(v, 2, sig1);
  33. SQ_GET_STRING(v, 3, sig2);
  34. sq_pushinteger(v, fuzzy_compare(sig1, sig2));
  35. return 1;
  36. }
  37. static SQRESULT sq_fuzzy_compare_small(HSQUIRRELVM v) {
  38. SQ_FUNC_VARS_NO_TOP(v);
  39. SQ_GET_STRING(v, 2, sig1);
  40. SQ_GET_STRING(v, 3, sig2);
  41. sq_pushinteger(v, fuzzy_compare_small(sig1, sig2));
  42. return 1;
  43. }
  44. #define _DECL_FUNC(name,nparams,pmask) {_SC(#name), sq_##name,nparams,pmask}
  45. static SQRegFunction fuzzy_hash_obj_funcs[]={
  46. _DECL_FUNC(fuzzy_hash_buf,2, _SC(".s")),
  47. _DECL_FUNC(fuzzy_hash_buf_small,2, _SC(".s")),
  48. _DECL_FUNC(fuzzy_hash_filename,2, _SC(".s")),
  49. _DECL_FUNC(fuzzy_compare,3, _SC(".ss")),
  50. _DECL_FUNC(fuzzy_compare_small,3, _SC(".ss")),
  51. {0,0}
  52. };
  53. #undef _DECL_FUNC
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. /* This defines a function that opens up your library. */
  58. SQRESULT sqext_register_fuzzy_hash (HSQUIRRELVM sqvm) {
  59. //add a namespace sqmix
  60. sq_pushstring(sqvm,_SC("sqfuzzy_hash"),-1);
  61. sq_newclass(sqvm,SQFalse);
  62. sq_insert_reg_funcs(sqvm, fuzzy_hash_obj_funcs);
  63. sq_newslot(sqvm,-3,SQTrue); //add sq_fossil table to the root table
  64. return SQ_OK;
  65. }
  66. #ifdef __cplusplus
  67. }
  68. #endif
  69. #endif