Skip to content

Commit

Permalink
Remove glib completely
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciej Lechowski committed Oct 16, 2021
1 parent f17d63e commit cd682da
Show file tree
Hide file tree
Showing 13 changed files with 314 additions and 145 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ IndentWidth: '4'
Language: Cpp
PointerAlignment: Right
SortIncludes: 'true'
SpaceAfterLogicalNot: 'true'
SpaceAfterLogicalNot: 'false'
TabWidth: '0'
8 changes: 3 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ set(CPACK_PACKAGE_VERSION ${PROJECT_VERSION})

# deb stuff
set(CPACK_DEBIAN_PACKAGE_HOMEPAGE "https://lchsk.com/xstarter.html")
set(CPACK_DEBIAN_PACKAGE_DEPENDS "libncurses5 (>= 5.0), libglib2.0-0 (>= 2.0)")
set(CPACK_DEBIAN_PACKAGE_DEPENDS "libncurses5 (>= 5.0)")

# rpm stuff
set(CPACK_RPM_PACKAGE_LICENSE "GPL")
set(CPACK_RPM_PACKAGE_URL "https://lchsk.com/xstarter.html")
set(CPACK_RPM_PACKAGE_DESCRIPTION "Application launcher for Linux")
set(CPACK_RPM_PACKAGE_REQUIRES "ncurses >= 5.0, glib2 >= 2.0")
set(CPACK_RPM_PACKAGE_REQUIRES "ncurses >= 5.0")

include(CPack)

Expand All @@ -42,7 +42,6 @@ find_package(PkgConfig REQUIRED)
message(STATUS "Cmake build type: " ${CMAKE_BUILD_TYPE})
message(STATUS "Curses include directory: " ${CURSES_INCLUDE_DIR})

pkg_check_modules(GLIB REQUIRED glib-2.0)
pkg_check_modules(CURSES REQUIRED ncurses)

add_library(lib-inih STATIC
Expand All @@ -52,7 +51,6 @@ add_library(lib-inih STATIC
set_target_properties(lib-inih PROPERTIES LINKER_LANGUAGE C)

include_directories(
${GLIB_INCLUDE_DIRS}
${CURSES_INCLUDE_DIRS}
external/
)
Expand All @@ -64,11 +62,11 @@ add_executable(xstarter
src/term.c
src/utils.c
src/utils_string.c
src/list.c
xstarter.1
)

target_link_libraries(xstarter
${GLIB_LIBRARIES}
${CURSES_LIBRARIES}
-pthread
-linih
Expand Down
113 changes: 113 additions & 0 deletions src/list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include <assert.h>
#include <stdlib.h>

#include "list.h"

static void resize(List *list);

// How full the map must be in order to trigger resize
static const float LIST_RESIZE_TRIGGER = 0.6;

// By what factor we're enlarging the list
static const size_t LIST_RESIZE_FACTOR = 2;

size_t list_in(List *list, void *value)
{
assert(list != NULL);

for (int i = 0; i < list->size; i++) {
if (list->data[i] == value) {
return 1;
}
}

return 0;
}

List *list_new(size_t initial_size)
{
List *list = calloc(1, sizeof(List));

list->data = calloc(initial_size, sizeof(void *));
list->cap = initial_size;
list->size = 0;

return list;
}

void list_free(List *list)
{
if (!list) {
return;
}

if (list->data) {
free(list->data);
}

free(list);
}

void list_append(List *list, void *value)
{
assert(list != NULL);

resize(list);

list->data[list->size] = value;
list->size++;
}

void *list_get(List *list, size_t index)
{
if (index >= list->size) {
return NULL;
}

return list->data[index];
}

size_t list_size(List *list)
{
assert(list != NULL);

return list->size;
}

void list_del(List *list, size_t index)
{
assert(list != NULL);

list->data[index] = NULL;

for (int i = index; i < list->size - 1; i++) {
list->data[i] = list->data[i + 1];
}

list->size--;
}

static void resize(List *list)
{
assert(list != NULL);

if (list->size < list->cap * LIST_RESIZE_TRIGGER) {
return;
}

list->cap = list->cap * LIST_RESIZE_FACTOR;

void **data = calloc(list->cap, sizeof(void *));

for (int i = 0; i < list->cap / LIST_RESIZE_FACTOR; i++) {
void *value = list->data[i];

if (value) {
data[i] = value;
}
}

free(list->data);

list->data = data;
}
20 changes: 20 additions & 0 deletions src/list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef LIST_H
#define LIST_H

#include <stddef.h>

typedef struct {
size_t size;
size_t cap;
void **data;
} List;

List *list_new(size_t initial_size);
void list_free(List *list);
void list_append(List *list, void *value);
void *list_get(List *list, size_t index);
size_t list_size(List *list);
void list_del(List *list, size_t index);
size_t list_in(List *list, void *value);

#endif /* LIST_H */
4 changes: 1 addition & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include <sys/stat.h> // umask
#include <unistd.h> // execvpe, readlink

#include <glib.h>

#include "scan.h"
#include "settings.h"
#include "term.h"
Expand Down Expand Up @@ -37,7 +35,7 @@ int main(int argc, char **argv)

str_copy(exec_term, conf->section_main->terminal, sizeof(exec_term));

if (! in_terminal()) {
if (!in_terminal()) {
cmdline_free(cmdline);
config_free(config_get());

Expand Down
Loading

0 comments on commit cd682da

Please sign in to comment.