Commit 3664bc59 authored by veronicamars73's avatar veronicamars73
Browse files

Adding aula 10 content

parents
PROG = diary
CC = g++
CPPFLAGS = -O0 -std=c++11 -g
OBJS = main.o Message.o Date.o Time.o Diary.o App.o Helpers.o
all: $(PROG)
$(PROG) : $(OBJS)
$(CC) $(OBJS) -o $(PROG)
main.o :
$(CC) $(CPPFLAGS) -c src/main.cpp
Message.o : include/Message.h
$(CC) $(CPPFLAGS) -c src/Message.cpp
Date.o : include/Date.h
$(CC) $(CPPFLAGS) -c src/Date.cpp
Time.o : include/Time.h
$(CC) $(CPPFLAGS) -c src/Time.cpp
Helpers.o : include/Helpers.h
$(CC) $(CPPFLAGS) -c src/Helpers.cpp
Diary.o : include/Diary.h
$(CC) $(CPPFLAGS) -c src/Diary.cpp
App.o : include/App.h
$(CC) $(CPPFLAGS) -c src/App.cpp
clean:
rm -f *.o
# Aula 10 de LP I
Projeto criado como método de avaliação de presença para a disciplina de *Linguagem de Programação I* no período de 2020.5.
# Informações Importantes
## Compilação
Compile o programa utilizando o comando `make` no terminal.
## Instruções de uso
O programa funciona como um diário.
- Para a adição de Mensagens use o comando
``` shell
$ ./diary add "Mensagem que se deseja adicionar"
```
ou
``` shell
$ ./diary add
< Digite sua mensagem >
```
- Para a listagem de mensagens do diário
``` shell
$ ./diary list
```
- Para a busca de mensagens no diário
``` shell
$ ./diary search "Termo de busca"
```
ou
``` shell
$ ./diary search
< Digite o termo de busca >
```
# Autoria
Programa desenvolvido por **Alaide Lisandra Melo Carvalho** (<mendie73@gmail.com>) como projeto para a disciplina de *Linguagem de Programação I* de 2020.5.
&copy; IMD/UFRN 2020.
File added
# 26/06/2020
- 18:30:05 more tests
- 18:37:22 more tests
- 18:37:39 more tests
- 18:43:24 more tests
# 29/06/2020
- 10:52:04 A message
- 11:24:10 Fearless tem aoty e red não
# 30/06/2020
- 09:52:24 Oh if you judge us we're all damned
#ifndef W5N_APP_H
#define W5N_APP_H
#include "Diary.h"
#include "Message.h"
#include <string>
struct App
{
Diary diary;
App(const std::string& filename);
int run(int argc, char* argv[]);
void add();
void add(const std::string message);
Message* search();
Message* search(const std::string looked);
void list_messages();
void print_message(Message message);
void result_search(Message* pointer);
};
#endif
#ifndef W5N_DATE_H
#define W5N_DATE_H
#include <string>
struct Date
{
unsigned year;
unsigned month;
unsigned day;
Date();
Date(unsigned year, unsigned month, unsigned day);
void set_from_string_date(const std::string& date);
std::string get_date_string();
};
#endif
#ifndef W5N_DIARY_H
#define W5N_DIARY_H
#include "Message.h"
#include <string>
struct Diary
{
Diary(const std::string& filename);
~Diary();
std::string filename;
Message* messages;
size_t message_size;
size_t message_max;
void add(const std::string& message);
void write();
Message* search(const std::string& message);
};
#endif
#ifndef W5N_HELPERS_H
#define W5N_HELPERS_H
#include <string>
#include "Date.h"
std::string format_current_date(const std::string& format);
std::string get_current_date();
std::string get_current_time();
int show_usage(std::string program);
int file_error();
int dateExists(std::ifstream &infile, Date date_to_test);
#endif
\ No newline at end of file
#ifndef W5N_MESSAGE_H
#define W5N_MESSAGE_H
#include "Date.h"
#include "Time.h"
#include <string>
struct Message
{
Message(std::string content,Date date,Time time);
std::string content;
Date date;
Time time;
Message();
};
#endif
#ifndef W5N_TIME_H
#define W5N_TIME_H
#include <string>
struct Time
{
unsigned hour;
unsigned minute;
unsigned second;
Time();
Time(unsigned hour, unsigned minute, unsigned second);
void set_from_string_time(const std::string& time);
std::string get_time_string();
};
#endif
#include "../include/App.h"
#include "../include/Date.h"
#include "../include/Time.h"
#include "../include/Helpers.h"
#include "../include/Message.h"
#include <iostream>
#include <string>
App::App(const std::string& filename) : diary(filename)
{
}
int App::run(int argc, char* argv[])
{
if (argc == 1) {
return show_usage(argv[0]);
}
std::string action = argv[1];
if (action == "add") {
if (argc == 2) {
add();
} else {
add(argv[2]);
}
} else if (action == "list") {
if (argc == 2)
{
list_messages();
}else{
show_usage(argv[0]);
}
} else if (action == "search") {
Message* pointer;
if (argc == 2) {
pointer = search();
} else {
pointer = search(argv[2]);
}
result_search(pointer);
} else {
return show_usage(argv[0]);
}
return 0;
}
void App::add()
{
std::string message;
std::cout << "Enter your message:" << std::endl;
std::getline(std::cin, message);
add(message);
}
void App::add(const std::string message)
{
diary.add(message);
}
Message* App::search()
{
std::string looked;
std::cout << "Enter the text to be searched:" << std::endl;
std::getline(std::cin, looked);
return search(looked);
}
Message* App::search(const std::string looked)
{
return diary.search(looked);
}
void App::list_messages()
{
for (size_t i = 0; i < diary.message_size; ++i) {
Message message = diary.messages[i];
print_message(message);
}
}
void App::print_message(Message message)
{
std::cout << "- " << message.date.get_date_string() << " " << message.time.get_time_string() << " " << message.content << std::endl;
}
void App::result_search(Message* pointer){
if (pointer != nullptr){
std::cout << "Term found at the " << pointer - diary.messages + 1 << "th message of the diary.\n";
std::cout << "The message content is:" << std::endl;
print_message(*pointer);
return;
}
std::cout << "Term not found" << std::endl;
}
\ No newline at end of file
#include "../include/Date.h"
#include <sstream>
#include <ctime>
#include <iomanip>
Date::Date(unsigned day, unsigned month, unsigned year): day(day), month(month), year(year) {
}
Date::Date(): day(0), month(0), year(0) {
}
void Date::set_from_string_date(const std::string& date){
std::stringstream string_to_convert(date);
char delimiter;
string_to_convert >> day;
string_to_convert >> delimiter;
string_to_convert >> month;
string_to_convert >> delimiter;
string_to_convert >> year;
}
std::string Date::get_date_string() {
std::stringstream str;
str << std::setfill('0') << std::setw(2) << day;
str << '/';
str << std::setfill('0') << std::setw(2) << month;
str << '/';
str << year;
return str.str();
}
\ No newline at end of file
#include "../include/Diary.h"
#include "../include/Message.h"
#include "../include/Date.h"
#include "../include/Time.h"
#include "../include/Helpers.h"
#include <fstream>
#include <string>
Diary::Diary(const std::string& filename) : message_max(10),filename(filename), message_size(0), messages(nullptr) {
message_max = 10;
messages = new Message[message_max];
std::ifstream archive(filename);
if(archive.fail()){
//file_error();
}else{
std::string lineofFile;
Date date_read(0,0,0);
Time time_read(0,0,0);
while(!archive.eof()){
std::getline(archive, lineofFile);
if(lineofFile.size()==0){
continue;
}
if(lineofFile[0]=='#'){
char delimiter;
std::string date;
date = lineofFile.substr(2);
date_read.set_from_string_date(date);
continue;
}
char delimiter;
std::string time, text;
time = lineofFile.substr(2,9);
text = lineofFile.substr(10);
time_read.set_from_string_time(time);
Message m_aux(text, date_read, time_read);
if(message_size>=message_max){
message_max = message_max*2;
Message* messages_aux = new Message[message_max];
for (int i = 0; i < message_size; ++i) {
messages_aux[i] = messages[i];
}
delete[] messages;
messages = messages_aux;
messages[message_size] = m_aux;
}else{
messages[message_size] = m_aux;
message_size = message_size + 1;
}
}
}
archive.close();
}
Diary::~Diary() {
delete[] messages;
}
void Diary::add(const std::string& message) {
if (message_size>=message_max){
message_max = message_max*2;
Message* messages_aux = new Message[message_max];
for (int i = 0; i < message_size; ++i) {
messages_aux[i] = messages[i];
}
delete[] messages;
messages = messages_aux;
}
Date now_date(0,0,0);
Time now_time(0,0,0);
now_date.set_from_string_date(get_current_date());
now_time.set_from_string_time(get_current_time());
Message aux(message,now_date,now_time);
messages[message_size] = aux;
write();
message_size = message_size + 1;
}
void Diary::write(){
std::ifstream archive(filename);
int exist;
if(archive.fail()){
//file_error();
exist=-1;
}else{
exist = dateExists(archive, messages[message_size].date);
}
archive.close();
std::ofstream record(filename,std::ios::app);
if(record.fail()){
file_error();
return;
}else{
if (exist==-1){
record << "# " << messages[message_size].date.get_date_string() << std::endl;
}
record <<"- "<< messages[message_size].time.get_time_string()<< " " << messages[message_size].content << std::endl;
}
record.close();
}
Message* Diary::search(const std::string& looked_str) {
if (message_size<1) {
return nullptr;
}else{
for (int i = 0; i < message_size; ++i) {
if ((messages[i].content).find(looked_str) != std::string::npos) {
return messages+i;
}
}
}
return nullptr;
}
#include "../include/Helpers.h"
#include "../include/Date.h"
#include <ctime>
#include <iostream>
#include <fstream>
std::string format_current_date(const std::string& format) {
std::time_t time = std::time(nullptr);
char result[1024];
std::strftime(result, sizeof(result), format.c_str(), std::localtime(&time));
return std::string(result);
}
std::string get_current_date() {
return format_current_date("%d/%m/%Y");
}
std::string get_current_time(){
return format_current_date("%H:%M:%S");
}
int show_usage(std::string program) {
std::cerr << "Uso: " << program << " add <mensagem> or " << program << " list or " << program << "search <term>" << std::endl;
return 1;
}
int file_error() {
std::cerr << "Erro ao abrir o arquivo" << std::endl;
return -1;
}
int dateExists(std::ifstream &infile, Date date_to_test){
std::string lineofFile;
while(!infile.eof()){
std::getline(infile, lineofFile);
if (lineofFile=="# "+date_to_test.get_date_string()){
return 1;
}
}
return -1;
}
#include "../include/Message.h"
#include <iostream>
Message::Message(std::string content, Date date, Time time): content(content), date(date), time(time) {
}
Message::Message() {
}
\ No newline at end of file
#include "../include/Time.h"
#include <sstream>
#include <ctime>
#include <iomanip>
Time::Time(unsigned hour, unsigned minute, unsigned second): hour(hour), minute(minute), second(second){
}
Time::Time(): hour(0), minute(0), second(0) {
}
void Time::set_from_string_time(const std::string& time){
std::stringstream string_to_convert(time);
char delimiter;
string_to_convert >> hour;
string_to_convert >> delimiter;
string_to_convert >> minute;
string_to_convert >> delimiter;
string_to_convert >> second;
}
std::string Time::get_time_string() {
std::stringstream str;
str << std::setfill('0') << std::setw(2) << hour;
str << ':';
str << std::setfill('0') << std::setw(2) << minute;
str << ':';
str << std::setfill('0') << std::setw(2) << second;
return str.str();
}
\ No newline at end of file
#include "../include/App.h"
int main(int argc, char* argv[])
{
App aplicativo("diary.md");
return aplicativo.run(argc, argv);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment