summaryrefslogtreecommitdiffstats
path: root/friendfinder/util
diff options
context:
space:
mode:
Diffstat (limited to 'friendfinder/util')
-rw-r--r--friendfinder/util/projection.c96
-rw-r--r--friendfinder/util/projection.h11
-rw-r--r--friendfinder/util/sqlite_helper.c205
-rw-r--r--friendfinder/util/sqlite_helper.h17
-rw-r--r--friendfinder/util/sqlite_helper.obin0 -> 13024 bytes
5 files changed, 329 insertions, 0 deletions
diff --git a/friendfinder/util/projection.c b/friendfinder/util/projection.c
new file mode 100644
index 0000000..d61a168
--- /dev/null
+++ b/friendfinder/util/projection.c
@@ -0,0 +1,96 @@
+#include <proj_api.h>
+#include "projection.h"
+
+struct projection
+{
+ int type;
+ projPJ pj;
+ int argc;
+ char *argv[];
+
+};
+
+static struct projection merc = {
+ PROJECTION_MERC,
+ NULL,
+ 6,
+ {
+ "proj=merc",
+ "datum=WGS84",
+ "k=1.0",
+ "units=m",
+ "over",
+ "no_defs"}
+};
+
+static struct projection utm = {
+ PROJECTION_UTM,
+ NULL,
+ 3,
+ {
+ "proj=utm",
+ "datum=WGS84",
+ "zone=32"
+ }
+};
+
+static struct projection *p;
+
+static inline int
+projection_init(int type)
+{
+ //XXX pj_free()
+ switch(type)
+ {
+ case PROJECTION_UTM:
+ p = &utm;
+ break;
+ case PROJECTION_MERC:
+ p = &merc;
+ break;
+ default:
+ p = NULL;
+ return -1;
+ }
+
+ if(p->pj != NULL)
+ return 0;
+
+ p->pj = pj_init(p->argc, p->argv);
+ if(p->pj == NULL)
+ {
+ p = NULL;
+ return -1;
+ }
+ return 0;
+}
+
+int
+project_latlon(double lat, double lon, int *e, int *n, int type)
+{
+ projUV point;
+ if(projection_init(type))
+ return -1;
+
+ point.u = lon * DEG_TO_RAD;
+ point.v = lat * DEG_TO_RAD;
+ point = pj_fwd(point, p->pj);
+ *e = point.u;
+ *n = point.v;
+ return 0;
+}
+
+int
+project_latlon_inv(int e, int n, double *lat, double *lon, int type)
+{
+ projUV point;
+ if(projection_init(type))
+ return -1;
+
+ point.u = e;
+ point.v = n;
+ point = pj_inv(point, p->pj);
+ *lon = point.u / DEG_TO_RAD;
+ *lat = point.v / DEG_TO_RAD;
+ return 0;
+}
diff --git a/friendfinder/util/projection.h b/friendfinder/util/projection.h
new file mode 100644
index 0000000..d1509ac
--- /dev/null
+++ b/friendfinder/util/projection.h
@@ -0,0 +1,11 @@
+#ifndef _PROJECTION_H
+#define _PROJECTION_H
+enum
+{
+ PROJECTION_UTM,
+ PROJECTION_MERC
+};
+
+int project_latlon(double lat, double lon, int *e, int *n, int type);
+int project_latlon_inv(int e, int n, double *lat, double *lon, int type);
+#endif
diff --git a/friendfinder/util/sqlite_helper.c b/friendfinder/util/sqlite_helper.c
new file mode 100644
index 0000000..528d2f0
--- /dev/null
+++ b/friendfinder/util/sqlite_helper.c
@@ -0,0 +1,205 @@
+
+#include <stdio.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "util/sqlite_helper.h"
+
+#ifdef WINCE
+#include <Evil.h>
+#endif
+
+
+int sql_result_step(struct sql_result *r)
+{
+ int ret;
+ if(r->stmt == NULL)
+ return -1;
+
+ ret = sqlite3_step(r->stmt);
+ if(ret != SQLITE_ROW)
+ {
+ sqlite3_finalize(r->stmt);
+ r->stmt = NULL;
+ return -1;
+ }
+ return 0;
+}
+
+void sql_result_destroy(struct sql_result *res)
+{
+ sqlite3_finalize(res->stmt);
+ free(res);
+}
+
+static struct sql_result *
+sql_result_create(sqlite3 *db, sqlite3_stmt *stmt)
+{
+ struct sql_result *result = malloc(sizeof(struct sql_result));
+ result->stmt = stmt;
+ result->columnCount = sqlite3_column_count(stmt);
+
+ /*
+ result->cols = (char **)malloc(result->columnCount * sizeof(char *));
+ result->columnTypes = (int *)malloc(result->columnCount * sizeof(int));
+ for(i = 0; i < result->columnCount; i++)
+ {
+ result->cols[i] = (char *)sqlite3_column_name(stmt, i);
+ result->columnTypes[i] = sqlite3_column_type(stmt, i);
+ }
+ */
+ return result;
+}
+
+static struct sql_result *_sql_query(sqlite3 *db, char *sql, int *ret)
+{
+ const char *tail;
+ sqlite3_stmt *stmt;
+
+ *ret = sqlite3_prepare_v2(db, sql, -1, &stmt, &tail);
+ *ret = sqlite3_step(stmt);
+ switch(*ret)
+ {
+ case SQLITE_DONE:
+ sqlite3_finalize(stmt);
+ return NULL;
+ case SQLITE_ROW:
+ return sql_result_create(db, stmt);
+ default:
+ printf("sql_query(%i): %s\n%s \n", *ret, sql, sqlite3_errmsg(db));
+ return NULL;
+ }
+}
+
+static int sql_query_smpl(sqlite3 *db, char *sql)
+{
+ int ret;
+ // char *errmsg;
+
+ const char *tail;
+ sqlite3_stmt *stmt;
+
+ ret = sqlite3_prepare_v2(db, sql, -1, &stmt, &tail);
+ if(ret != 0)
+ printf("ret %i\n", ret);
+ ret = sqlite3_step(stmt);
+ switch(ret)
+ {
+ case SQLITE_DONE:
+ sqlite3_finalize(stmt);
+ return sqlite3_last_insert_rowid(db);
+ case SQLITE_ROW:
+ printf("not a simple query %s\nreturned row", sql);
+ return 0;
+ default:
+ sqlite3_finalize(stmt);
+ printf("sql_query(%i): %s\n%s \n", ret, sql, sqlite3_errmsg(db));
+ return -1;
+ }
+
+ /*
+ ret = sqlite3_exec(osm->db, sql, NULL, NULL, &errmsg);
+ if(ret != SQLITE_OK)
+ {
+ printf("osm_query_smpl(%i): %s\n", ret, sql);
+ printf("err mesg %s\n", errmsg);
+ return -1;
+ }
+
+ return 0;
+ */
+}
+
+#ifdef WINCE
+static inline void unix2win_path(char *str)
+{
+ while(*str)
+ {
+ if(*str == '/')
+ *str = '\\';
+ str++;
+ }
+}
+
+char *wince_full_path(char *file)
+{
+ char pwd[100];
+ char dirbuff[512];
+ getcwd(pwd, 100);
+ sprintf(dirbuff, "%s/%s", pwd, file);
+ unix2win_path(dirbuff);
+ return strdup(dirbuff);
+}
+#endif
+
+
+sqlite3 *sql_open(char *filename)
+{
+ sqlite3 *db;
+ char *file;
+#ifdef WINCE
+ file = wince_full_path(filename);
+#else
+ file = filename;
+#endif
+ if(sqlite3_open(file, &db))
+ {
+ fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
+#ifdef WINCE
+ free(file);
+#endif
+ return NULL;
+ }
+#ifdef WINCE
+ free(file);
+#endif
+ return db;
+}
+
+int sql_exec(sqlite3 *db, char *msgfmt, ...)
+{
+ char sql[512];
+ va_list args;
+ int ret;
+
+ va_start(args, msgfmt);
+ ret = vsprintf(sql, msgfmt, args);
+ va_end(args);
+ if(ret < 0)
+ return ret;
+
+ return sql_query_smpl(db, sql);
+}
+
+
+struct sql_result *sql_query(sqlite3 *db, int *error, char *msgfmt, ...)
+{
+ char sql[512];
+ va_list args;
+ int ret;
+ struct sql_result *res;
+
+ va_start(args, msgfmt);
+ ret = vsprintf(sql, msgfmt, args);
+ va_end(args);
+
+ fprintf(stdout, "%s\n", sql);
+
+ if(ret < 0)
+ {
+ if(error)
+ *error = ret;
+ return NULL;
+ }
+
+ res = _sql_query(db, sql, &ret);
+ if(res == NULL)
+ {
+ if(error)
+ *error = ret;
+ return NULL;
+ }
+ return res;
+}
+
diff --git a/friendfinder/util/sqlite_helper.h b/friendfinder/util/sqlite_helper.h
new file mode 100644
index 0000000..99f2ab1
--- /dev/null
+++ b/friendfinder/util/sqlite_helper.h
@@ -0,0 +1,17 @@
+#ifndef ____DB_H_
+#define ____DB_H_
+
+#include <sqlite3.h>
+
+struct sql_result
+{
+ sqlite3_stmt *stmt;
+ int columnCount;
+};
+
+int sql_result_step(struct sql_result *);
+void sql_result_destroy(struct sql_result *res);
+sqlite3 *sql_open(char *filename);
+int sql_exec(sqlite3 *db, char *msgfmt, ...);
+struct sql_result *sql_query(sqlite3 *db, int *error, char *msgfmt, ...);
+#endif
diff --git a/friendfinder/util/sqlite_helper.o b/friendfinder/util/sqlite_helper.o
new file mode 100644
index 0000000..424fc5a
--- /dev/null
+++ b/friendfinder/util/sqlite_helper.o
Binary files differ