Skip to content
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
all: library test

library:
gcc -c geohash.c -o geohash.o
ar rcs libgeohash.a geohash.o
gcc -fPIC -c geohash.c -o geohash.o
gcc -shared -o libgeohash.so geohash.o
rm geohash.o

test:
Expand All @@ -13,4 +13,4 @@ test:
rm a.out

clean:
rm -rf *.a *.o
rm -rf *.a *.o *.so
17 changes: 10 additions & 7 deletions geohash.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,26 @@ unsigned int index_for_char(char c, char *string) {
return index;
}

char* get_neighbor(char *hash, int direction) {
char* get_neighbor(const char *hash, int direction) {

int hash_length = strlen(hash);

char last_char = hash[hash_length - 1];
char last_char = hash[hash_length - 1];

int is_odd = hash_length % 2;
char **border = is_odd ? odd_borders : even_borders;
char **neighbor = is_odd ? odd_neighbors : even_neighbors;

char *base = malloc(sizeof(char) * 1);
char *base = malloc(sizeof(char) * hash_length + 1);
base[0] = '\0';
strncat(base, hash, hash_length - 1);

if(index_for_char(last_char, border[direction]) != -1)
base = get_neighbor(base, direction);

if(index_for_char(last_char, border[direction]) != -1)
{
char *newBase = get_neighbor(base, direction);
strncpy( base , newBase , hash_length );
free( newBase );
}
int neighbor_index = index_for_char(last_char, neighbor[direction]);
last_char = char_map[neighbor_index];

Expand Down Expand Up @@ -242,7 +245,7 @@ GeoCoord geohash_decode(char *hash) {
}


char** geohash_neighbors(char *hash) {
char** geohash_neighbors(const char *hash) {

char** neighbors = NULL;

Expand Down
13 changes: 11 additions & 2 deletions geohash.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifdef __cplusplus
extern "C" {
#endif

// Metric in meters
typedef struct GeoBoxDimensionStruct {

Expand Down Expand Up @@ -69,9 +73,14 @@ extern GeoCoord geohash_decode(char* hash);
* N, NE, E, SE, S, SW, W, NW
* 0, 1, 2, 3, 4, 5, 6, 7
*/
extern char** geohash_neighbors(char* hash);
extern char** geohash_neighbors(const char* hash);

/*
* Returns the width and height of a precision value.
*/
extern GeoBoxDimension geohash_dimensions_for_precision(int precision);
extern GeoBoxDimension geohash_dimensions_for_precision(int precision);


#ifdef __cplusplus
}
#endif
Binary file added libgeohash.a
Binary file not shown.
Binary file added libgeohash.so
Binary file not shown.