Michał Biolik / Mbed 2 deprecated SD_TUT

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed

file_manager.cpp

Committer:
micbio
Date:
2016-12-05
Revision:
2:e699312248f3
Parent:
1:58d2021e301d

File content as of revision 2:e699312248f3:

#include "file_manager.hpp"

#define MAX_TEXT_LEN        400

extern Serial pc;

char buffer[MAX_TEXT_LEN];

void file_manager::print_files(void)
{
    pc.printf("1. %s\r\n", path_sisk);
    pc.printf("2. %s\r\n", path_agh);
    pc.printf("3. %s\r\n", path_krk);
}

void file_manager::get_string(void)
{
//    char * p_my_//cmd = p_cmd;
//    uint8_t char_counter = 0;
//    static char byte_received;
//    
//    do
//    {
//        byte_received = pc.getc();
//        *p_my_cmd = byte_received;
//        p_my_cmd++;
//        char_counter++;
//        if (char_counter >= CMD_LENGTH)
//        {
//            break;      // CMD length reached max length.
//        }        
//        pc.putc(byte_received);
//        for (int i = 0; i < len; i++)
//        {
//            pc.putc(*(p_cmd + i));
//        }
//    }while(byte_received != '\r');
//    
//    len = char_counter;
//    
//    pc.printf("\r\r\n");
//    pc.putc('Y');    
}

void file_manager::get_path(void)
{
    pc.printf("Current file to write to is: %s.\r\n", current_path);    
}

void file_manager::set_path(void)
{
    char number;
    bool correct_number = false;

    if (fp != NULL)
    {
        pc.printf("First close the file!\r\n");
        return;
    }

    this->get_path();
    pc.printf("Write the number of new file to write to. \r\n");    
    this->print_files();

    do
    {
        correct_number = true;
        number = pc.getc();
        switch(number)
        {
            case '1':
                current_path = path_sisk;
                break;
            case '2':
                current_path = path_agh;
                break;
            case '3':
                current_path = path_krk;
                break;
            default:
                pc.printf("Incorrect number, try again.\r\n");
                correct_number = false;
        }
    }while(!correct_number);

    pc.printf("Setting path to: %s.\r\n", current_path);
}

bool file_manager::open_file(char option)
{
    if (fp != NULL)
    {
        pc.printf("File already opened!\r\n");
        return false;
    }

    fp = fopen(this->current_path, &option);

    if (fp != NULL)
    {
        pc.printf("Opened file: %s.\r\n", this->current_path);
    }

    return true;
}

bool file_manager::close_file(void)
{
    if (fp == NULL)
    {
        pc.printf("File already closed!\r\n");
        return false;
    }

    fclose(fp);

    fp = NULL;

    return true;
}

void file_manager::write_to_file(void)
{
    this->open_file('w');

    

    this->close_file();
}


void file_manager::append_to_file(void)
{
    this->open_file('a');

    

    this->close_file();
}

void file_manager::read_file(void)
{
    long lSize;
    size_t result;

    this->open_file('r');

    fseek(fp , 0 , SEEK_END);
    lSize = ftell (fp);
    rewind(fp);
    
    // copy the file into the buffer:
    result = fread(buffer, 1, lSize, fp);
    if (result != lSize) 
    {
        pc.printf("Reading error");
    }
    else
    {
        pc.printf("File: %s.\r\r\n", this->current_path);
    }
    

    this->close_file();
    
}