Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
misterabdul committed Jun 16, 2021
2 parents baeda2f + afd5930 commit e4f64a3
Show file tree
Hide file tree
Showing 13 changed files with 1,752 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "lib/libkbbi"]
path = lib/libkbbi
url = [email protected]:misterabdul/libkbbi.git
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# KBBI GTK

KBBI offline remake with GTK4
KBBI offline remake with GTK4.

This is my hobby project to learn more about C programming, widget programming using GTK, and dynamic library. Feel free to explore, fork, or create a pull request if you interested.

![Alt text](./assets/screenshots/screenshot_1.png "KBBI Offline")
Binary file added assets/screenshots/screenshot_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
1 change: 1 addition & 0 deletions lib/libkbbi
Submodule libkbbi added at 0c1af3
42 changes: 42 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
BIN := kbbi-gtk

SRCDIR := src
OBJDIR := obj
BINDIR := bin
LIBDIR := lib

LIB_KBBI := libkbbi

PKG_CFG_GTK4 := $(shell pkg-config --cflags --libs gtk+-3.0 webkit2gtk-4.0) -ldl

CFLAGS ?= -Wall -Wextra -g

BIN_SRCS := $(shell find $(SRCDIR) -name *.c)
BIN_OBJS := $(subst $(SRCDIR)/,$(SRCDIR)_,$(BIN_SRCS:%=$(OBJDIR)/%.o))

.PHONY: all

all: $(BINDIR)/$(BIN) $(BINDIR)/$(LIB_KBBI).so

clean:
@$(RM) -rf bin/*
@$(RM) -rf obj/*

# Link all the objects to create main binary
$(BINDIR)/$(BIN): $(BIN_OBJS)
@echo LINK $(BIN)
@$(CC) $(BIN_OBJS) -o $@ $(PKG_CFG_GTK4) $(CFLAGS)

# Compile each main source code
$(OBJDIR)/$(SRCDIR)_%.c.o: $(SRCDIR)/%.c
@echo CC $<
@$(CC) -c $< -o $@ $(PKG_CFG_GTK4) $(CFLAGS)

# Move libkbbi.so
$(BINDIR)/$(LIB_KBBI).so: $(LIBDIR)/$(LIB_KBBI)/bin/$(LIB_KBBI).so
@mv $(LIBDIR)/$(LIB_KBBI)/bin/$(LIB_KBBI).so $(BINDIR)/

# Compile libkbbi.so
$(LIBDIR)/$(LIB_KBBI)/bin/$(LIB_KBBI).so:
@cd $(LIBDIR)/$(LIB_KBBI); \
$(MAKE) all
2 changes: 2 additions & 0 deletions obj/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
157 changes: 157 additions & 0 deletions src/lib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#include <dlfcn.h>
#include <stdlib.h>
#include <string.h>

#include "lib.h"

/**
* The Lib's private data struct.
*/
typedef struct _private
{
void* soHandler;
Results (*initResult)(void);
void (*freeResult)(Results);
int (*search)(Results*, int*, char*, int);
int (*count)(void);
char* (*version)(void);
} * Private;

int
Lib_load(Lib* lib, const char* path)
{
void* soHandler = dlopen(path, RTLD_LAZY);
if (!soHandler)
return 0;

/* clear any existing eror */
dlerror();

Results (*initResult)(void);
void (*freeResult)(Results);
int (*search)(Results*, int*, char*, int);
int (*count)(void);
char* (*version)(void);

*(void**)(&initResult) = dlsym(soHandler, "init_result");
*(void**)(&freeResult) = dlsym(soHandler, "free_result");
*(void**)(&search) = dlsym(soHandler, "search");
*(void**)(&count) = dlsym(soHandler, "count");
*(void**)(&version) = dlsym(soHandler, "version");

if (dlerror())
return 0;

Private libPrivateInstance = malloc(sizeof(struct _private));
libPrivateInstance->soHandler = soHandler;
libPrivateInstance->initResult = initResult;
libPrivateInstance->freeResult = freeResult;
libPrivateInstance->search = search;
libPrivateInstance->count = count;
libPrivateInstance->version = version;

Lib libInstance = malloc(sizeof(struct _lib));
libInstance->results = NULL;
libInstance->resultSize = 0;
libInstance->private = libPrivateInstance;
*lib = libInstance;

return 1;
}

/**
* Custom function to free the result recursively.
*
* @param[in] result
*/
void
freeResult(Results result)
{
if (result) {
// if (result->artikata)
// free(result->artikata);
// if (result->katakunci)
// free(result->katakunci);
freeResult(result->next);
free(result);
}
}

void
Lib_freeResult(Lib* libInstance)
{
if (*libInstance)
freeResult((*libInstance)->results);

(*libInstance)->results = NULL;
(*libInstance)->resultSize = 0;
}

void
Lib_close(Lib* libInstance)
{
if (*libInstance) {
Private libPrivateInstance = (Private)(*libInstance)->private;

if (libPrivateInstance && libPrivateInstance->soHandler) {
dlclose(libPrivateInstance->soHandler);
free(libPrivateInstance);
}
}

free(*libInstance);
*libInstance = NULL;
}

int
Lib_search(Lib libInstance, const char* query)
{
int status = 0;

if (libInstance && query) {
Private libPrivateInstance = (Private)libInstance->private;

if (libPrivateInstance && libPrivateInstance->search &&
libPrivateInstance->initResult && libPrivateInstance->freeResult) {
Results results = libPrivateInstance->initResult();
int resultSize = 0;
status =
libPrivateInstance->search(&results, &resultSize, query, strlen(query));

if (libInstance->results)
libPrivateInstance->freeResult(libInstance->results);
libInstance->results = results;
libInstance->resultSize = resultSize;
}
}

return status;
}

int
Lib_count(Lib libInstance)
{
if (libInstance) {
Private libPrivateInstance = (Private)libInstance->private;

if (libPrivateInstance && libPrivateInstance->count) {
return libPrivateInstance->count();
}
}

return 0;
}

char*
Lib_version(Lib libInstance)
{
if (libInstance) {
Private libPrivateInstance = (Private)libInstance->private;

if (libPrivateInstance && libPrivateInstance->version) {
return libPrivateInstance->version();
}
}

return NULL;
}
78 changes: 78 additions & 0 deletions src/lib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#ifndef _LIB_H
#define _LIB_H

/**
* The results struct.
*/
typedef struct _results
{
char* katakunci;
char* artikata;
struct _results* next;
} * Results;

/**
* The lib struct.
*/
typedef struct _lib
{
Results results;
int resultSize;
void* private;
} * Lib;

/**
* Load the lib from given path.
*
* @param[out] lib The lib instance.
* @param[in] path The lib's path.
* @return Result of the load lib process.
*/
int
Lib_load(Lib* lib, const char* path);

/**
* Close the lib.
*
* @param[out] lib The lib instance.
*/
void
Lib_close(Lib* lib);

/**
* Free the result instance.
*
* @param[out] lib The lib instance.
*/
void
Lib_freeResult(Lib* lib);

/**
* Do search on lib with given query.
*
* @param[in] lib The lib instance.
* @param[in] query The search query.
* @return Status of the search (found or not found).
*/
int
Lib_search(Lib lib, const char* query);

/**
* Count all words inside the lib.
*
* @param[in] lib The lib instance.
* @return Total number of words inside the lib.
*/
int
Lib_count(Lib lib);

/**
* Get the current lib version.
*
* @param[in] lib The lib instance.
* @return The current lib version string.
*/
char*
Lib_version(Lib lib);

#endif
Loading

0 comments on commit e4f64a3

Please sign in to comment.