First commit to this server
This commit is contained in:
1 parent
ec05d3601a
commit
cce6f6b9c6
44 files changed
+5529
-296
No files matched your search
+216
@@ -0,0 +1,216 @@
|
||||
#include <string.h>
|
||||
#include "foo1.h"
|
||||
#include "utils/filesys.h"
|
||||
|
||||
const char dateout[16];
|
||||
const char timeout[20];
|
||||
extern const char* commands;
|
||||
extern const int noCmds;
|
||||
extern struct user_ctx user;
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning( push )
|
||||
#pragma warning( disable : 836 )
|
||||
#pragma warning( disable : 594 )
|
||||
#pragma warning( disable : 4090 )
|
||||
#pragma warning( disable : 859 )
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Command objects. Normally these would go into separate translation units.
|
||||
* Quit is a special command that has its own return status, needed to signal
|
||||
* to main that we are exiting.
|
||||
* Callee functions are responsible for parsing and converting the opts string
|
||||
* for any necessary arguments.
|
||||
*/
|
||||
int
|
||||
dummy(const char* opts)
|
||||
{
|
||||
return CMD_OK;
|
||||
}
|
||||
|
||||
int
|
||||
aaa(const char* opts)
|
||||
{
|
||||
int size;
|
||||
char** splitresult = split(opts, ' ', &size);
|
||||
|
||||
if(size < 2)
|
||||
printf("%s called with no options.\n", splitresult[0]);
|
||||
|
||||
for(int n = 1; n < size; n++)
|
||||
printf("%s called with option %s.\n", splitresult[0], splitresult[n]);
|
||||
|
||||
free(splitresult);
|
||||
|
||||
return CMD_OK;
|
||||
}
|
||||
|
||||
int
|
||||
alist(const char* opts)
|
||||
{
|
||||
int size;
|
||||
char** splitresult = split(opts, ' ', &size);
|
||||
|
||||
for(int n = 1; n < size; n++)
|
||||
printf("access_lists called with option %s.\n", splitresult[n]);
|
||||
|
||||
free(splitresult);
|
||||
|
||||
return CMD_OK;
|
||||
}
|
||||
|
||||
int
|
||||
amp(const char* opts)
|
||||
{
|
||||
printf("amp called with %s\n", opts);
|
||||
|
||||
return CMD_OK;
|
||||
}
|
||||
|
||||
int
|
||||
app(const char* opts)
|
||||
{
|
||||
printf("app called with %s\n", opts);
|
||||
return CMD_OK;
|
||||
}
|
||||
|
||||
int
|
||||
arp(const char* opts)
|
||||
{
|
||||
printf("arp called with %s\n", opts);
|
||||
return CMD_OK;
|
||||
}
|
||||
|
||||
int
|
||||
badl(const char* opts)
|
||||
{
|
||||
int n = 0;
|
||||
char line[100];
|
||||
FILE* fp = fopen("./list.dev", "r");
|
||||
|
||||
if(!fp) {
|
||||
printf("No lists have been created yet\n");
|
||||
return CMD_FILEEXST;
|
||||
}
|
||||
|
||||
while(fgets(line, sizeof(line), fp))
|
||||
if(strlen(line) > 1) printf("%d - %s", ++n, line);
|
||||
|
||||
fclose(fp);
|
||||
return CMD_OK;
|
||||
}
|
||||
|
||||
int
|
||||
batch(const char* opts)
|
||||
{
|
||||
printf("batch called with %s\n", opts);
|
||||
return CMD_OK;
|
||||
}
|
||||
|
||||
int
|
||||
bert(const char* opts)
|
||||
{
|
||||
printf("bert called with %s\n", opts);
|
||||
return CMD_OK;
|
||||
}
|
||||
|
||||
int
|
||||
get_date(const char* opts)
|
||||
{
|
||||
sprintf(opts, "%s\n", __DATE__);
|
||||
return CMD_OK;
|
||||
}
|
||||
|
||||
int
|
||||
date(const char* opts)
|
||||
{
|
||||
memset((const char*)dateout, '\0', sizeof(dateout));
|
||||
get_date(dateout);
|
||||
printf("%s\n", dateout);
|
||||
|
||||
return CMD_OK;
|
||||
}
|
||||
|
||||
int
|
||||
get_time(const char* opts)
|
||||
{
|
||||
sprintf(opts, "%s\n", __TIME__);
|
||||
return CMD_OK;
|
||||
}
|
||||
|
||||
int
|
||||
time2(const char* opts)
|
||||
{
|
||||
memset((const uint64_t*)timeout, '\0', sizeof(timeout));
|
||||
get_time(timeout);
|
||||
printf("%s\n", timeout);
|
||||
return CMD_OK;
|
||||
}
|
||||
|
||||
int
|
||||
list(const char* opts)
|
||||
{
|
||||
FILE* f = NULL;
|
||||
char buf[16] = { 0 };
|
||||
char dev[16] = { 0 };
|
||||
|
||||
strncpy(buf, right(opts, (int)strlen(opts) - 6), strlen(opts) - 2);
|
||||
if(strlen(buf) == 2)
|
||||
return CMD_ARGS;
|
||||
|
||||
printf("%s\n", buf);
|
||||
if(strcmp(buf, "all") == 0) {
|
||||
badl(NULL);
|
||||
return CMD_OK;
|
||||
}
|
||||
|
||||
printf("Adding new device %s to list.\n", buf);
|
||||
f = fopen("./list.dev", "w");
|
||||
if(f == NULL)
|
||||
return CMD_IOERR;
|
||||
|
||||
do {
|
||||
scanf("%15s", dev);
|
||||
if(strcmp(dev, ".") != 0)
|
||||
fwrite(&dev, strlen(dev), 1, f);
|
||||
fprintf(f, "\n");
|
||||
} while(strlen(dev) > 1);
|
||||
|
||||
if(f) fclose(f);
|
||||
|
||||
return CMD_OK;
|
||||
}
|
||||
|
||||
int
|
||||
loglevel(const char* opts)
|
||||
{
|
||||
int size;
|
||||
char** splitresult = split((const char*)opts, ' ', &size);
|
||||
|
||||
if(size == 1) {
|
||||
printf("Current log level: %i\n", user.loglevel);
|
||||
free(splitresult);
|
||||
return CMD_OK;
|
||||
}
|
||||
|
||||
int m = atoi(splitresult[1]);
|
||||
if((m < 0) && m > 4)
|
||||
return CMD_ARGS;
|
||||
|
||||
user.loglevel = m;
|
||||
free(splitresult);
|
||||
|
||||
return CMD_OK;
|
||||
}
|
||||
|
||||
int
|
||||
filet(const char* opts)
|
||||
{
|
||||
return CMD_OK;
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning( pop )
|
||||
#endif
|
||||
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
#include "intrinsic.h"
|
||||
|
||||
extern struct user_ctx user;
|
||||
extern int writeconfig();
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning( push )
|
||||
#pragma warning( disable : 4090 )
|
||||
#pragma warning( disable : 594 )
|
||||
#endif
|
||||
|
||||
int
|
||||
help(const char* opts)
|
||||
{
|
||||
int size;
|
||||
int retcode;
|
||||
char** splitresult = split(opts, ' ', &size);
|
||||
|
||||
if(size == 1)
|
||||
retcode = find_help_section(opts);
|
||||
else
|
||||
retcode = find_help_section(splitresult[1]);
|
||||
|
||||
free(&splitresult[0]);
|
||||
|
||||
return retcode;
|
||||
}
|
||||
|
||||
int
|
||||
find_help_section(const char* section)
|
||||
{
|
||||
bool found = false;
|
||||
FILE* fp = NULL;
|
||||
char cfg_path[MAXBUF];
|
||||
char chunk[MAXBUF];
|
||||
|
||||
memset(cfg_path, 0, sizeof(cfg_path));
|
||||
get_userdir(cfg_path);
|
||||
#ifndef _MSC_VER
|
||||
strcat(cfg_path, "/.interp.hlp");
|
||||
#else
|
||||
strcat(cfg_path, "\\.interp.hlp");
|
||||
#endif
|
||||
|
||||
fp = fopen(cfg_path, "r");
|
||||
if(!fp) return CMD_FILEEXST;
|
||||
|
||||
while(fgets(chunk, sizeof(chunk), fp) != NULL) {
|
||||
int n = strncmp(section, chunk, strlen(section));
|
||||
|
||||
if(n == 0) {
|
||||
replace_char(chunk, '|', '\n');
|
||||
fputs(chunk, stdout);
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
|
||||
if((!found) && strncmp(section, "?", (size_t)1) != 0)
|
||||
printf("Keyword not found\n");
|
||||
|
||||
if(fp) fclose(fp);
|
||||
|
||||
return CMD_OK;
|
||||
}
|
||||
|
||||
int
|
||||
motd(const char* string)
|
||||
{
|
||||
char cfg_path[MAXBUF];
|
||||
char mot_path[MAXBUF];
|
||||
|
||||
if(strlen(string) < 3)
|
||||
return CMD_ARGS;
|
||||
|
||||
if(strchr(string, '"') == NULL)
|
||||
return CMD_QUOTES;
|
||||
|
||||
memset(cfg_path, 0, sizeof(cfg_path));
|
||||
get_userdir(cfg_path);
|
||||
strcpy(mot_path, cfg_path);
|
||||
|
||||
remove_first(mot_path, ".interp.ini");
|
||||
#ifndef _MSC_VER
|
||||
strcat(mot_path, "/.motd");
|
||||
#else
|
||||
strcat(mot_path, "\\.motd");
|
||||
#endif
|
||||
char newm[MAXMOTD];
|
||||
memset(newm, '\0', MAXMOTD);
|
||||
mid(string, 6, find_ch_index(string, '"'), newm, strlen(string));
|
||||
|
||||
remove_first(newm, "\"");
|
||||
encode(mot_path, newm);
|
||||
|
||||
return CMD_OK;
|
||||
}
|
||||
|
||||
int
|
||||
prompt(const char* opts)
|
||||
{
|
||||
char *buf;
|
||||
|
||||
int n = countc(opts, '\"');
|
||||
if (n < 2)
|
||||
return CMD_ARGS;
|
||||
|
||||
substr(opts, '\"', &buf);
|
||||
memset(user.prompt, 0, strlen(user.prompt));
|
||||
strcpy(user.prompt, buf);
|
||||
|
||||
free(buf);
|
||||
|
||||
return CMD_OK;
|
||||
}
|
||||
|
||||
int
|
||||
quit(const char* opts)
|
||||
{
|
||||
return CMD_QUIT;
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning( pop )
|
||||
#endif
|
||||
|
||||
+272
@@ -0,0 +1,272 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
#include "utils/config.h"
|
||||
#include "intrinsic.h"
|
||||
#include "support.h"
|
||||
#include "parser.h"
|
||||
#include "error_info.h"
|
||||
#include "logger.h"
|
||||
|
||||
extern const int noCmds;
|
||||
extern int help(const char*);
|
||||
extern int StartLogger(int);
|
||||
extern bool proc_cmds(char**, int);
|
||||
struct user_ctx user;
|
||||
char str[1024];
|
||||
|
||||
#define MAX_LINE 1024
|
||||
|
||||
/**
|
||||
* @file main.c
|
||||
* @author Tim O'Neil
|
||||
* @date 3/9/2018
|
||||
*
|
||||
* @brief Probably pretty portable CLI interpreter
|
||||
*
|
||||
* @section DESCRIPTION test
|
||||
*
|
||||
* Alternate command line interpreter prototype.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Anticipate ctrl + c
|
||||
*
|
||||
* no signals we need to handle as far as we
|
||||
* know right now. May change in the future.
|
||||
*/
|
||||
void
|
||||
sig_handler(int sign)
|
||||
{
|
||||
signal(SIGINT, sig_handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* buffer sanitizer, on hold for now
|
||||
*/
|
||||
static int
|
||||
sanitize_buf()
|
||||
{
|
||||
static char ok_chars[] = "abcdefghijklmnopqrstuvwxyz"
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
"1234567890_-.@";
|
||||
|
||||
char user_data[] = "Bad char 1:} Bad char 2:{";
|
||||
char *cp = user_data; // Cursor into string
|
||||
const char *end = user_data + strlen(user_data);
|
||||
|
||||
for (cp += strspn(cp, ok_chars); cp != end; cp += strspn(cp, ok_chars))
|
||||
*cp = '_';
|
||||
|
||||
return CMD_OK;
|
||||
}
|
||||
|
||||
static void
|
||||
reverse(char s[])
|
||||
{
|
||||
size_t i, j;
|
||||
char c;
|
||||
|
||||
for (i = 0, j = (int)strlen(s) - 1; i < j; i++, j--) {
|
||||
c = s[i];
|
||||
s[i] = s[j];
|
||||
s[j] = c;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
myitoa(int n, char s[])
|
||||
{
|
||||
size_t i, sign;
|
||||
|
||||
if ((sign = n) < 0) /* record sign */
|
||||
n = -n; /* make n positive */
|
||||
i = 0;
|
||||
do { /* generate digits in reverse order */
|
||||
s[i++] = n % 10 + '0'; /* get next digit */
|
||||
} while ((n /= 10) > 0); /* delete it */
|
||||
if (sign < 0)
|
||||
s[i++] = '-';
|
||||
s[i] = '\0';
|
||||
reverse(s);
|
||||
}
|
||||
|
||||
/**
|
||||
* initial state
|
||||
*
|
||||
* set defaults including prompt which is a "> " (greater than + space), can be changed
|
||||
* via the 'prompt' intrinsic command.
|
||||
*/
|
||||
static int
|
||||
init(void)
|
||||
{
|
||||
char tmp[16] = { 0 };
|
||||
memset(user.prompt, 0, sizeof(user.prompt));
|
||||
strcpy(tmp, get_keyvalue("prompt", ""));
|
||||
|
||||
if(strlen(tmp) == 0)
|
||||
strcpy(tmp, get_keyvalue("prompt", ">"));
|
||||
|
||||
#ifndef _MSC_VER
|
||||
int len2 = (int)strlen(tmp);
|
||||
tmp[len2 - 1] = '\0';
|
||||
#endif
|
||||
char banner_path[MAXBUF];
|
||||
char cfg_path[MAXBUF];
|
||||
signal(SIGINT, sig_handler);
|
||||
memset(cfg_path, 0, sizeof(cfg_path));
|
||||
|
||||
memset(banner_path, 0, sizeof(banner_path));
|
||||
get_userdir(cfg_path);
|
||||
strcpy(banner_path, cfg_path);
|
||||
|
||||
#ifndef _MSC_VER
|
||||
strcat(banner_path, "/.motd");
|
||||
strcat(cfg_path, "/.interp.ini");
|
||||
#else
|
||||
strcat(banner_path, "\\.motd");
|
||||
strcat(cfg_path, "\\.interp.ini");
|
||||
#endif
|
||||
|
||||
if (access(banner_path, 0) == 0) {
|
||||
decode(banner_path);
|
||||
printf("\n");
|
||||
}
|
||||
return CMD_OK;
|
||||
}
|
||||
|
||||
static int
|
||||
readconfig(void)
|
||||
{
|
||||
user.admin = 0;
|
||||
char adminperm[10] = { 0 };
|
||||
char cfg_path[MAXBUF] = { 0 };
|
||||
|
||||
get_userdir(cfg_path);
|
||||
#ifndef _MSC_VER
|
||||
strcat(cfg_path, "/.interp.ini");
|
||||
#else
|
||||
strcat(cfg_path, "\\.interp.ini");
|
||||
#endif
|
||||
ini_table_s* config = ini_table_create();
|
||||
if(ini_table_read_from_file(config, cfg_path)) {
|
||||
|
||||
strcpy(user.prompt, ini_table_get_entry(config, "Main", "prompt"));
|
||||
user.loglevel = atoi(ini_table_get_entry(config, "Main", "loglevel"));
|
||||
strcpy(adminperm, ini_table_get_entry(config, "Main", "admin"));
|
||||
|
||||
if(strcmp(adminperm, "true") == 0)
|
||||
user.admin = 1;
|
||||
}
|
||||
ini_table_destroy(config);
|
||||
|
||||
return CMD_OK;
|
||||
}
|
||||
|
||||
static int
|
||||
writeconfig(void)
|
||||
{
|
||||
char cfg_path[MAXBUF] = { 0 };
|
||||
char astr[10] = { 0 };
|
||||
get_userdir(cfg_path);
|
||||
|
||||
#ifndef _MSC_VER
|
||||
strcat(cfg_path, "/.interp.ini");
|
||||
#else
|
||||
strcat(cfg_path, "\\.interp.ini");
|
||||
#endif
|
||||
|
||||
ini_table_s* config = ini_table_create();
|
||||
if(ini_table_read_from_file(config, cfg_path)) {
|
||||
|
||||
ini_table_create_entry(config, "Main", "prompt", user.prompt);
|
||||
myitoa(user.loglevel, astr);
|
||||
ini_table_create_entry(config, "Main", "loglevel", astr);
|
||||
|
||||
if(user.admin == 1)
|
||||
ini_table_create_entry(config, "Main", "admin", "true");
|
||||
else
|
||||
ini_table_create_entry(config, "Main", "admin", "false");
|
||||
|
||||
ini_table_write_to_file(config, cfg_path);
|
||||
}
|
||||
|
||||
ini_table_destroy(config);
|
||||
|
||||
return CMD_OK;
|
||||
}
|
||||
|
||||
#ifdef _MSV_VER
|
||||
char*
|
||||
get_cmdln(void)
|
||||
{
|
||||
char ch;
|
||||
int i = 0, flag = 0;
|
||||
|
||||
for (i = 0; i < MAX_LINE && flag == 0; ++i) {
|
||||
ch = _getch();
|
||||
switch (ch) {
|
||||
case 13:
|
||||
str[i] = '\0';
|
||||
flag = 1;
|
||||
break;
|
||||
|
||||
case '\b':
|
||||
if (i > 0) i--;
|
||||
str[i--] = '\0';
|
||||
printf("\b \b");
|
||||
break;
|
||||
|
||||
default:
|
||||
str[i] = ch;
|
||||
printf("%c", ch);
|
||||
}
|
||||
}
|
||||
str[strlen(str)] = '\0';
|
||||
|
||||
return &str[0];
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* main.c, pretty self explanitory I hope.
|
||||
* Will be changed to take command line options eventually.
|
||||
*/
|
||||
int
|
||||
main(int argc, char** argv)
|
||||
{
|
||||
size_t size, len;
|
||||
bool bDo = true;
|
||||
//char* cmd_string = NULL;
|
||||
|
||||
init();
|
||||
readconfig();
|
||||
|
||||
do {
|
||||
len = 0;
|
||||
char* cmd_string = NULL;
|
||||
printf("%s ", user.prompt);
|
||||
|
||||
#ifdef _MSV_VER
|
||||
cmd_string = get_cmdln();
|
||||
#else
|
||||
getline(&cmd_string, &len, stdin);
|
||||
#endif
|
||||
size = (size_t)strlen(cmd_string);
|
||||
char** splitresult = split(cmd_string, ' ', &size);
|
||||
bDo = proc_cmds(splitresult, size);
|
||||
|
||||
free(&splitresult[0]);
|
||||
} while (bDo);
|
||||
|
||||
writeconfig();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
Executable
+141
@@ -0,0 +1,141 @@
|
||||
#include "threadpool.h"
|
||||
|
||||
ThreadPool*
|
||||
thread_pool_init(size_t num_threads)
|
||||
{
|
||||
ThreadPool* pool = malloc(sizeof(ThreadPool));
|
||||
if(!pool) {
|
||||
perror("Memory allocation failed for thread pool");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (mtx_init(&pool->lock, mtx_plain) != thrd_success) {
|
||||
perror("Mutex initialization failed");
|
||||
free(pool);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (cnd_init(&pool->condition) != thrd_success) {
|
||||
perror("Condition variable initialization failed");
|
||||
mtx_destroy(&pool->lock);
|
||||
|
||||
free(pool);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pool->task_queue = NULL;
|
||||
pool->num_threads = num_threads > 0 ? num_threads : DEFAULT_THREAD_POOL_SIZE;
|
||||
pool->shutdown = false;
|
||||
|
||||
pool->threads = malloc(pool->num_threads * sizeof(thrd_t));
|
||||
if (!pool->threads) {
|
||||
perror("Memory allocation failed for thread pool threads");
|
||||
|
||||
mtx_destroy(&pool->lock);
|
||||
cnd_destroy(&pool->condition);
|
||||
free(pool);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < pool->num_threads; ++i) {
|
||||
if (thrd_create(&pool->threads[i], thread_function, pool) != thrd_success) {
|
||||
perror("Thread creation failed");
|
||||
|
||||
thread_pool_destroy(pool);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return pool;
|
||||
}
|
||||
|
||||
void
|
||||
thread_pool_destroy(ThreadPool* pool)
|
||||
{
|
||||
if (!pool)
|
||||
return;
|
||||
|
||||
mtx_lock(&pool->lock);
|
||||
pool->shutdown = true;
|
||||
mtx_unlock(&pool->lock);
|
||||
|
||||
cnd_broadcast(&pool->condition);
|
||||
|
||||
for (size_t i = 0; i < pool->num_threads; ++i) {
|
||||
thrd_join(pool->threads[i], NULL);
|
||||
printf("Thread destroyed\n");
|
||||
}
|
||||
|
||||
free(pool->threads);
|
||||
mtx_destroy(&pool->lock);
|
||||
cnd_destroy(&pool->condition);
|
||||
|
||||
free(pool);
|
||||
}
|
||||
|
||||
bool
|
||||
thread_pool_enqueue(ThreadPool* pool, void (*function)(void*), void* arg)
|
||||
{
|
||||
if (!pool || !function)
|
||||
return false;
|
||||
|
||||
Task* new_task = malloc(sizeof(Task));
|
||||
if (!new_task) {
|
||||
perror("Memory allocation failed for new task");
|
||||
return false;
|
||||
}
|
||||
|
||||
new_task->function = function;
|
||||
new_task->arg = arg;
|
||||
new_task->next = NULL;
|
||||
|
||||
mtx_lock(&pool->lock);
|
||||
|
||||
if (pool->task_queue == NULL)
|
||||
pool->task_queue = new_task;
|
||||
else {
|
||||
Task* last_task = pool->task_queue;
|
||||
while (last_task->next != NULL)
|
||||
last_task = last_task->next;
|
||||
|
||||
last_task->next = new_task;
|
||||
}
|
||||
|
||||
cnd_signal(&pool->condition);
|
||||
mtx_unlock(&pool->lock);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int
|
||||
thread_function(void* arg)
|
||||
{
|
||||
ThreadPool* pool = (ThreadPool*)arg;
|
||||
while (true) {
|
||||
mtx_lock(&pool->lock);
|
||||
|
||||
while (!pool->task_queue && !pool->shutdown)
|
||||
cnd_wait(&pool->condition, &pool->lock);
|
||||
|
||||
|
||||
if (pool->shutdown) {
|
||||
mtx_unlock(&pool->lock);
|
||||
return thrd_success;
|
||||
}
|
||||
|
||||
Task* task = pool->task_queue;
|
||||
if (task) {
|
||||
pool->task_queue = task->next;
|
||||
}
|
||||
|
||||
mtx_unlock(&pool->lock);
|
||||
|
||||
if (task) {
|
||||
task->function(task->arg);
|
||||
free(task);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,300 @@
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
#define strcasecmp _stricmp
|
||||
#define strncasecmp _strnicmp
|
||||
#else
|
||||
#include <strings.h>
|
||||
#endif
|
||||
#include "utils/config.h"
|
||||
|
||||
static void print_log(int line, const char* msg, const char* extra)
|
||||
{
|
||||
printf("TConfig [Line: %i]: ", line);
|
||||
printf(msg, extra);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
static ini_entry_s* _ini_entry_create(ini_section_s* section,
|
||||
const char* key, const char* value)
|
||||
{
|
||||
if ((section->size % 10) == 0) {
|
||||
section->entry =
|
||||
realloc(section->entry, (10+section->size) * sizeof(ini_entry_s));
|
||||
}
|
||||
ini_entry_s* entry = §ion->entry[section->size++];
|
||||
entry->key = malloc((strlen(key)+1)*sizeof(char));
|
||||
entry->value = malloc((strlen(value)+1)*sizeof(char));
|
||||
|
||||
strcpy(entry->key, key);
|
||||
strcpy(entry->value, value);
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
static ini_section_s* _ini_section_create(ini_table_s* table,
|
||||
const char* section_name)
|
||||
{
|
||||
if ((table->size % 10) == 0) {
|
||||
table->section =
|
||||
realloc(table->section, (10+table->size) * sizeof(ini_section_s));
|
||||
}
|
||||
ini_section_s* section = &table->section[table->size++];
|
||||
section->size = 0;
|
||||
section->name = malloc((strlen(section_name)+1) * sizeof(char));
|
||||
|
||||
strcpy(section->name, section_name);
|
||||
section->entry = malloc(10 * sizeof(ini_entry_s));
|
||||
|
||||
return section;
|
||||
}
|
||||
|
||||
static ini_section_s* _ini_section_find(ini_table_s* table, const char* name)
|
||||
{
|
||||
for (int i = 0; i < table->size; i++) {
|
||||
if (strcmp(table->section[i].name, name) == 0) {
|
||||
return &table->section[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static ini_entry_s* _ini_entry_find(ini_section_s* section, const char* key)
|
||||
{
|
||||
for (int i = 0; i < section->size; i++) {
|
||||
if (strcmp(section->entry[i].key, key) == 0) {
|
||||
return §ion->entry[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static ini_entry_s* _ini_entry_get(ini_table_s* table, const char* section_name,
|
||||
const char* key)
|
||||
{
|
||||
ini_section_s* section = _ini_section_find(table, section_name);
|
||||
if (section == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ini_entry_s* entry = _ini_entry_find(section, key);
|
||||
if (entry == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
ini_table_s* ini_table_create()
|
||||
{
|
||||
ini_table_s* table = malloc(sizeof(ini_table_s));
|
||||
table->size = 0;
|
||||
table->section = malloc(10 * sizeof(ini_section_s));
|
||||
|
||||
return table;
|
||||
}
|
||||
|
||||
void ini_table_destroy(ini_table_s* table)
|
||||
{
|
||||
for (int i = 0; i < table->size; i++) {
|
||||
ini_section_s* section = &table->section[i];
|
||||
|
||||
for (int q = 0; q < section->size; q++) {
|
||||
ini_entry_s* entry = §ion->entry[q];
|
||||
free(entry->key);
|
||||
|
||||
free(entry->value);
|
||||
}
|
||||
free(section->entry);
|
||||
free(section->name);
|
||||
}
|
||||
free(table->section);
|
||||
free(table);
|
||||
}
|
||||
|
||||
bool ini_table_read_from_file(ini_table_s* table, const char* file)
|
||||
{
|
||||
FILE* f = fopen(file, "r");
|
||||
if (f == NULL) return false;
|
||||
|
||||
enum {Section, Key, Value, Comment} state = Section;
|
||||
int c;
|
||||
int position = 0;
|
||||
int spaces = 0;
|
||||
int line = 0;
|
||||
int buffer_size = 128 * sizeof(char);
|
||||
char* buf = malloc(buffer_size);
|
||||
char* value = NULL;
|
||||
|
||||
ini_section_s* current_section = NULL;
|
||||
|
||||
memset(buf, '\0', buffer_size);
|
||||
while((c = getc(f)) != EOF) {
|
||||
if (position > buffer_size-2) {
|
||||
|
||||
buffer_size += 128 * sizeof(char);
|
||||
size_t value_offset = value == NULL ? 0 : value - buf;
|
||||
buf = realloc(buf, buffer_size);
|
||||
|
||||
memset(buf+position, '\0', buffer_size-position);
|
||||
if (value != NULL)
|
||||
value = buf + value_offset;
|
||||
}
|
||||
switch(c) {
|
||||
case ' ':
|
||||
switch(state) {
|
||||
case Value: if (value[0] != '\0') spaces++; break;
|
||||
default: if (buf[0] != '\0') spaces++; break;
|
||||
}
|
||||
break;
|
||||
case ';':
|
||||
if (state == Value) {
|
||||
buf[position++] = c;
|
||||
break;
|
||||
}
|
||||
else {
|
||||
state = Comment;
|
||||
buf[position++] = c;
|
||||
|
||||
while (c != EOF && c != '\n') {
|
||||
c = getc(f);
|
||||
if (c != EOF && c != '\n') buf[position++] = c;
|
||||
}
|
||||
}
|
||||
// fallthrough
|
||||
case '\n':
|
||||
// fallthrough
|
||||
case EOF:
|
||||
line++;
|
||||
if (state == Value) {
|
||||
if (current_section == NULL) {
|
||||
current_section = _ini_section_create(table, "");
|
||||
}
|
||||
_ini_entry_create(current_section, buf, value);
|
||||
value = NULL;
|
||||
} else if (state == Comment) {
|
||||
if (current_section == NULL) {
|
||||
current_section = _ini_section_create(table, "");
|
||||
}
|
||||
_ini_entry_create(current_section, buf, "");
|
||||
} else if (state == Section) {
|
||||
print_log(line, "Section `%s' missing `]' operator.", buf);
|
||||
} else if(state == Key && position) {
|
||||
print_log(line, "Key `%s' missing `=' operator.", buf);
|
||||
}
|
||||
memset(buf, '\0', buffer_size);
|
||||
state = Key;
|
||||
position = 0;
|
||||
spaces = 0;
|
||||
break;
|
||||
case '[':
|
||||
state = Section;
|
||||
break;
|
||||
case ']':
|
||||
current_section = _ini_section_create(table, buf);
|
||||
memset(buf, '\0', buffer_size);
|
||||
position = 0;
|
||||
spaces = 0;
|
||||
state = Key;
|
||||
break;
|
||||
case '=':
|
||||
if (state == Key) {
|
||||
state = Value;
|
||||
buf[position++] = '\0';
|
||||
value = buf + position;
|
||||
spaces = 0;
|
||||
continue;
|
||||
}
|
||||
default:
|
||||
for(;spaces > 0; spaces--) buf[position++] = ' ';
|
||||
buf[position++] = c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
free(buf);
|
||||
fclose(f);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ini_table_write_to_file(ini_table_s* table, const char* file)
|
||||
{
|
||||
FILE* f = fopen(file, "w+");
|
||||
if (f == NULL) return false;
|
||||
for (int i = 0; i < table->size; i++) {
|
||||
ini_section_s* section = &table->section[i];
|
||||
fprintf(f, i > 0 ? "\n[%s]\n" : "[%s]\n", section->name);
|
||||
for (int q = 0; q < section->size; q++) {
|
||||
ini_entry_s* entry = §ion->entry[q];
|
||||
if (entry->key[0] == ';') {
|
||||
fprintf(f, "%s\n", entry->key);
|
||||
} else {
|
||||
fprintf(f, "%s = %s\n", entry->key, entry->value);
|
||||
}
|
||||
}
|
||||
}
|
||||
fclose(f);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void ini_table_create_entry(ini_table_s* table, const char* section_name,
|
||||
const char* key, const char* value)
|
||||
{
|
||||
ini_section_s* section = _ini_section_find(table, section_name);
|
||||
if (section == NULL) {
|
||||
section = _ini_section_create(table, section_name);
|
||||
}
|
||||
ini_entry_s* entry = _ini_entry_find(section, key);
|
||||
if (entry == NULL) {
|
||||
entry = _ini_entry_create(section, key, value);
|
||||
}else {
|
||||
free(entry->value);
|
||||
entry->value = malloc((strlen(value)+1) * sizeof(char));
|
||||
strcpy(entry->value, value);
|
||||
}
|
||||
}
|
||||
|
||||
bool ini_table_check_entry(ini_table_s* table, const char* section_name,
|
||||
const char* key)
|
||||
{
|
||||
return (_ini_entry_get(table, section_name, key) != NULL);
|
||||
}
|
||||
|
||||
const char* ini_table_get_entry(ini_table_s* table, const char* section_name,
|
||||
const char* key)
|
||||
{
|
||||
ini_entry_s* entry = _ini_entry_get(table, section_name, key);
|
||||
if (entry == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
return entry->value;
|
||||
}
|
||||
|
||||
bool ini_table_get_entry_as_int(ini_table_s* table, const char* section_name,
|
||||
const char* key, int* value)
|
||||
{
|
||||
const char* val = ini_table_get_entry(table, section_name, key);
|
||||
if (val == NULL) {
|
||||
return false;
|
||||
}
|
||||
*value = atoi(val);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ini_table_get_entry_as_bool(ini_table_s* table, const char* section_name,
|
||||
const char* key, bool* value)
|
||||
{
|
||||
const char* val = ini_table_get_entry(table, section_name, key);
|
||||
if (val == NULL) {
|
||||
return false;
|
||||
}
|
||||
if (strcasecmp(val, "on") == 0 || strcasecmp(val, "true") == 0) {
|
||||
*value = true;
|
||||
}else {
|
||||
*value = false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,232 @@
|
||||
#include "utils/filesys.h"
|
||||
#include "utils/config.h"
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning( push )
|
||||
#pragma warning( disable : 594 )
|
||||
#pragma warning( disable : 4047 )
|
||||
#pragma warning( disable : 4024 )
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* @brief find_cfgfile search for ini in a few predefined locations
|
||||
*
|
||||
* @details make three attempts to locate the cfg file requested
|
||||
* using three different strategies (/etc, $HOME, and
|
||||
* the home for the current user using the passwd
|
||||
* database), errors out with an integer code if
|
||||
* finally unsuccessful. If the bUseDot is set the
|
||||
* search will make the file name a dot file during
|
||||
* search in the user directories
|
||||
*
|
||||
* @param const string file_name: name of cfg file to search for
|
||||
* @param string path_found: result of successful search
|
||||
* @param boolean make it a dot file flag
|
||||
* @return integer status code, path_found is NULLed if unsuccessful
|
||||
*/
|
||||
#ifndef _MSC_VER
|
||||
int
|
||||
find_cfgfile(const char* file_name, char* path_found, bool bUseDot)
|
||||
{
|
||||
if(file_name == NULL && path_found == NULL)
|
||||
return CMD_ARGS;
|
||||
|
||||
char search_this_path[MAXSIZE];
|
||||
char* sp = &search_this_path[0];
|
||||
strcpy(sp, "/etc");
|
||||
// Search /etc first for a plain,
|
||||
// un-decorated (no dot prefix)
|
||||
// file_name
|
||||
memset(sp, 0, strlen(search_this_path));
|
||||
sp = strcpy(search_this_path, "/etc");
|
||||
sp = strcat(search_this_path, "/");
|
||||
|
||||
sp = strcat(sp, file_name);
|
||||
|
||||
if(access(sp, F_OK) != -1 ) {
|
||||
strcpy(path_found, sp);
|
||||
return CMD_OK;
|
||||
}
|
||||
// then try for whatever is in $HOME,
|
||||
// if bUseDot == true prefix the file
|
||||
// name with a dot.
|
||||
char* homedir = getenv("HOME");
|
||||
if(homedir != NULL) {
|
||||
memset(sp, 0, strlen(search_this_path));
|
||||
|
||||
strcpy(sp, homedir);
|
||||
strcat(sp, "/");
|
||||
if(bUseDot) strcat(sp, ".");
|
||||
|
||||
strcat(sp, file_name);
|
||||
if(access(sp, F_OK) != -1 ) {
|
||||
strcpy(path_found, sp);
|
||||
|
||||
return CMD_OK;
|
||||
}
|
||||
}
|
||||
// Finally try the user's directory as
|
||||
// specified in the passwd data base
|
||||
uid_t uid = getuid();
|
||||
struct passwd* pw = getpwuid(uid);
|
||||
if(pw != NULL) {
|
||||
|
||||
memset(sp, 0, strlen(search_this_path));
|
||||
strcpy(sp, pw->pw_dir);
|
||||
strcat(sp, "/");
|
||||
|
||||
if(bUseDot) strcat(sp, ".");
|
||||
strcat(sp, file_name);
|
||||
if(access(search_this_path, F_OK) != -1 ) {
|
||||
|
||||
strcpy(path_found, sp);
|
||||
return CMD_OK;
|
||||
}
|
||||
}
|
||||
// nothing worked, set the result string
|
||||
// param to null and return an error code
|
||||
strcpy(path_found, "\0");
|
||||
|
||||
return CMD_FILENF;
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief create_cfgfile create a new ini file
|
||||
*
|
||||
* @details copies the passed parameter to a buffer to
|
||||
* protect against file names larger than 256
|
||||
* chars.
|
||||
*
|
||||
* @param const char* the full path of the new file
|
||||
* @return integer status code
|
||||
*/
|
||||
int
|
||||
create_cfgfile(const char* file_path)
|
||||
{
|
||||
char cfg_path[MAXBUF];
|
||||
memset(cfg_path, 0, sizeof(cfg_path));
|
||||
get_userdir(cfg_path);
|
||||
|
||||
#ifndef _MSC_VER
|
||||
strcat(cfg_path, "/.interp.ini");
|
||||
#else
|
||||
strcat(cfg_path, "\\.interp.ini");
|
||||
#endif
|
||||
|
||||
ini_table_s* config = ini_table_create();
|
||||
if(!ini_table_read_from_file(config, cfg_path)) {
|
||||
// create new table
|
||||
|
||||
ini_table_create_entry(config, "Main", "prompt", "> ");
|
||||
ini_table_create_entry(config, "Main", "loglevel", "0");
|
||||
ini_table_create_entry(config, "Main", "admin", "false");
|
||||
ini_table_write_to_file(config, cfg_path);
|
||||
}
|
||||
|
||||
ini_table_destroy(config);
|
||||
|
||||
return CMD_OK;
|
||||
}
|
||||
|
||||
#ifndef _MSC_VER
|
||||
void
|
||||
get_userdir(char* path)
|
||||
{
|
||||
uid_t uid = getuid();
|
||||
struct passwd* pw = getpwuid(uid);
|
||||
strcpy(path, pw->pw_dir);
|
||||
}
|
||||
#else
|
||||
void
|
||||
get_userdir(char* path)
|
||||
{
|
||||
wchar_t wpath[MAXBUF * 2] = { 0 };
|
||||
|
||||
if (FAILED(SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, wpath)))
|
||||
path = NULL;
|
||||
else
|
||||
wcstombs(path, wpath, MAXBUF);
|
||||
}
|
||||
#endif
|
||||
|
||||
int
|
||||
write_motd(char* path, const char* motd)
|
||||
{
|
||||
char newm[MAXMOTD];
|
||||
memset(newm, '\0', MAXMOTD);
|
||||
mid(motd, 6, find_ch_index(motd, '"'), newm, strlen(motd));
|
||||
|
||||
remove_first(newm, "\"");
|
||||
FILE* fp = fopen(path, "w");
|
||||
if (fp == NULL) return CMD_ERR;
|
||||
|
||||
fprintf(fp, "%s", newm);
|
||||
fclose(fp);
|
||||
|
||||
return CMD_OK;
|
||||
}
|
||||
|
||||
int
|
||||
read_motd(const char* path)
|
||||
{
|
||||
FILE* stream;
|
||||
char line[MAXMOTD];
|
||||
int errno;
|
||||
|
||||
errno = CMD_OK;
|
||||
if((stream = fopen(path, "r")) != NULL) {
|
||||
if (NULL != fgets(&line, MAXMOTD, stream))
|
||||
errno = CMD_FILEEXST;
|
||||
fclose(stream);
|
||||
}
|
||||
return errno;
|
||||
}
|
||||
|
||||
const char*
|
||||
get_keyvalue(const char* key, const char* def)
|
||||
{
|
||||
//struct config_setting_t const *setting;
|
||||
char cfg_path[MAXBUF];
|
||||
memset(cfg_path, 0, sizeof(cfg_path));
|
||||
|
||||
get_userdir(cfg_path);
|
||||
#ifndef _MSC_VER
|
||||
strcat(cfg_path, "/.interp.ini");
|
||||
#else
|
||||
strcat(cfg_path, "\\.interp.ini");
|
||||
#endif
|
||||
|
||||
if(!file_exists(cfg_path))
|
||||
create_cfgfile(cfg_path);
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
int
|
||||
set_keyvalue(const char* key, const char* value)
|
||||
{
|
||||
char cfg_path[MAXBUF];
|
||||
memset(cfg_path, 0, sizeof(cfg_path));
|
||||
get_userdir(cfg_path);
|
||||
#ifndef _MSC_VER
|
||||
strcat(cfg_path, "/.interp.ini");
|
||||
#else
|
||||
strcat(cfg_path, "\\.interp.ini");
|
||||
#endif
|
||||
|
||||
return CMD_OK;
|
||||
}
|
||||
|
||||
bool
|
||||
file_exists(const char* filename)
|
||||
{
|
||||
struct stat buffer;
|
||||
return (stat (filename, &buffer) == 0);
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning( pop )
|
||||
#endif
|
||||
|
||||
Executable
+378
@@ -0,0 +1,378 @@
|
||||
#define __STDC_WANT_LIB_EXT1__ 1
|
||||
|
||||
#include "utils/huff.h"
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* @file huff.c
|
||||
* @author Tim O'Neil
|
||||
* @date 3/9/2018
|
||||
*
|
||||
* @brief Probably pretty portable huffman coder
|
||||
*
|
||||
* @section DESCRIPTION
|
||||
*
|
||||
* Huffman coder for making text blobs psuedo-encrypted
|
||||
* and psuedo-compressed and editable only through this
|
||||
* the application (more or less). This code is not
|
||||
* tested with other implementations, so its totally
|
||||
* possible to be a wonky, one-off, not comatible, non-
|
||||
* standard implementation (if there is such a thing.)
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief encode coder entrypoint, this one of our public facing
|
||||
* interfaces, the other being decode, below.
|
||||
*
|
||||
* @details Given a buffer of text encode it and write the output
|
||||
* to a file.
|
||||
* @param const char* file_name2: path of output file
|
||||
* @param char* buf: string containing text to encode
|
||||
*/
|
||||
void
|
||||
encode(const char* file_name2, char* buf)
|
||||
{
|
||||
FILE* en;
|
||||
int freq[256] = {0};
|
||||
info header[256];
|
||||
|
||||
char codes[256][40];
|
||||
char code[40];
|
||||
strcpy(code, "");
|
||||
|
||||
List_t mylist_en;
|
||||
size_t num, no_of_chars;
|
||||
en = fopen(file_name2, "w");
|
||||
|
||||
if(en) {
|
||||
find_freq(buf, freq);
|
||||
no_of_chars = strlen(buf);
|
||||
|
||||
if(no_of_chars > 0) {
|
||||
make_tree(&mylist_en, freq);
|
||||
num = create_header(freq, (int)no_of_chars, header);
|
||||
|
||||
find_code(mylist_en.head, code, codes);
|
||||
compress(buf, en, codes, (int)no_of_chars, header, freq, (int)num);
|
||||
}
|
||||
}
|
||||
fclose(en);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief decode: decode a buffer of huffman encoded text
|
||||
*
|
||||
* @details Decode a file and send the output to stdout
|
||||
*
|
||||
* @param const char*: file_name1: path of input file
|
||||
*/
|
||||
void
|
||||
decode(const char* file_name1)
|
||||
{
|
||||
info y, * ptrrr;
|
||||
FILE* en;
|
||||
ptrrr = &y;
|
||||
|
||||
code_generated x, * ptrr;
|
||||
ptrr = &x;
|
||||
List_t mylist_de;
|
||||
|
||||
int i, num, no_of_chars, count = 0;
|
||||
int freq[256] = {0};
|
||||
en = fopen(file_name1, "r");
|
||||
|
||||
if(en) {
|
||||
fread(ptrr, sizeof(code_generated), 1, en);
|
||||
num = ptrr->c;
|
||||
|
||||
for(i = 0; i < num; ++i) {
|
||||
fread(ptrrr, sizeof(info), 1, en);
|
||||
freq[(int)ptrrr->ch] = ptrrr->freq;
|
||||
}
|
||||
|
||||
make_tree(&mylist_de, freq);
|
||||
no_of_chars = find_no_of_chars(freq);
|
||||
node_t* root = mylist_de.head;
|
||||
|
||||
node_t* ptr = root;
|
||||
while(fread(ptrr, sizeof(code_generated), 1, en) == 1)
|
||||
ptr = decode_char(stdout, ptr, ptrr->c, 1, root, &count, no_of_chars);
|
||||
}
|
||||
fclose(en);
|
||||
}
|
||||
|
||||
node_t*
|
||||
decode_char(FILE* de, node_t* ptr, int x, int y,
|
||||
node_t* root, int* count, int no_of_chars)
|
||||
{
|
||||
if(y != 9 &&(*count) != no_of_chars) {
|
||||
if(ptr->llink == 0 && ptr->rlink == 0) {
|
||||
fputc(ptr->c, de);
|
||||
|
||||
ptr = root;
|
||||
++(*count);
|
||||
ptr = decode_char(de, ptr, x, y, root, count, no_of_chars);
|
||||
}
|
||||
else if(x % 2 == 0 && ptr->llink)
|
||||
ptr = decode_char(de, ptr->llink, x / 2, ++y, root, count, no_of_chars);
|
||||
else if(x%2!=0 && ptr->rlink)
|
||||
ptr = decode_char(de, ptr->rlink, x / 2, ++y, root, count, no_of_chars);
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void
|
||||
insert_in_list(List_t*, node_t*);
|
||||
|
||||
void
|
||||
init_list(List_t* ptr)
|
||||
{
|
||||
ptr->head = 0;
|
||||
}
|
||||
|
||||
void
|
||||
disp_list(const List_t* ptr)
|
||||
{
|
||||
node_t* temp = ptr->head;
|
||||
while(temp) {
|
||||
printf("%d( %c )\t",temp->freq,temp->c);
|
||||
temp = temp->link;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void
|
||||
make_node(List_t* ptr, int n, char c)
|
||||
{
|
||||
node_t* temp;
|
||||
temp = (node_t*)malloc(sizeof(node_t));
|
||||
temp->freq = n;
|
||||
|
||||
temp->c = c;
|
||||
temp->link = 0;
|
||||
temp->rlink = 0;
|
||||
|
||||
temp->llink = 0;
|
||||
insert_in_list(ptr, temp);
|
||||
//free(temp);
|
||||
}
|
||||
|
||||
void
|
||||
insert_in_list(List_t* ptr, node_t* t)
|
||||
{
|
||||
if(ptr->head == 0)
|
||||
ptr->head = t;
|
||||
else {
|
||||
node_t* prev = 0;
|
||||
node_t* pres = ptr->head;
|
||||
|
||||
while(pres && pres->freq <= t->freq) {
|
||||
prev = pres;
|
||||
pres = pres->link;
|
||||
}
|
||||
if(pres == ptr->head) {
|
||||
ptr->head = t;
|
||||
t->link = pres;
|
||||
}
|
||||
else if(pres == 0)
|
||||
prev->link = t;
|
||||
else {
|
||||
t->link = pres;
|
||||
prev->link = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
make_list(List_t* ptr, int* freq)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < 256; ++i) {
|
||||
if(freq[i] != 0)
|
||||
make_node(ptr, freq[i], i);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
find_freq(char* fp, int* freq)
|
||||
{
|
||||
char c;
|
||||
unsigned n = 0;
|
||||
|
||||
while(n < strlen(fp)) {
|
||||
c = fp[n++];
|
||||
++freq[(int)c];
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
make_tree(List_t* ptr, int* freq)
|
||||
{
|
||||
node_t * t1, * t2;
|
||||
init_list(ptr);
|
||||
make_list(ptr,freq);
|
||||
|
||||
while(ptr->head->link != 0) {
|
||||
t1=ptr->head;
|
||||
t2=ptr->head->link;
|
||||
|
||||
ptr->head=t2->link;
|
||||
t1->link = 0;
|
||||
t2->link = 0;
|
||||
|
||||
node_t* temp;
|
||||
temp=(node_t*)malloc(sizeof(node_t));
|
||||
temp->freq = t1->freq + t2->freq;
|
||||
|
||||
temp->c = 0;
|
||||
temp->link = 0;
|
||||
temp->rlink = t2;
|
||||
|
||||
temp->llink = t1;
|
||||
insert_in_list(ptr, temp);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
find_code(node_t* ptr, char* code, char codes[256][40])
|
||||
{
|
||||
char temp[40];
|
||||
if(ptr->rlink == 0 && ptr->llink == 0)
|
||||
strcpy(codes[(int)(ptr->c)], code);
|
||||
|
||||
if(ptr->rlink != 0) {
|
||||
strcpy(temp, code);
|
||||
strcat(temp, "1");
|
||||
|
||||
find_code(ptr->rlink, temp, codes);
|
||||
}
|
||||
if(ptr->llink != 0) {
|
||||
strcpy(temp, code);
|
||||
strcat(temp, "0");
|
||||
|
||||
find_code(ptr->llink, temp, codes);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
find_no_of_chars(int* freq)
|
||||
{
|
||||
int i, sum = 0;
|
||||
for(i = 0; i < 256; ++i)
|
||||
sum = sum + freq[i];
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
void
|
||||
compress(char* fp, FILE* en, char codes[256][40],
|
||||
int no_of_chars, info* header, int* freq, int num)
|
||||
{
|
||||
char ch;
|
||||
int n = 0, i = 0, j = 0, count = 0, k, l;
|
||||
unsigned char c;
|
||||
code_generated x;
|
||||
char string_rev[9];
|
||||
x.c = num;
|
||||
|
||||
fwrite(&x, sizeof(code_generated), 1 ,en);
|
||||
for(k = 0, l = 0; k < 256; ++k) {
|
||||
if(freq[k]) {
|
||||
fwrite(&header[l], sizeof(info), 1, en);
|
||||
++l;
|
||||
}
|
||||
}
|
||||
while((ch = fp[n++]) != EOF) {
|
||||
j = 0;
|
||||
++count;
|
||||
|
||||
while(codes[(int)ch][j] != '\0') {
|
||||
string_rev[i] = codes[(int)ch][j];
|
||||
++i; ++j;
|
||||
|
||||
if(i == 8) {
|
||||
string_rev[i] = '\0';
|
||||
strre(string_rev, (int)strlen(string_rev));
|
||||
|
||||
c = convert_string_char(string_rev);
|
||||
strcpy(string_rev, "");
|
||||
i = 0; x.c = c;
|
||||
|
||||
fwrite(&x,sizeof(x),1,en);
|
||||
}
|
||||
if(count == no_of_chars ) {
|
||||
while(codes[(int)ch][j]) {
|
||||
string_rev[i] = codes[(int)ch][j];
|
||||
|
||||
++i; ++j;
|
||||
if(i == 8) {
|
||||
string_rev[i] = '\0';
|
||||
|
||||
strre(string_rev, (int)strlen(string_rev));
|
||||
c = convert_string_char(string_rev);
|
||||
strcpy(string_rev, "");
|
||||
|
||||
i = 0; x.c=c;
|
||||
fwrite(&x,sizeof(x),1,en);
|
||||
}
|
||||
}
|
||||
string_rev[i] = '\0';
|
||||
strre(string_rev, (int)strlen(string_rev));
|
||||
c = convert_string_char(string_rev);
|
||||
|
||||
strcpy(string_rev, "");
|
||||
x.c = c;
|
||||
fwrite(&x, sizeof(x), 1, en);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
strre(char* str, int n)
|
||||
{
|
||||
int i = 0, j = n - 1;
|
||||
char temp;
|
||||
|
||||
while(i < j) {
|
||||
temp = str[i];
|
||||
str[i] = str[j];
|
||||
str[j] = temp;
|
||||
++i; --j;
|
||||
}
|
||||
str[n]='\0';
|
||||
}
|
||||
|
||||
int
|
||||
create_header(int* freq, int no_of_chars, info* header)
|
||||
{
|
||||
int i, j;
|
||||
|
||||
for(i = 0, j = 0; i < 256; ++i) {
|
||||
if(freq[i] != 0) {
|
||||
header[j].freq = freq[i];
|
||||
header[j].ch = i;
|
||||
++j;
|
||||
}
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
||||
unsigned char
|
||||
convert_string_char(char string[8])
|
||||
{
|
||||
int i=0;
|
||||
unsigned char c = 0;
|
||||
|
||||
while(string[i]) {
|
||||
if(string[i] == '1') {
|
||||
c = c * 2;
|
||||
c = c + 1;
|
||||
}
|
||||
else
|
||||
c = c * 2;
|
||||
|
||||
++i;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
Reference in new issue
Block a user