First commit

This commit is contained in:
Isaac Shoebottom 2023-09-15 23:55:52 -03:00
commit c997a29967
51 changed files with 595 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
# CLion Shit
cmake-build-*

8
Assignment1/.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

1
Assignment1/.idea/.name Normal file
View File

@ -0,0 +1 @@
Assignment1

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Assgn1.iml" filepath="$PROJECT_DIR$/.idea/Assgn1.iml" />
</modules>
</component>
</project>

View File

@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Assignment1" type="CMakeRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Assignment1" TARGET_NAME="Assignment1" CONFIG_NAME="Debug-WSL" RUN_TARGET_PROJECT_NAME="Assignment1" RUN_TARGET_NAME="Assignment1">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
</component>

View File

@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="For Testing" type="CMakeRunConfiguration" factoryName="Application" REDIRECT_INPUT="true" REDIRECT_INPUT_PATH="$PROJECT_DIR$/tests/exit.txt" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Assignment1" TARGET_NAME="Assignment1" CONFIG_NAME="Debug-WSL" RUN_TARGET_PROJECT_NAME="Assignment1" RUN_TARGET_NAME="Assignment1">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
</component>

View File

@ -0,0 +1,19 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Test" type="ShConfigurationType">
<option name="SCRIPT_TEXT" value="wsl ./test.sh" />
<option name="INDEPENDENT_SCRIPT_PATH" value="true" />
<option name="SCRIPT_PATH" value="" />
<option name="SCRIPT_OPTIONS" value="" />
<option name="INDEPENDENT_SCRIPT_WORKING_DIRECTORY" value="false" />
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$/tests" />
<option name="INDEPENDENT_INTERPRETER_PATH" value="true" />
<option name="INTERPRETER_PATH" value="" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="EXECUTE_IN_TERMINAL" value="true" />
<option name="EXECUTE_SCRIPT_FILE" value="false" />
<envs />
<method v="2">
<option name="RunConfigurationTask" enabled="false" run_configuration_name="Assignment1 For Test" run_configuration_type="CMakeRunConfiguration" run_configuration_target="CMakeBuildProfile:Debug-WSL" />
</method>
</configuration>
</component>

View File

@ -0,0 +1,100 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="ALL" />
</component>
<component name="CMakePresetLoader">{
&quot;useNewFormat&quot;: true
}</component>
<component name="CMakeReloadState">
<option name="reloaded" value="true" />
</component>
<component name="CMakeRunConfigurationManager">
<generated>
<config projectName="Assignment1" targetName="Assgn1" />
<config projectName="Assignment1" targetName="Assignment1" />
</generated>
</component>
<component name="CMakeSettings">
<configurations>
<configuration PROFILE_NAME="Debug-WSL" ENABLED="true" CONFIG_NAME="Debug" TOOLCHAIN_NAME="WSL" />
</configurations>
</component>
<component name="ChangeListManager">
<list default="true" id="cce10de5-af41-4b9e-8677-8aa7427730c2" name="Changes" comment="" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="ClangdSettings">
<option name="formatViaClangd" value="false" />
</component>
<component name="ExecutionTargetManager" SELECTED_TARGET="CMakeBuildProfile:Debug-WSL" />
<component name="ProjectApplicationVersion">
<option name="ide" value="CLion" />
<option name="majorVersion" value="2023" />
<option name="minorVersion" value="2.1" />
</component>
<component name="ProjectColorInfo">{
&quot;associatedIndex&quot;: 1
}</component>
<component name="ProjectId" id="2VLSCc4EW35DrGeyefadRoK0juQ" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"RunOnceActivity.cidr.known.project.marker": "true",
"WebServerToolWindowFactoryState": "false",
"cf.first.check.clang-format": "false",
"cidr.known.project.marker": "true",
"last_opened_file_path": "C:/Users/Isaac/OneDrive - University of New Brunswick/Year 4 UNB/CS3413/Assgn1/CMakeLists.txt",
"node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
"node.js.selected.package.tslint": "(autodetect)",
"settings.build.tools.auto.reload": "ALL",
"settings.editor.selected.configurable": "CPPToolchains",
"vue.rearranger.settings.migration": "true"
}
}]]></component>
<component name="RunManager">
<configuration default="true" type="CLionExternalRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true">
<method v="2">
<option name="CLION.EXTERNAL.BUILD" enabled="true" />
</method>
</configuration>
<configuration name="Assignment1" type="CMakeRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Assignment1" TARGET_NAME="Assignment1" CONFIG_NAME="Debug-WSL" RUN_TARGET_PROJECT_NAME="Assignment1" RUN_TARGET_NAME="Assignment1">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="cce10de5-af41-4b9e-8677-8aa7427730c2" name="Changes" comment="" />
<created>1694613741352</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1694613741352</updated>
<workItem from="1694613742699" duration="297000" />
<workItem from="1694614060478" duration="55000" />
<workItem from="1694614128674" duration="6986000" />
</task>
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" />
</component>
<component name="VCPKGProject">
<isAutomaticEditVcpkgJson value="false" />
<isAutomaticCheckingOnLaunch value="false" />
<isAutomaticFoundErrors value="true" />
<isAutomaticFoundErrors value="true" />
</component>
</project>

