First commit, everything finished and ready.
This commit is contained in:
83
Makefile
Normal file
83
Makefile
Normal file
@@ -0,0 +1,83 @@
|
||||
# GNU/Linux specific Make directives.
|
||||
|
||||
# Declare tools.
|
||||
SHELL = /bin/sh
|
||||
CC = clang
|
||||
LD = clang
|
||||
ECHO = @echo
|
||||
|
||||
CFLAGS = -std=c17 -Wall -Wextra -O0 -Wno-unused-variable -Wno-unused-parameter -I./include
|
||||
LDFLAGS =
|
||||
|
||||
EXECUTABLE = inverT
|
||||
|
||||
SOURCES = main.c functions.c
|
||||
OBJECTS = $(SOURCES:.c=.o)
|
||||
CLEANFILES = main.o functions.o
|
||||
|
||||
|
||||
|
||||
# To get a fully verbose make output do declare 'AT' at the command line like so: 'make AT='.
|
||||
# By default AT is undefined and thus assigned the string '@' which suppresses output from commands.
|
||||
AT ?= @
|
||||
|
||||
# Colors and efects
|
||||
#######################################
|
||||
|
||||
NORMAL = \033[m
|
||||
|
||||
FG_BLACK = \033[30m
|
||||
FG_DKRED = \033[31m
|
||||
FG_DKGREEN = \033[32;7m
|
||||
FG_DKYELLOW = \033[33m
|
||||
FG_DKBLUE = \033[34m
|
||||
FG_DKMAGENTA = \033[35m
|
||||
FG_DKCYAN = \033[36m
|
||||
FG_GRAY = \033[37m
|
||||
FG_DKGRAY = \033[30;1m
|
||||
FG_REDFULL = \033[31;7m
|
||||
FG_GREEN = \033[32;1m
|
||||
FG_YELLOW = \033[33;7m
|
||||
FG_BLUE = \033[34;7m
|
||||
FG_MAGENTA = \033[35;1m
|
||||
FG_CYAN = \033[36;1m
|
||||
FG_WHITE = \033[37;1m
|
||||
|
||||
|
||||
BG_BLACK = \033[40m
|
||||
BG_DKRED = \033[41m
|
||||
BG_DKGREEN = \033[42m
|
||||
BG_DKYELLOW = \033[43m
|
||||
BG_DKBLUE = \033[44m
|
||||
BG_DKMAGENTA = \033[45m
|
||||
BG_DKCYAN = \033[46m
|
||||
BG_GRAY = \033[47m
|
||||
BG_DKGRAY = \033[40;1m
|
||||
BG_RED = \033[41;1m
|
||||
BG_GREEN = \033[42;1m
|
||||
BG_YELLOW = \033[43;1m
|
||||
BG_BLUE = \033[44;1m
|
||||
BG_MAGENTA = \033[45;1m
|
||||
BG_CYAN = \033[46;1m
|
||||
BG_WHITE = \033[47;1m
|
||||
|
||||
##########################################
|
||||
|
||||
.SUFFIXES: .c .o
|
||||
.PHONY: clean
|
||||
|
||||
# Compile
|
||||
all: clean $(EXECUTABLE)
|
||||
|
||||
.c.o:
|
||||
$(ECHO) "[$(FG_BLUE)COMPILING$(NORMAL)] $@"
|
||||
$(AT) $(CC) $(CFLAGS) -o $*.o -c $<
|
||||
|
||||
$(EXECUTABLE): $(OBJECTS)
|
||||
$(ECHO) "[$(FG_YELLOW)LINKING$(NORMAL)] $(EXECUTABLE)"
|
||||
$(AT) $(LD) $(LDFLAGS) $(OBJECTS) -o $(EXECUTABLE)
|
||||
$(ECHO) "[$(FG_DKGREEN)SUCCESS$(NORMAL)] Run ./$(EXECUTABLE) to start your application, Enjoy :)"
|
||||
|
||||
clean:
|
||||
$(ECHO) "[$(FG_REDFULL)CLEANED$(NORMAL)] $(CLEANFILES)"
|
||||
$(AT) rm -rf $(CLEANFILES)
|
43
functions.c
Normal file
43
functions.c
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <main.h>
|
||||
|
||||
int ask_file() {
|
||||
|
||||
printf ("Insert name of the file to be read : ");
|
||||
fgets(info.filename, info.fileNameSize, stdin);
|
||||
info.filename[strlen(info.filename)-1] = '\0';
|
||||
invert_file();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int get_file(char *fileName) {
|
||||
|
||||
strcpy(info.filename, fileName);
|
||||
invert_file();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int invert_file(void) {
|
||||
|
||||
if ((info.fd= fopen(info.filename, "r")) != NULL){
|
||||
int ft, i = 0;
|
||||
|
||||
fseek(info.fd, 0, SEEK_END);
|
||||
|
||||
ft = ftell(info.fd);
|
||||
while(i < ft)
|
||||
{
|
||||
i++;
|
||||
fseek(info.fd, -i, SEEK_END);
|
||||
printf("%c", fgetc(info.fd));
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
fclose(info.fd);
|
||||
}
|
||||
|
||||
else {
|
||||
perror(info.filename);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
28
include/main.h
Normal file
28
include/main.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef __CHCOUNT_H__
|
||||
#define __CHCOUNT_H__
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
//Function declarations
|
||||
int ask_file(); // It has no flags, it just prompts for filename
|
||||
int get_file(char *fileName); // Gets the filename passed on has an argument
|
||||
int invert_file(void); // Invert the file stored in the variable info.filename
|
||||
|
||||
|
||||
//This are general parameters for the program
|
||||
struct parameters {
|
||||
|
||||
char filename[100]; // Filename where the text is
|
||||
FILE *fd; // File descriptor for the text file
|
||||
char version[5]; // Holds the software version
|
||||
int fileNameSize; // File maximum name size
|
||||
char help[]; // Help menu to be printed on screen
|
||||
};
|
||||
|
||||
struct parameters info;
|
||||
|
||||
#endif
|
65
main.c
Normal file
65
main.c
Normal file
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
This program inverts the caracters in a
|
||||
given text file, using command line arguments
|
||||
or by prompting the question to the user.
|
||||
|
||||
By Joni Silva, 27/05/2022.
|
||||
|
||||
*/
|
||||
|
||||
#include <main.h>
|
||||
|
||||
|
||||
// Necessary to initalize my struct with all the info
|
||||
struct parameters info = {
|
||||
.filename = "EMPTY",
|
||||
.fd = NULL,
|
||||
.version = "1.0",
|
||||
.fileNameSize = 80,
|
||||
// Below is the variable that olds the text to dislpay for help
|
||||
.help = "\n\
|
||||
------------------------------------------------\n\
|
||||
Usage: ./chcount -c [<some.text.file>]\n\
|
||||
-help Print help and exit.\n\
|
||||
-v Print application version and exit.\n\
|
||||
-i Invert text caracters.\n\
|
||||
Example: ./chcount -c test.txt\n",
|
||||
};
|
||||
|
||||
|
||||
int main (int argc, char *argv[]) {
|
||||
|
||||
|
||||
if (argc == 1) {
|
||||
ask_file();
|
||||
}
|
||||
|
||||
else if (argc == 2) {
|
||||
if (strncmp(argv[1], "-help", 5) == 0) {
|
||||
printf("%s\n", info.help);
|
||||
}
|
||||
else if (strncmp(argv[1], "-v", 2) == 0) {
|
||||
printf("Version %s\n", info.version);
|
||||
}
|
||||
else {
|
||||
get_file(argv[1]);
|
||||
}
|
||||
}
|
||||
|
||||
else if (argc == 3) {
|
||||
if(strncmp(argv[1], "-i", 2) == 0) {
|
||||
get_file(argv[2]);
|
||||
}
|
||||
else {
|
||||
printf("\nInvalid flag!!!");
|
||||
printf("%s\n", info.help);
|
||||
}
|
||||
}
|
||||
|
||||
else {
|
||||
printf("\nTo many parameters!!!");
|
||||
printf("%s\n", info.help);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user