feat: IOB structures and iob_init()
This commit is contained in:
parent
269f50a367
commit
92c0b5ba96
2 changed files with 80 additions and 0 deletions
|
|
@ -0,0 +1,34 @@
|
|||
#include "io_backend.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
static struct iob_context context;
|
||||
static FILE *input;
|
||||
|
||||
int iob_init(struct iob_context *ctx)
|
||||
{
|
||||
context = *ctx;
|
||||
|
||||
switch (context.mode)
|
||||
{
|
||||
IOB_MODE_STDIN:
|
||||
input = stdin;
|
||||
return 0;
|
||||
|
||||
IOB_MODE_SCRIPT:
|
||||
if (context.args == NULL)
|
||||
return -2;
|
||||
input = fopen(context.args, "r");
|
||||
if (input == NULL)
|
||||
return -4;
|
||||
|
||||
IOB_MODE_CMD:
|
||||
if (context.args != NULL)
|
||||
return -2;
|
||||
else
|
||||
return 0;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
#ifndef IO_BACKEND_H
|
||||
#define IO_BACKEND_H
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
enum iob_mode {
|
||||
IOB_MODE_NULL = 0,
|
||||
IOB_MODE_STDIN,
|
||||
IOB_MODE_SCRIPT,
|
||||
IOB_MODE_CMD
|
||||
};
|
||||
|
||||
/* @struct iob_context
|
||||
* @var mode
|
||||
* @var args contains
|
||||
* the script name when mode is set to IOB_SCRIPT,
|
||||
* the command to execute when mode is set to IOB_CMD,
|
||||
*/
|
||||
struct iob_context {
|
||||
enum iob_mode mode;
|
||||
char* args;
|
||||
};
|
||||
|
||||
/*
|
||||
* @brief Initializes the IO Backend module
|
||||
*
|
||||
* @param context contains the input mode and the args
|
||||
* @return 0 on success, the corresponding error code otherwise
|
||||
*/
|
||||
int iob_init(struct iob_context *context);
|
||||
|
||||
/* TODO
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
void iob_close();
|
||||
|
||||
/*i TODO
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
ssize_t stream_read(char** stream);
|
||||
|
||||
#endif /* ! IO_BACKEND_H */
|
||||
Loading…
Add table
Add a link
Reference in a new issue