Skip to content

Commit

Permalink
New libUseful files
Browse files Browse the repository at this point in the history
  • Loading branch information
ColumPaget committed Jul 19, 2017
1 parent deffab5 commit c795999
Show file tree
Hide file tree
Showing 10 changed files with 2,163 additions and 0 deletions.
38 changes: 38 additions & 0 deletions libUseful-2.8/Errors.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "Errors.h"
#include "includes.h"
#include <errno.h>

ERROR_CALLBACK libUsefulErrorCallback=libUsefulDefaultErrorCallback;

char *ErrorString;

char *GetErrorString(char *Buffer)
{

}

void libUsefulDefaultErrorCallback(int flags, const char *where, const char *file, int line, const char *String)
{
const char *ptr="";

if (flags & ERRFLAG_ERRNO) ptr=strerror(errno);
fprintf(stderr, "ERROR: %s %s:%d %s. %s\n", where, file, line, String, ptr);
}


void InternalRaiseError(int flags, const char *where, const char *file, int line, const char *FmtStr, ...)
{
char *Tempstr=NULL;
va_list args;

if (libUsefulErrorCallback)
{
va_start(args,FmtStr);
Tempstr=VFormatStr(Tempstr,FmtStr,args);
va_end(args);

libUsefulErrorCallback(flags, where, file, line, Tempstr);
}

DestroyString(Tempstr);
}
16 changes: 16 additions & 0 deletions libUseful-2.8/Errors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef LIBUSEFUL_ERRORS_H
#define LIBUSEFUL_ERRORS_H

#define ERRFLAG_ERRNO 1

#define RaiseError(flags, type, fmt, ...) InternalRaiseError(flags, type, __FILE__, __LINE__, fmt, ##__VA_ARGS__)


typedef void (*ERROR_CALLBACK)(int flags, const char *where, const char *file, int line, const char *ErrorString);
extern ERROR_CALLBACK libUsefulErrorCallback;

void libUsefulDefaultErrorCallback(int flags, const char *where, const char *file, int line, const char *String);
char *GetErrorString(char *Buffer);
void InternalRaiseError(int flags, const char *type, const char *file, int line, const char *FmtStr, ...);

#endif
110 changes: 110 additions & 0 deletions libUseful-2.8/Expect.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include "includes.h"
#include "Expect.h"


//Values for 'flags' that are not visible to the user
//These must not clash with any visible values
#define DIALOG_DONE 67108864


void ExpectDialogAdd(ListNode *ExpectDialogs, char *Expect, char *Reply, int Flags)
{
TExpectDialog *ExpectDialog;

ExpectDialog=(TExpectDialog *) calloc(1,sizeof(TExpectDialog));
ExpectDialog->Expect=CopyStr(ExpectDialog->Expect,Expect);
ExpectDialog->Reply=CopyStr(ExpectDialog->Reply,Reply);
ExpectDialog->Flags=Flags;

ListAddItem(ExpectDialogs,ExpectDialog);
}

void ExpectDialogDestroy(void *p_Item)
{
TExpectDialog *ExpectDialog;

ExpectDialog=(TExpectDialog *) p_Item;
DestroyString(ExpectDialog->Expect);
DestroyString(ExpectDialog->Reply);
free(ExpectDialog);
}



int STREAMExpectDialog(STREAM *S, ListNode *ExpectDialogs)
{
int inchar;
ListNode *Curr;
TExpectDialog *ExpectDialog;

inchar=STREAMReadChar(S);
while (inchar !=EOF)
{
if (inchar > 0)
{
Curr=ListGetNext(ExpectDialogs);
while (Curr)
{
ExpectDialog=(TExpectDialog *) Curr->Item;
if (! (ExpectDialog->Flags & DIALOG_DONE))
{
//if the current value does not equal where we are in the string
//we have to consider whether it is the first character in the string
if (ExpectDialog->Expect[ExpectDialog->Match]!=inchar) ExpectDialog->Match=0;

if (ExpectDialog->Expect[ExpectDialog->Match]==inchar)
{
ExpectDialog->Match++;
if (ExpectDialog->Expect[ExpectDialog->Match]=='\0')
{
ExpectDialog->Match=0;
ExpectDialog->Flags |= DIALOG_DONE;
if (ExpectDialog->Reply) STREAMWriteLine(ExpectDialog->Reply,S);
if (ExpectDialog->Flags & DIALOG_END) return(TRUE);
if (ExpectDialog->Flags & DIALOG_FAIL) return(FALSE);
}
}

if (! (ExpectDialog->Flags & DIALOG_OPTIONAL)) break;
}
Curr=ListGetNext(Curr);
}
}
inchar=STREAMReadChar(S);
}

return(FALSE);
}


int STREAMExpectAndReply(STREAM *S, const char *Expect, const char *Reply)
{

if (STREAMReadToString(S, NULL, NULL, Expect))
{
if (Reply)
{
STREAMWriteLine(Reply,S);
STREAMFlush(S);
}
return(TRUE);
}

return(FALSE);
}


int STREAMExpectSilence(STREAM *S, int wait)
{
int inchar;
int len=0, SavedTimeout;

SavedTimeout=S->Timeout;
S->Timeout=wait;
inchar=STREAMReadChar(S);
while (inchar > 0) inchar=STREAMReadChar(S);

S->Timeout=SavedTimeout;

return(FALSE);
}
37 changes: 37 additions & 0 deletions libUseful-2.8/Expect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

#ifndef LIBUSEFUL_EXPECT_H
#define LIBUSEFUL_EXPECT_H

#include "file.h"

#define DIALOG_END 1
#define DIALOG_FAIL 2
#define DIALOG_OPTIONAL 4

typedef struct
{
int Flags;
int Match;
char *Expect;
char *Reply;
} TExpectDialog;


#ifdef __cplusplus
extern "C" {
#endif

int STREAMExpectAndReply(STREAM *S, const char *Expect, const char *Reply);
int STREAMExpectSilence(STREAM *S, int wait);

void ExpectDialogAdd(ListNode *Dialogs, char *Expect, char *Reply, int Flags);
int STREAMExpectDialog(STREAM *S, ListNode *Dialogs);

void ExpectDialogDestroy(void *Item);

#ifdef __cplusplus
}
#endif


#endif
Loading

0 comments on commit c795999

Please sign in to comment.