00001 #include <stdafx.h> 00002 00003 #ifdef STANDARD 00004 #include <stdio.h> 00005 #include <string.h> 00006 #else 00007 #include <my_global.h> 00008 #include <my_sys.h> 00009 #endif 00010 00011 #include <mysql.h> 00012 #include <m_ctype.h> 00013 #include <m_string.h> // To get strmov() 00014 00015 00016 //Function to return the distance between two keypoints 00017 00018 //Make with 'g++ -fPIC -I/usr/include/mysql -I/usr/include/libmodplug -O -pipe -o libObjRecUtil.so -shared -lmysqlclient ObjRecUtil.cc' 00019 //Install with CREATE FUNCTION kp_dist RETURNS INTEGER SONAME "libObjRecUtil.so"; 00020 00021 00022 /* These must be right or mysqld will not find the symbol! */ 00023 extern "C" { 00024 my_bool kp_dist_init( UDF_INIT* initid, UDF_ARGS* args, char* message ); 00025 void kp_dist_deinit( UDF_INIT* initid ); 00026 longlong kp_dist(UDF_INIT * initid, UDF_ARGS *args, char *is_null, char * /*error*/ ); 00027 } 00028 00029 my_bool kp_dist_init( UDF_INIT* initid, UDF_ARGS* args, char* message ) 00030 { 00031 uint i; 00032 00033 if (args->arg_count != 2) 00034 { 00035 strcpy(message,"kp_dist must have two arguments"); 00036 return 1; 00037 } 00038 00039 initid->maybe_null=1; /* The result may be null */ 00040 initid->decimals=2; /* We want 2 decimals in the result */ 00041 initid->max_length=6; /* 3 digits + . + 2 decimals */ 00042 return 0; 00043 } 00044 00045 void kp_dist_deinit( UDF_INIT* initid ) 00046 { 00047 } 00048 00049 longlong kp_dist(UDF_INIT *initid __attribute__((unused)), UDF_ARGS *args, 00050 char *is_null, char *error __attribute__((unused))) 00051 { 00052 longlong dist = 0; 00053 00054 if (args->args[0] == NULL || args->args[1] == NULL || 00055 args->lengths[0] < 128 || args->lengths[1] < 128 00056 ) 00057 { 00058 *is_null = 1; 00059 return dist; 00060 } 00061 00062 char* kp1 = args->args[0]; 00063 char* kp2 = args->args[1]; 00064 00065 for(int i=0; i<128; i++) 00066 { 00067 dist += (kp1[i]-kp2[i])*(kp1[i]-kp2[i]); 00068 } 00069 00070 return dist; 00071 00072 }