View File

@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.22)
project(Assignment1 C)
set(CMAKE_C_STANDARD 11)
include(CTest)
add_executable(
Assignment1
linked_list.h
linked_list.c
node.h
node.c
main.c
)

Binary file not shown.

98
Assignment1/linked_list.c Normal file
View File

@ -0,0 +1,98 @@
#include <stdio.h>
#include <stddef.h>
#include <stdbool.h>
#include <string.h>
#include "node.h"
#define MAX_LEN 100
Node *search(Node **head, char *data) {
Node *current = *head;
while (current != NULL) {
if (strcmp(current->data, data) == 0) {
return current;
}
current = current->next_elem;
}
return NULL;
}
Node *searchForPrevious(Node **head, Node *current) {
Node *previous = *head;
while (previous != NULL) {
if (previous->next_elem == current) {
return previous;
}
previous = previous->next_elem;
}
return NULL;
}
void add(Node **head, char *data) {
if (*head == NULL) {
*head = createNode(data);
replaceData(*head, data);
} else {
Node *current = *head;
while (current->next_elem != NULL) {
current = current->next_elem;
}
current->next_elem = createNode(data);
replaceData(current->next_elem, data);
}
}
bool delete(Node **head, char *data) {
if (*head == NULL) {
return false;
}
Node *result = search(head, data);
if (result == NULL) {
return false;
} else {
Node *previous = searchForPrevious(head, result);
if (previous == NULL) {
// If the node to be deleted is the head
Node *next = result->next_elem;
destroyNode(result);
*head = next;
} else {
// If the node to be deleted is not the head
previous->next_elem = result->next_elem;
destroyNode(result);
}
return true;
}
}
bool findAndReplace(Node **head, char *data, char *newData) {
Node *current = search(head, data);
if (current != NULL) {
replaceData(current, newData);
return true;
}
return false;
}
bool stop(Node **head) {
Node *current = *head;
while (current != NULL) {
Node *next = current->next_elem;
destroyNode(current);
current = next;
}
return true;
}
void printList(Node **head) {
Node *current = *head;
while (current != NULL) {
printf("%s\n", current->data);
current = current->next_elem;
}
}

25
Assignment1/linked_list.h Normal file
View File

@ -0,0 +1,25 @@
//
// Created by Isaac on 9/15/2023.
//
#ifndef ASSIGNMENT1_LINKED_LIST_H
#define ASSIGNMENT1_LINKED_LIST_H
#include <stdbool.h>
#include "node.h"
Node *search(Node **head, char *data);
Node *searchForPrevious(Node **head, Node *current);
void add(Node **head, char *data);
bool delete(Node **head, char *data);
bool findAndReplace(Node **head, char *data, char *newData);
void printList(Node **head);
bool stop(Node **head);
#endif //ASSIGNMENT1_LINKED_LIST_H

41
Assignment1/main.c Normal file
View File

@ -0,0 +1,41 @@
//
// Created by Isaac on 9/15/2023.
//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "linked_list.h"
#define MAX_LEN 100
int main() {
Node *head = NULL;
// Double buffer to support up to 100 chars in find and replace command
char buffer[MAX_LEN * 2];
bool stopLoop = false;
while (!stopLoop) {
fgets(buffer, MAX_LEN * 2, stdin);
buffer[strcspn(buffer, "\r\n")] = 0;
if (buffer[0] == 'a') {
buffer[MAX_LEN - 1] = '\0'; // Prevent buffer overflow
add(&head, buffer + 2);
} else if (buffer[0] == 'd') {
buffer[MAX_LEN - 1] = '\0'; // Prevent buffer overflow
delete(&head, buffer + 2);
} else if (buffer[0] == 'f') {
char *data = buffer + 2;
char input1[MAX_LEN], input2[MAX_LEN];
// sscanf assigns a null terminator to the end of the string, so don't need to do it manually
sscanf(data, "%99s %99s", input1, input2);
// Could check if the replacement was successful by using return value of function, but I don't have to
findAndReplace(&head, input1, input2);
} else if (buffer[0] == 'p') {
printList(&head);
} else if (buffer[0] == 's') {
stopLoop = stop(&head);
}
}
return EXIT_SUCCESS;
}

48
Assignment1/node.c Normal file
View File

@ -0,0 +1,48 @@
//
// Created by Isaac on 9/15/2023.
//
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "node.h"
//Assume that the string passed in is null terminated
Node *createNode(char *data) {
// Allocate memory for the node, and check if it was successful
Node *node = calloc(1, sizeof(Node));
if (node == NULL) {
printf("Error allocating memory for node\n");
exit(EXIT_FAILURE);
}
// Allocate memory for the data, and check if it was successful
node->data = calloc(strlen(data), sizeof(char));
if (node->data == NULL) {
printf("Error allocating memory for node data\n");
exit(EXIT_FAILURE);
}
// Copy data from the string passed in to the node's data
// Makes sure if the string passed in is changed, the node's data is not changed
// Also makes sure that if the data is on the stack, it is not freed at some other point
strcpy(node->data, data);
node->next_elem = NULL;
return node;
}
void destroyNode(Node *node) {
// Free the data first, then free the node
free(node->data);
free(node);
}
void replaceData(Node *node, char *data) {
// Free the data first, then allocate memory for the new data
free(node->data);
node->data = calloc(strlen(data), sizeof(char));
if (node->data == NULL) {
printf("Error allocating memory for replacing data\n");
exit(EXIT_FAILURE);
}
// Same rational as for createNode
strcpy(node->data, data);
}

18
Assignment1/node.h Normal file
View File

@ -0,0 +1,18 @@
//
// Created by Isaac on 9/15/2023.
//
#ifndef ASSIGNMENT1_NODE_H
#define ASSIGNMENT1_NODE_H
typedef struct node {
struct node *next_elem;
char *data;
} Node;
Node *createNode(char *data);
void destroyNode(Node *node);
void replaceData(Node *node, char *data);
#endif //ASSIGNMENT1_NODE_H

View File

@ -0,0 +1,4 @@
a test1
a test2
p
s

View File

@ -0,0 +1,2 @@
test1
test2

View File

@ -0,0 +1,2 @@
test1
test2

View File

@ -0,0 +1,5 @@
a test1
a test2
d test1
p
s

View File

@ -0,0 +1,3 @@
d hello
p
s

View File

View File

@ -0,0 +1 @@
test2

View File

@ -0,0 +1 @@
test2

View File

@ -0,0 +1 @@
s

View File

View File

View File

@ -0,0 +1,5 @@
a test1
a test2
f test1 test3
p
s

View File

@ -0,0 +1,2 @@
test3
test2

View File

@ -0,0 +1,2 @@
test3
test2

View File

@ -0,0 +1,5 @@
a hello
a hi
a hi
p
s

View File

@ -0,0 +1,3 @@
hello
hi
hi

View File

@ -0,0 +1,3 @@
hello
hi
hi

View File

@ -0,0 +1,11 @@
a hello
a hi
a hey
p
f hey yes
p
d hi
p
f hi maybe
p
s

View File

@ -0,0 +1,10 @@
hello
hi
hey
hello
hi
yes
hello
yes
hello
yes

View File

@ -0,0 +1,10 @@
hello
hi
hey
hello
hi
yes
hello
yes
hello
yes

36
Assignment1/tests/test.sh Normal file
View File

@ -0,0 +1,36 @@
#!/usr/bin/bash
function files() {
if [ ! -f "$1.txt" ]; then
echo "Creating $1.txt"
touch "$1.txt"
fi
if [ ! -f "$1_Expected.txt" ]; then
echo "Creating $1_Expected.txt"
touch "$1_Expected.txt"
fi
if [ ! -f "$1_Result.txt" ]; then
echo "Creating $1_Result.txt"
touch "$1_Result.txt"
fi
}
function test() {
# Check if the files exist and if not, create them
files "$1"
../cmake-build-debug-wsl/Assignment1 < "$1.txt" > "$1_Result.txt"
if diff -u "$1_Result.txt" "$1_Expected.txt";
then
echo "$1 test passed"
else
echo "$1 test failed"
fi
}
test "findAndReplace"
test "add"
test "delete"
test "pdf1"
test "pdf2"
test "exit"
test "deleteEmpty"

