Skip to content

File Handling

RenΓ© Descartes Muala edited this page May 1, 2021 · 1 revision

File Handling πŸ—‚

Probably the most important function of landb is that gives to you the ability to write and read variables from files. Actually the landb interpreter reads files in LDS format, so lets write some example.

Example.lds file:

(mail:
	(details:
		sender=s:"[email protected]"
		receiver=s:"YourEmail"
		subject=s:"Landb info" )
	message=s:"Landb is a library to handle your variables" )

Interesting! In the file we have a mail container witch has details and a message. Make sure o copy that file to your executable directory.

The connect() method πŸ”—

This method connects the database to a LDS file, from where we can read and write data. Note: If the file doesn't exist, it creates a new file.

    // Usage
    database.connect("file_name");
    // Example - Connecting to "students.lds"
    database.connect("students.lds");

The push() & pull() methods πŸ“₯πŸ“€

Once we have successful connected our database to a file we can database.push() to write and database.pull() to read data.

Implementation πŸ”¨

First we connect our database to a file, and if the connection wasn't successful done, we log and error and exit the program.

    // Connecting to Example.lds
    if(!database.connect("Example.lds")){
        std::cout << "Error: connection failed, unable to open file Example.lds" << std::endl;
        exit(1);
    }

Then we pull the file data

    // Pull data from file
    database.pull();

Printing Everything πŸ“Ί

    // Printing everything
    std::cout << "=\t Email details \t =" << std::endl
    << "Sender: " << database.get< std::string >("mail.details", "sender", lan::String) << std::endl
    << "Receiver: " << database.get< std::string>("mail.details", "receiver", lan::String) << std::endl
    << "Subject: " << database.get< std::string>("mail.details", "subject", lan::String) << std::endl
    << "Message: " << database.get< std::string>("mail", "message", lan::String) << std::endl;
    return 0;
}

Here is the entire code {✨}

#include <iostream>
#include "landb.hpp"

int main(int argc, const char * argv[]) {
    
    // Database
    lan::db database;
    
    // Connecting to Example.lds
    if(!database.connect("Example.lds")){
        std::cout << "Error: connection failed, unable to open file Example.lds" << std::endl;
        exit(1);
    }
    
    // Pull data from file
    database.pull();
    
    // Printing everything
    std::cout << "=\t Email details \t =" << std::endl
    << "Sender: " << database.get< std::string >("mail.details", "sender", lan::String) << std::endl
    << "Receiver: " << database.get< std::string>("mail.details", "receiver", lan::String) << std::endl
    << "Subject: " << database.get< std::string>("mail.details", "subject", lan::String) << std::endl
    << "Message: " << database.get< std::string>("mail", "message", lan::String) << std::endl;
    return 0;
}

And one more example 😁

Now we are going to save a color (variable)! First se create the variable

    // Creating the variable
    database.set< std::string >("myColor", "blue", lan::String);

The we connect and check the connection

    // Connecting to Example2.lds
    if(!database.connect("Example2.lds")){
        std::cout << "Error: connection failed, unable to open file Example2.lds" << std::endl;
        exit(1);
    }

Finally we push data to the file

    // Push data to file
    database.push();

Here is the entire code {✨}

#include <iostream>
#include "landb.hpp"

int main(int argc, const char * argv[]) {
    
    // Database
    lan::db database;
    
    // Creating a variable
    database.set< std::string >("myColor", "blue", lan::String);
    
    // Connecting to Example2.lds
    if(!database.connect("Example2.lds")){
        std::cout << "Error: connection failed, unable to open file Example2.lds" << std::endl;
        exit(1);
    }
    
    // Push data to file
    database.push();
    
    return 0;
}

Go back to Home.