Wiki

Clone wiki

occam-web / Beginners-Guide / 0.A-Dummy-Program

Wrapping a Program With an OCCAM Object

In this tutorial, you are going to learn how to create an OCCAM object to wrap your code. For that purpose, you will be using a simple program that will perform a simple task. Despite simple, this task will lead you through some of the basic OCCAM tools that you need to master.

A Dummy Program

You can download the dummy program from this link.

Note: This tutorial assumes you are using Ubuntu 14:04. If you are using another OS, please adjust accordingly

You can test the program before wrapping it in an OCCAM object. But first, create a folder to use during this tutorial.

$ mkdir ~/occam_workspace
cd ~/occam_workspace

Download and extract the contents of the zip file into the directory you just created:

wget https://bitbucket.org/occam/occam-web/wiki/Beginners-Guide/program.zip
unzip program.zip

You should get the following:

dummy_program
├── config.json
├── input.txt
└── main.cpp

Brief description of main.cpp

The program takes as inputs three files: 1. An input file 2. A configuration file 3. An output file

...
    if(argc!=4)
    {
        std::cout << "Wrong usage:"<<std::endl<<
        "\t"<<  argv[0]<<" input_file configuration_file output_file"<<std::endl;
        return -1;
    }
    std::string input_file=argv[1];
    std::string configuration_file=argv[2];
    std::string output_file=argv[3];
...

Then, the program reads the input file and prints it to the standard output.

    ...
    /* Print input file */
    std::cout<<"::::::::::::::::: Printing Input :::::::::::::::::"<<std::endl;
    std::ifstream input(input_file, std::ifstream::binary);
    std::string line;
    while (std::getline(input, line)) std::cout<<line<<std::endl;
    ...

The program tries to load a JSON configuration file that should contain four values (see config.json).

    ...
    /* Read Configurations */
    std::cout<<"::::::::::::::::: Reading Configuration :::::::::::::::::"<<std::endl;
    //Defaults
    int int_config=0;
    std::string string_config="null";
    std::string list_config="default";
    bool bool_config=false;
    Json::Reader json_reader;
    Json::Value json_root;
    bool valid_root;
    try
    {
        std::ifstream configuration_descriptor(configuration_file, std::ifstream::binary);
        valid_root=json_reader.parse(configuration_descriptor,json_root);
        if (!valid_root)
        {
            std::cout  << "Failed to parse configuration\n"
            << json_reader.getFormattedErrorMessages();
        }
        else
        {
            int_config=json_root["int_config"].asUInt64();
            string_config=json_root["string_config"].asString();
            list_config=json_root["list_config"].asString();
            bool_config=json_root["bool_config"].asBool();
        }
    }
    catch(...)
    {
        std::cout<<"Using defaults"<<std::endl;
    }
    ...
Finaly, the program saves the configuration values to the output file.
    ...
    /* Write output */
    std::cout<<"::::::::::::::::: Writting Output :::::::::::::::::"<<std::endl;
    std::ofstream output(output_file, std::ifstream::binary);
    Json::Value json_output;
    Json::Value json_configurations;
    json_configurations["int_config"]=int_config;
    json_configurations["string_config"]=string_config;
    json_configurations["list_config"]=list_config;
    json_configurations["bool_config"]=bool_config;
    Json::Value json_configurations_array(Json::arrayValue);
    json_configurations_array.append(json_configurations);
    json_output["configurations"]=json_configurations_array;
    output<<json_output<<std::endl;
    ...

Compiling the program

Open a terminal and navigate to the extracted folder:

$ cd ~/occam_workspace/dummy_program

In order to run this program we need to install the libjsoncpp-dev package (in ubuntu 14.04):

$ sudo apt-get install libjsoncpp-dev

Once this package is installed, we can compile the program:

$ g++ -o dummy main.cpp -ljsoncpp -std=c++11

Your folder should look like this:

dummy_program
├── config.json
├── dummy
├── input.txt
└── main.cpp

Running the program

From within the dummy_program folder run:

$ ./dummy input.txt config.json output.json

You should get an output file named output.json with the following contents:

{
    "configurations" :
    [
        {
            "bool_config" : true,
            "int_config" : 2,
            "list_config" : "list",
            "string_config" : "def"
        }
    ]
}

NEXT: Creating a simple OCCAM object

Updated