Skip to content

Commit

Permalink
Merge pull request #4 from yihaoyan/master
Browse files Browse the repository at this point in the history
yihao version2
  • Loading branch information
w32zhong committed Dec 27, 2015
2 parents 49a0d59 + 5dfc3f1 commit 6ce3029
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
11 changes: 8 additions & 3 deletions term-lookup/run/term_lookup_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
#include <mysql/mysql.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main() {

int main() {
/*
test term look up open; print all tables in the database
*/
char* dbname = "search_engine_db";
char *dbname = "search_engine_db:root:123";
MYSQL *conn;
conn = (MYSQL *)term_lookup_open(dbname);
MYSQL_RES *res;
Expand All @@ -22,8 +24,11 @@ main() {
printf("%s \n", row[0]);
}
mysql_free_result(res);
term_id_t test1 = term_lookup(conn, L"house");
term_id_t test1 = term_lookup(conn, L"happy");
printf("%d\n", test1);
term_lookup_close(conn);

/*******************************/

return 0;
}
55 changes: 51 additions & 4 deletions term-lookup/term-lookup.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,66 @@
#include "term-lookup.h"
#include "wstring.h"

/*
split a string by delimiliter
*/
char** str_split(char* a_str, const char a_delim)
{
char** result = 0;
size_t count = 0;
char* tmp = a_str;
char* last_comma = 0;
char delim[2];
delim[0] = a_delim;
delim[1] = 0;
/* Count how many elements will be extracted. */
while (*tmp)
{
if (a_delim == *tmp)
{
count++;
last_comma = tmp;
}
tmp++;
}
/* Add space for trailing token. */
count += last_comma < (a_str + strlen(a_str) - 1);
/* Add space for terminating null string so caller
knows where the list of returned strings ends. */
count++;
result = malloc(sizeof(char*) * count);
if (result)
{
size_t idx = 0;
char* token = strtok(a_str, delim);
while (token)
{
*(result + idx++) = strdup(token);
token = strtok(0, delim);
}
*(result + idx) = 0;
}
return result;
}

/*
open a database and return the connection;
and create table dict_term automatically if not exist
params:
dninfo= "di:username:password (search_engine_db:root:123)"
*/

void *term_lookup_open(char *dbname){
void *term_lookup_open(char *di){
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char dbinfo[512];
strcpy(dbinfo, di);
char **info = str_split(dbinfo, ':');
char *server = "localhost";
char *user = "root";
char *password = "123";
char *database = dbname; //"search_engine_db";
char *user = *(info+1);
char *password = *(info+2);
char *database = *(info+0); //"search_engine_db";
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
Expand Down

0 comments on commit 6ce3029

Please sign in to comment.