start adding low level functionality to help with future development
This commit is contained in:
@@ -1,21 +0,0 @@
|
||||
#include "cloneString.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char* cloneString(const char* str) {
|
||||
if (str == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t len = strlen(str);
|
||||
char* clone = malloc((len + 1) * sizeof(char));
|
||||
|
||||
if (clone == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
strcpy(clone, str);
|
||||
return clone;
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
#ifndef CLONESTRING_H
|
||||
#define CLONESTRING_H
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
char* cloneString(const char* str);
|
||||
|
||||
#endif // CLONESTRING_H
|
||||
82
src/hashmap/hashmap.c
Normal file
82
src/hashmap/hashmap.c
Normal file
@@ -0,0 +1,82 @@
|
||||
#include "hashmap.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
struct table *createTable(int size)
|
||||
{
|
||||
struct table *t = (struct table *)malloc(sizeof(struct table));
|
||||
t->size = size;
|
||||
t->list = (struct node **)malloc(sizeof(struct node *) * size);
|
||||
int i;
|
||||
for (i = 0; i < size; i++)
|
||||
t->list[i] = NULL;
|
||||
return t;
|
||||
}
|
||||
|
||||
int hashCode(struct table *t, int key)
|
||||
{
|
||||
if (key < 0)
|
||||
return -(key % t->size);
|
||||
return key % t->size;
|
||||
}
|
||||
|
||||
int remove(struct table *t, int key)
|
||||
{
|
||||
int pos = hashCode(t, key);
|
||||
struct node *list = t->list[pos];
|
||||
struct node *temp = list;
|
||||
struct node *prev = NULL;
|
||||
while (temp)
|
||||
{
|
||||
if (temp->key == key)
|
||||
{
|
||||
if (prev)
|
||||
prev->next = temp->next;
|
||||
else
|
||||
t->list[pos] = temp->next;
|
||||
|
||||
free(temp);
|
||||
return 1;
|
||||
}
|
||||
prev = temp;
|
||||
temp = temp->next;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void insert(struct table *t, int key, void* val)
|
||||
{
|
||||
int pos = hashCode(t, key);
|
||||
struct node *list = t->list[pos];
|
||||
struct node *newNode = (struct node *)malloc(sizeof(struct node));
|
||||
struct node *temp = list;
|
||||
while (temp)
|
||||
{
|
||||
if (temp->key == key)
|
||||
{
|
||||
temp->val = val;
|
||||
return;
|
||||
}
|
||||
temp = temp->next;
|
||||
}
|
||||
newNode->key = key;
|
||||
newNode->val = val;
|
||||
newNode->next = list;
|
||||
t->list[pos] = newNode;
|
||||
}
|
||||
|
||||
void *lookup(struct table *t, int key)
|
||||
{
|
||||
int pos = hashCode(t, key);
|
||||
struct node *list = t->list[pos];
|
||||
struct node *temp = list;
|
||||
while (temp)
|
||||
{
|
||||
if (temp->key == key)
|
||||
{
|
||||
return temp->val;
|
||||
}
|
||||
temp = temp->next;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
24
src/hashmap/hashmap.h
Normal file
24
src/hashmap/hashmap.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef HASHMAP_H
|
||||
#define HASHMAP_H
|
||||
|
||||
struct node
|
||||
{
|
||||
int key;
|
||||
void *val;
|
||||
struct node *next;
|
||||
};
|
||||
struct table
|
||||
{
|
||||
int size;
|
||||
struct node **list;
|
||||
};
|
||||
|
||||
struct table *createTable(int size);
|
||||
|
||||
int hashCode(struct table *t, int key);
|
||||
|
||||
int remove(struct table *t, int key);
|
||||
|
||||
void insert(struct table *t, int key, void* val);
|
||||
|
||||
#endif // HASHMAP_H
|
||||
21
src/main.c
21
src/main.c
@@ -1,10 +1,16 @@
|
||||
#include "cloneString/cloneString.h"
|
||||
#include "string/string.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int main() {
|
||||
const char* str = "Hello, World!";
|
||||
char* str = malloc(100 * sizeof(char));
|
||||
if (str == NULL) {
|
||||
printf("Failed to allocate memory\n");
|
||||
return 1;
|
||||
}
|
||||
strcpy(str, " \t\n\r\f\vHello, World! \t\n\r\f\v");
|
||||
char* clone = cloneString(str);
|
||||
|
||||
if (clone == NULL) {
|
||||
@@ -12,10 +18,15 @@ int main() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Original string: %s\n", str);
|
||||
printf("Cloned string: %s\n", clone);
|
||||
stripString(clone, WHITE_SPACE);
|
||||
|
||||
printf("Original string: \"%s\"\n", str);
|
||||
|
||||
free(str);
|
||||
|
||||
|
||||
printf("Cloned string: \"%s\"\n", clone);
|
||||
free(clone);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
74
src/string/string.c
Normal file
74
src/string/string.c
Normal file
@@ -0,0 +1,74 @@
|
||||
#include "string.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
const char *WHITE_SPACE = " \t\n\r\f\v";
|
||||
|
||||
char *cloneString(char *str)
|
||||
{
|
||||
if (str == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t len = strlen(str);
|
||||
char *clone = malloc((len + 1) * sizeof(char));
|
||||
|
||||
if (clone == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
strcpy(clone, str);
|
||||
return clone;
|
||||
}
|
||||
|
||||
void stripString(char *str, const char *chars)
|
||||
{
|
||||
if (str == NULL || chars == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
size_t len = strlen(str);
|
||||
size_t charsLen = strlen(chars);
|
||||
|
||||
if (len == 0 || charsLen == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
size_t i = 0;
|
||||
while (i < len)
|
||||
{
|
||||
if (strchr(chars, str[i]) == NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
if (i > 0)
|
||||
{
|
||||
memmove(str, str + i, len - i + 1);
|
||||
}
|
||||
size_t j = len-i - 1;
|
||||
while (j > 0)
|
||||
{
|
||||
if (strchr(chars, str[j]) == NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
j--;
|
||||
}
|
||||
|
||||
if (j < len)
|
||||
{
|
||||
str[j + 1] = '\0';
|
||||
}
|
||||
|
||||
str = realloc(str, (j + 1) * sizeof(char));
|
||||
|
||||
return;
|
||||
}
|
||||
10
src/string/string.h
Normal file
10
src/string/string.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef CLONESTRING_H
|
||||
#define CLONESTRING_H
|
||||
|
||||
extern const char * WHITE_SPACE;
|
||||
|
||||
char* cloneString(char* str);
|
||||
|
||||
void stripString(char* str, const char* chars);
|
||||
|
||||
#endif // CLONESTRING_H
|
||||
6
src/translate/ArObject/ArObject.c
Normal file
6
src/translate/ArObject/ArObject.c
Normal file
@@ -0,0 +1,6 @@
|
||||
|
||||
struct ArObject
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
0
src/translate/ArObject/ArObject.h
Normal file
0
src/translate/ArObject/ArObject.h
Normal file
0
src/translate/ArString/ArString.c
Normal file
0
src/translate/ArString/ArString.c
Normal file
Reference in New Issue
Block a user