Commit 629c0537 authored by veronicamars73's avatar veronicamars73
Browse files

Adding working search

parent 3664bc59
No preview for this file type
......@@ -8,3 +8,6 @@
- 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
# 01/07/2020
- 10:30:41 Nostalgia aoty 2020
- 10:30:58 my name is jonas
......@@ -6,6 +6,7 @@
#include <string>
#include <vector>
struct App
{
......@@ -16,10 +17,10 @@ struct App
void add();
void add(const std::string message);
Message* search();
Message* search(const std::string looked);
std::vector<Message*> search();
std::vector<Message*> search(const std::string looked);
void list_messages();
void print_message(Message message);
void result_search(Message* pointer);
void result_search(std::vector<Message*> pointer);
};
#endif
......@@ -2,21 +2,19 @@
#define W5N_DIARY_H
#include "Message.h"
#include <vector>
#include <string>
struct Diary
{
Diary(const std::string& filename);
~Diary();
std::string filename;
Message* messages;
std::vector<Message> messages;
size_t message_size;
size_t message_max;
void add(const std::string& message);
void write();
Message* search(const std::string& message);
std::vector<Message*> search(const std::string& message);
};
#endif
......@@ -5,6 +5,7 @@
#include "../include/Message.h"
#include <iostream>
#include <string>
#include <vector>
App::App(const std::string& filename) : diary(filename)
{
......@@ -33,7 +34,7 @@ int App::run(int argc, char* argv[])
}
} else if (action == "search") {
Message* pointer;
std::vector<Message*> pointer;
if (argc == 2) {
pointer = search();
} else {
......@@ -62,7 +63,7 @@ void App::add(const std::string message)
}
Message* App::search()
std::vector<Message*> App::search()
{
std::string looked;
std::cout << "Enter the text to be searched:" << std::endl;
......@@ -71,7 +72,7 @@ Message* App::search()
return search(looked);
}
Message* App::search(const std::string looked)
std::vector<Message*> App::search(const std::string looked)
{
return diary.search(looked);
......@@ -90,11 +91,13 @@ 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);
void App::result_search(std::vector<Message*> pointer){
if (!pointer.empty()){
for(auto msg : pointer){
std::cout << "Term found at the " << msg - (&diary.messages[0]) + 1 << "th message of the diary.\n";
std::cout << "The message content is:" << std::endl;
print_message(*msg);
}
return;
}
std::cout << "Term not found" << std::endl;
......
......@@ -5,10 +5,10 @@
#include "../include/Helpers.h"
#include <fstream>
#include <string>
#include <vector>
Diary::Diary(const std::string& filename) : message_max(10),filename(filename), message_size(0), messages(nullptr) {
message_max = 10;
messages = new Message[message_max];
Diary::Diary(const std::string& filename) : filename(filename), message_size(0) {
std::ifstream archive(filename);
if(archive.fail()){
......@@ -36,45 +36,23 @@ Diary::Diary(const std::string& filename) : message_max(10),filename(filename),
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;
}
messages.push_back(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;
messages.push_back(aux);
write();
message_size = message_size + 1;
......@@ -105,16 +83,13 @@ void Diary::write(){
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;
}
std::vector<Message*> Diary::search(const std::string& looked_str) {
std::vector<Message*> result;
for (int i = 0; i < message_size; ++i) {
if ((messages[i].content).find(looked_str) != std::string::npos) {
result.push_back(&messages[i]);
}
}
return nullptr;
return result;
}
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