BIN
Lab1/Lab1.pdf Normal file

Binary file not shown.

BIN
Lab1/a.out Normal file

Binary file not shown.

4
Lab1/compile_script.sh Normal file
View File

@ -0,0 +1,4 @@
#!/bin/bash
gcc -Wall -Werror test.c
./a.out < input.txt > output.txt
diff correct_output.txt output.txt

1
Lab1/correct_output.txt Normal file
View File

@ -0,0 +1 @@
Hello

31
Lab1/history_backup.txt Normal file
View File

@ -0,0 +1,31 @@
108 15:06 pwd
109 15:06 mkdir "Lab 1"
110 15:06 cd Lab\ 1/
111 15:08 cd ..
112 15:08 rm Lab\ 1/
113 15:09 rmdir Lab\ 1/
114 15:09 mkdir lab1
115 15:09 mv lab1/ Lab1
116 15:09 cd D
117 15:09 cd Lab1/
118 15:10 touch test.c
123 15:14 nano test.c
124 15:14 gcc -Wall -Werror test.c
125 15:14 echo Hello world > input.txt
126 15:14 echo Hello planet >> input.txt
127 15:15 cat input.txt
128 15:15 echo Hello > correct_output.txt
129 15:15 ./a.out < input.txt
130 15:17 ./a.out < input.txt > output.txt
131 15:17 diff correct_output.txt output.txt
132 15:17 diff correct_output.txt incorrect
133 15:22 echo Hello world > incorrect_output.txt
134 15:22 diff correct_output.txt incorrect_output.txt
135 15:23 history | tail -n 20 > script.sh
136 15:23 nano script.sh
137 15:25 nano tmp.txt
138 15:25 cat tmp.txt script.sh > compile_script.sh
139 15:25 ls
141 15:26 chmod +x compile_script.sh
142 15:26 ./compile_script.sh
143 15:28 history > history_backup.txt

View File

@ -0,0 +1 @@
Hello world

2
Lab1/input.txt Normal file
View File

@ -0,0 +1,2 @@
Hello world
Hello planet

1
Lab1/output.txt Normal file
View File

@ -0,0 +1 @@
Hello

3
Lab1/script.sh Normal file
View File

@ -0,0 +1,3 @@
gcc -Wall -Werror test.c
./a.out < input.txt > output.txt
diff correct_output.txt output.txt

7
Lab1/test.c Normal file
View File

@ -0,0 +1,7 @@
#include<stdio.h>
int main(int argc, char** argv){
char temp[100];
scanf("%s", temp);
printf("%s\n", temp);
return 0;
}

1
Lab1/tmp.txt Normal file
View File

@ -0,0 +1 @@
#!/bin/bash

35
Lab1/to_submit.txt Normal file
View File

@ -0,0 +1,35 @@
108 15:06 pwd
109 15:06 mkdir "Lab 1"
110 15:06 cd Lab\ 1/
111 15:08 cd ..
112 15:08 rm Lab\ 1/
113 15:09 rmdir Lab\ 1/
114 15:09 mkdir lab1
115 15:09 mv lab1/ Lab1
116 15:09 cd D
117 15:09 cd Lab1/
118 15:10 touch test.c
123 15:14 nano test.c
124 15:14 gcc -Wall -Werror test.c
125 15:14 echo Hello world > input.txt
126 15:14 echo Hello planet >> input.txt
127 15:15 cat input.txt
128 15:15 echo Hello > correct_output.txt
129 15:15 ./a.out < input.txt
130 15:17 ./a.out < input.txt > output.txt
131 15:17 diff correct_output.txt output.txt
132 15:17 diff correct_output.txt incorrect
133 15:22 echo Hello world > incorrect_output.txt
134 15:22 diff correct_output.txt incorrect_output.txt
135 15:23 history | tail -n 20 > script.sh
136 15:23 nano script.sh
137 15:25 nano tmp.txt
138 15:25 cat tmp.txt script.sh > compile_script.sh
139 15:25 ls
141 15:26 chmod +x compile_script.sh
142 15:26 ./compile_script.sh
143 15:28 history > history_backup.txt
#!/bin/bash
gcc -Wall -Werror test.c
./a.out < input.txt > output.txt
diff correct_output.txt output.txt