Skip to content

Instantly share code, notes, and snippets.

View jshardy's full-sized avatar

Joshua Hardy jshardy

View GitHub Profile
@jshardy
jshardy / BST.c
Last active February 26, 2019 09:44
Binary Search Tree
// Binary Search Tree Node
#pragma once
template <typename T>
class BST_Tree;
template <typename T>
class BST_Node
{
public:
@jshardy
jshardy / PadString.c
Created February 26, 2019 09:30
PadString
/**********************************************************************
* Function: padString(char *string, size_t size, fillchar)
* Purpose: padString for writing specific data file
* Precondition: pass string, total string size desired, and fillchar
* Postcondition: string is padded with fillchar
************************************************************************/
void padString(char *string, size_t size, char fillchar)
{
size_t i = 0;
while(*string != 0)
@jshardy
jshardy / CreateProcess-RedictOutput.c
Last active February 26, 2019 09:25
Create Process/Redirect Output
pid_t create_process(char *cmd, int file_input, int file_output)
{
pid_t pid = -1;
char **array_args = NULL;
pid = fork();
if(pid == 0)
{
//child
@jshardy
jshardy / RemoveControlCharacters
Created February 26, 2019 09:15
Remove Control Characters
/**********************************************************************
* Function: void remove_control_characters(char *str_line)
* implemented for good measure
* Purpose: remove all control characters from a string
* Precondition: pass a string
* Postcondition: all control characters removed from string
************************************************************************/
void remove_control_characters(char *str_line)
{
if(str_line != NULL)
@jshardy
jshardy / CompStringFromEnd.c
Created February 26, 2019 09:14
Chomp String From End with Char
void ChompStringFromEnd(char *string, char chompChar)
{
int len = strlen(string);
while(string[len--] != chompChar && len >= 0)
{
if(string[len] == chompChar)
{
string[len + 1] = 0;
break;
}
@jshardy
jshardy / GetToken.c
Last active February 26, 2019 09:24
GetToken Array
char **GetTokens(char *tokens, const char *delim)
{
char *index = NULL;
char **cmdArgs = NULL;
int argCount = 0;
index = strtok(tokens, delim);
while(index)
{
cmdArgs = (char**)realloc(cmdArgs, sizeof(char*) * (argCount + 1));
@jshardy
jshardy / ForkExec.c
Created February 26, 2019 09:13
Fork Exec
// Returns proccess return value
int CreateProcess(char **cmdArgs)
{
pid_t pid = 0;
int rval = 0;
pid = fork();
// if pid == 0 then I'm in child
if(pid == 0)
@jshardy
jshardy / Trim.c
Created February 26, 2019 09:12
Trim
/**********************************************************************
* Function: trim_string(" my string ")
* Purpose: trims spaces from front and back.
* Precondition: a string pointer
* Postcondition: string without leading or following spaces
************************************************************************/
void trim_string(char *string)
{
left_trim(string);
right_trim(string);
@jshardy
jshardy / TerminalColorDefines.c
Created February 25, 2019 22:59
Linux Terminal Colors
#define COLOR_RED "\x1b[31m"
#define COLOR_GREEN "\x1b[32m"
#define COLOR_YELLOW "\x1b[33m"
#define COLOR_BLUE "\x1b[34m"
#define COLOR_MAGENTA "\x1b[35m"
#define COLOR_CYAN "\x1b[36m"
#define COLOR_RESET "\x1b[0m"
printf(COLOR_RED "This is RED" COLOR_RESET);
@jshardy
jshardy / GetLine.c
Created February 25, 2019 22:54
GetLine()
char *GetLine()
{
size_t current_line_size = MAX_STR;
char *str_line = NULL;
char c = 0;
size_t index = 0;
str_line = malloc(sizeof(char) * current_line_size);
while((c = fgetc(stdin)) != '\n')
{