Skip to content

Commit

Permalink
Implemented a linked list class, hacked to remove dependency on GList.
Browse files Browse the repository at this point in the history
  • Loading branch information
vogon committed Oct 12, 2002
1 parent da69c4d commit caa5afa
Show file tree
Hide file tree
Showing 7 changed files with 311 additions and 53 deletions.
91 changes: 91 additions & 0 deletions libinews/IList.c
@@ -0,0 +1,91 @@
/* LibINews -- the only IcculusNews backend with the power of nougat
* copyright (c) 2002 Colin "vogon" Bayer
*
* [ -- Insert GPL boilerplate here -- ]
*
*/

#include "IList.h"

/* IList, the helping phriendly doubly-linked list. */

IList *ilist_append(IList *list, IList *new);
IList *ilist_append_data(IList *list, void *data);

IList *ilist_prepend(IList *list, IList *new);
IList *ilist_prepend_data(IList *list, void *data);

IList *ilist_remove(IList *ptr);

unsigned int ilist_length(IList *ptr);

IList *__get_last(IList *list);
IList *__get_first(IList *list);

/* FIXME: extremely quick implementation. probably 50 zillion bugs in the next
* 65 lines, but who's counting? -- vogon. */

IList *ilist_append(IList *list, IList *new) {
new->prev = __get_last(list);
if (new->prev) new->prev->next = new;
new->next = NULL;

return __get_first(new);
}

IList *ilist_append_data(IList *list, void *data) {
IList *new = (IList *)malloc(sizeof(IList));

new->data = data;

return ilist_append(list, new);
}

IList *ilist_prepend(IList *list, IList *new) {
new->next = __get_first(list);
if (new->next) new->next->prev = new;
new->prev = NULL;

return new;
}

IList *ilist_prepend_data(IList *list, void *data) {
IList *new = (IList *)malloc(sizeof(IList));

new->data = data;

return ilist_prepend(list, new);
}

IList *ilist_remove(IList *ptr) {
if (ptr->prev) ptr->prev->next = ptr->next;
if (ptr->next) ptr->next->prev = ptr->prev;

return __get_first(ptr->next);
}

unsigned int ilist_length(IList *ptr) {
int count = 0;

for (IList *iter = __get_first(ptr); iter != NULL; iter = iter->next) {
count++;
}

return count;
}

IList *__get_last(IList *list) {
IList *last = list;

while (last && last->next) { last = last->next; }

return last;
}

IList *__get_first(IList *list) {
IList *first = list;

while (first && first->prev) { first = first->prev; }

return first;
}
32 changes: 32 additions & 0 deletions libinews/IList.h
@@ -0,0 +1,32 @@
/* LibINews -- the only IcculusNews backend with the power of nougat
* copyright (c) 2002 Colin "vogon" Bayer
*
* [ -- Insert GPL boilerplate here -- ]
*
*/

/* IList, the helping phriendly doubly-linked list. */

#include <stdlib.h>

struct _IList {
void *data;
struct _IList *prev;
struct _IList *next;
};

typedef struct _IList IList;

extern IList *ilist_append(IList *list, IList *new);
extern IList *ilist_append_data(IList *list, void *data);

extern IList *ilist_prepend(IList *list, IList *new);
extern IList *ilist_prepend_data(IList *list, void *data);

extern IList *ilist_remove(IList *ptr);

extern unsigned int ilist_length(IList *ptr);

#define ilist_free(ptr) free(ptr)
#define ilist_prev(ptr) ptr->prev
#define ilist_next(ptr) ptr->next
22 changes: 14 additions & 8 deletions libinews/IcculusNews.h
Expand Up @@ -86,25 +86,25 @@ extern Sint8 INEWS_init();
extern void INEWS_deinit();

/* get the version of the library used at runtime */
extern INEWS_Version *INEWS_getVersion();
extern const INEWS_Version *INEWS_getVersion();

/* get the version of the server daemon */
extern char *INEWS_getServerVersion();
extern inline const char *INEWS_getServerVersion();

/* get the current remote host */
extern char *INEWS_getHost();
extern inline const char *INEWS_getHost();

/* get the current remote port */
extern Uint16 INEWS_getPort();
extern inline Uint16 INEWS_getPort();

/* get the current IcculusNews username */
extern const char *INEWS_getUserName();
extern inline const char *INEWS_getUserName();

/* get the current IcculusNews user ID */
extern Uint16 INEWS_getUID();
extern inline Uint16 INEWS_getUID();

/* get the currently-selected IcculusNews queue ID */
extern Uint16 INEWS_getQID();
extern inline Uint16 INEWS_getQID();

