Mercurial Hosting > dis
changeset 1:b82f21466dfd
You can just printf the file what are you doing!
author | VilyaemKenyaz |
---|---|
date | Fri, 25 Aug 2023 22:59:12 -0400 |
parents | e92d7e15bc33 |
children | 2b6ce8d53356 |
files | README.md basedfilelib.h c.ksh dis dis.c |
diffstat | 5 files changed, 149 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/README.md Fri Aug 25 22:44:59 2023 -0400 +++ b/README.md Fri Aug 25 22:59:12 2023 -0400 @@ -1,2 +1,2 @@ #dis - Displays a program, alternative to "cat", uses my based file library + Displays a file, alternative to "cat", uses my based file library
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/basedfilelib.h Fri Aug 25 22:59:12 2023 -0400 @@ -0,0 +1,129 @@ +// Based File Library +// +// Makes File I/O two functions, more like TempleOS, instead of *nix. + +#define MAX_LINES 100000 +#define MAXLENGTH 512 + +char* ReadFile(const char* fileName) { + FILE* file = fopen(fileName, "rb"); + if (file == NULL) { + perror("Error opening file"); + return NULL; + } + + fseek(file, 0, SEEK_END); + long fileSize = ftell(file); + rewind(file); + + char* fileContent = (char*)malloc(fileSize * sizeof(char)); + if (fileContent == NULL) { + perror("Error allocating memory for file content"); + fclose(file); + return NULL; + } + + size_t readSize = fread(fileContent, sizeof(char), fileSize, file); + if (readSize != fileSize) { + perror("Error reading file"); + fclose(file); + free(fileContent); + return NULL; + } + + fclose(file); + return fileContent; +} + +void WriteFile(const char* fileName, const char* content) { + FILE* file = fopen(fileName, "wb"); + if (file == NULL) { + perror("Error opening file"); + } + + size_t writeSize = fwrite(content, sizeof(char), strlen(content), file); + if (writeSize != strlen(content)) { + perror("Error writing to file"); + fclose(file); + } + + fclose(file); +} + + +char* DeleteLine(char* str, unsigned int line_number) { + char* lines[MAX_LINES]; + char* line; + unsigned int current_line = 0; + + line = strtok(str, "\n"); + while (line != NULL && current_line < MAX_LINES) { + lines[current_line++] = line; + line = strtok(NULL, "\n"); + } + + // Delete the specified line + if (line_number > 0 && line_number <= current_line) { + for (unsigned int i = line_number - 1; i < current_line - 1; i++) { + lines[i] = lines[i + 1]; + } + current_line--; + } + + // Reconstruct the string with remaining lines + str[0] = '\0'; + for (unsigned int i = 0; i < current_line; i++) { + strcat(str, lines[i]); + strcat(str, "\n"); + } + + return str; +} + + + +char* ReadLine(const char *str, unsigned int lineNum) { + int currentLine = 1; + size_t strLen = strlen(str); + char *line = NULL; + + line = malloc(strLen + 1); + if (line == NULL) { + fprintf(stderr, "Error: memory allocation failed.\n"); + exit(1); + } + + int lineStartIndex = 0; + int lineEndIndex = 0; + + // Traverse through the string until the desired line or the end of the string + while (currentLine <= lineNum && lineEndIndex < strLen) { + // Find the start index of the current line + while (lineStartIndex < strLen && (str[lineStartIndex] == '\n' || str[lineStartIndex] == '\r')) { + lineStartIndex++; + lineEndIndex++; + } + + // Find the end index of the current line + lineEndIndex = lineStartIndex; + while (lineEndIndex < strLen && str[lineEndIndex] != '\n' && str[lineEndIndex] != '\r') { + lineEndIndex++; + } + + // Check if the desired line is found + if (currentLine == lineNum) { + // Copy the line to the allocated memory + strncpy(line, str + lineStartIndex, lineEndIndex - lineStartIndex); + line[lineEndIndex - lineStartIndex] = '\0'; + break; + } + + // Move to the start of the next line + currentLine++; + lineStartIndex = lineEndIndex; + } + + return line; +} + +//BasedFileLib
--- a/c.ksh Fri Aug 25 22:44:59 2023 -0400 +++ b/c.ksh Fri Aug 25 22:59:12 2023 -0400 @@ -1,6 +1,6 @@ #!/bin/sh clear rm dis - cc dis -o dis + cc dis.c -o dis cp dis /usr/bin/ gdb -ex run dis
--- a/dis.c Fri Aug 25 22:44:59 2023 -0400 +++ b/dis.c Fri Aug 25 22:59:12 2023 -0400 @@ -1,2 +1,20 @@ //Displays a program, alternative to "cat", uses my based file library +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "basedfilelib.h" +void main(int argc, char* argv[]){ +if (argc == 1 ) { puts("Dis by William King at Peep Soft\nDisplays a file to stdout, better than cat at this purpose\nUsage: dis (file)");exit(0);} +puts("Displaying:"); +char * filetoread = argv[0]; +char * file = ReadFile(filetoread); +printf("%s\n",filetoread); +printf("%s\n",file); +while(strlen(file) != 0){ +char * print = ReadLine(file,1); +DeleteLine(file,1); +} +exit(0); +} +