/* get detailed information on a chosen queue */
extern QueueInfo *INEWS_getQueueInfo(int qid);
Expand All @@ -113,7 +113,7 @@ extern QueueInfo *INEWS_getQueueInfo(int qid);
extern QueueInfo **INEWS_getAllQueuesInfo();

/* get the error number of the last error to occur */
extern Sint8 INEWS_getLastError();
extern inline Sint8 INEWS_getLastError();

/* connect to an IcculusNews server */
extern Sint8 INEWS_connect(const char *hostname, Uint32 port);
Expand All @@ -133,6 +133,12 @@ extern ArticleInfo **INEWS_digest(int n);
/* submit an article. OMG */
extern Sint8 INEWS_submitArticle(char *title, char *body);

/* change the approval status of article aid */
Sint8 INEWS_changeApprovalStatus(Uint32 aid, bool approve);

/* change the deletion status of article aid */
Sint8 INEWS_changeDeletionStatus(Uint32 aid, bool delete);

/* free the memory dynamically allocated by a call to INEWS_digest */
extern void INEWS_freeDigest(ArticleInfo **digest);

Expand Down
8 changes: 4 additions & 4 deletions libinews/Makefile
@@ -1,12 +1,12 @@
CC = gcc-3.2
BASE_CFLAGS = -std=c99 -ggdb `pkg-config --cflags glib-2.0` -D_REENTRANT
BASE_LDFLAGS = -ggdb -shared `pkg-config --libs glib-2.0` -lpthread
BASE_CFLAGS = -std=c99 -fPIC -ggdb `pkg-config --cflags glib-2.0` -D_REENTRANT
BASE_LDFLAGS = -ggdb -fPIC -shared `pkg-config --libs glib-2.0` -lpthread

.c.o:
$(CC) $(BASE_CFLAGS) $(CFLAGS) -c $<

libinews.so.0: net.o utility.o init.o
$(CC) $(BASE_LDFLAGS) $(LDFLAGS) -o libinews.so.0 utility.o net.o init.o
libinews.so.0: net.o utility.o init.o IList.o
$(CC) $(BASE_LDFLAGS) $(LDFLAGS) -o libinews.so.0 IList.o utility.o net.o init.o

test: libinews.so.0 test.c
$(CC) -ggdb -L. -linews -o test test.c
Expand Down
24 changes: 14 additions & 10 deletions libinews/internals.h
Expand Up @@ -23,6 +23,7 @@
/* we use glib, because I'm too lazy to write
* string-mangling functions myself */
#include <glib-2.0/glib.h>
#include "IList.h"

#define INEWS_MAJOR __INEWS_LINKTIME_MAJOR
#define INEWS_MINOR __INEWS_LINKTIME_MINOR
Expand All @@ -42,7 +43,8 @@ typedef struct {
} ServerState;

ServerState serverstate;
GList *qinfoptr;
/*GList *qinfoptr;*/
IList *qinfoptr;
int fd;
struct sockaddr_in sa;
size_t sa_len;
Expand All @@ -55,16 +57,16 @@ pthread_t nop_thread;
Sint8 INEWS_init();
void INEWS_deinit();

INEWS_Version *INEWS_getVersion();
char *INEWS_getServerVersion();
char *INEWS_getHost();
Uint16 INEWS_getPort();
const char *INEWS_getUserName();
Uint16 INEWS_getUID();
Uint16 INEWS_getQID();
const INEWS_Version *INEWS_getVersion();
inline const char *INEWS_getServerVersion();
inline const char *INEWS_getHost();
inline Uint16 INEWS_getPort();
inline const char *INEWS_getUserName();
inline Uint16 INEWS_getUID();
inline Uint16 INEWS_getQID();
QueueInfo *INEWS_getQueueInfo(int qid);
QueueInfo **INEWS_getAllQueuesInfo();
Sint8 INEWS_getLastError();
inline Sint8 INEWS_getLastError();

void INEWS_freeDigest(ArticleInfo **digest);
void INEWS_freeQueuesInfo(QueueInfo **qinfo);
Expand All @@ -75,13 +77,15 @@ Sint8 INEWS_retrQueueInfo();
Sint8 INEWS_changeQueue(int qid);
ArticleInfo **INEWS_digest(int n);
Sint8 INEWS_submitArticle(char *title, char *body);
Sint8 INEWS_changeApprovalStatus(Uint32 aid, bool approve);
Sint8 INEWS_changeDeletionStatus(Uint32 aid, bool delete);
void INEWS_disconnect();

Sint8 __read_line(char *str, int max_sz);
Sint8 __write_block(char *str);
char *__chop(char *str);
void *__nop_thread(void *foo);
void __free_queue_info_list_element(GList *ptr);
void __free_queue_info_list_element(/*G*/IList *ptr);
void __print_protocol_fuckery_message();

#endif

0 comments on commit caa5afa

Please sign in to comment.