Programming Samples

Programming Samples

Job applications frequently request code samples. I am posting a few samples of my source code from my current projects. At some point I may copy some of my self-hosted Gitlab repositories to Github, but have not convinced myself to do that as yet.

ZIP file of my CMake/C++ demo project.

There are currently 3 parts to this sample, listed in the side-bar.

  1. Logging routines
  2. JSON/STL encode routines
  3. JSON/STL decode routines

There is a fourth self-contained example, also in the side bar: C++ 20 Logging Module.

The main source file for the demo is shown at the end of this page.

I have been trying to develop a personal naming convention for files. The "u_" indicates it is a utility, and followed by a category, sub-category, etc.

The main source file is listed at the end of this post, and output messages from it precede that and follow the remainder of this description.

The messages are in a standardized format. I had purchased External link CLion having really enjoyed working with a related IDE, External link Android Studio, and discovered it understood my format. The file and line numbers are detected by CLion and can be clicked to navigate to the corresponding line in the source file.

The JSON routines use polymorphism to decode/encode JSON data. Using C++ templates, the defined type is traversed either writing or reading the associated data-type. The strings are URL encoded.

This demonstration program allocates a standard library container (STL) and writes it out to a file. Subsequently, the STL file is read back in, and its data presented.

Note: The JSON routines can be extended to arbitrary classes by writing specific encode and decode functions. I've done this by implementing classes handling geometry and vectors.

While not large examples I hope they provide insight into my programming. I hope to add additional files, perhaps on Github, relating to other components.

The logging output has the same format for the demo project and the C++ 20 logging module.

Logging output

INFO:main.cpp:36:main:STARTED
INFO:main.cpp:45:test_log:example of the log levels
TODO:main.cpp:46:test_log:example implementation needed
DEBUG:main.cpp:47:test_log:example debug message
INFO:main.cpp:48:test_log:example informational  message
WARN:main.cpp:49:test_log:example warning message
ERROR:main.cpp:50:test_log:example error message
INFO:main.cpp:69:test_file_write:data before encoding
INFO:main.cpp:70:test_file_write:"test data"
INFO:main.cpp:70:test_file_write:  1
INFO:main.cpp:70:test_file_write:  2
INFO:main.cpp:70:test_file_write:  3
INFO:main.cpp:70:test_file_write:  6
INFO:main.cpp:70:test_file_write:  5
INFO:main.cpp:70:test_file_write:  4
INFO:main.cpp:72:test_file_write:encoded string written to file
INFO:main.cpp:73:test_file_write:  ["%22test%20data%22",[1,2,3,6,5,4]]
INFO:main.cpp:62:test_file_read:data read back in from file
INFO:main.cpp:63:test_file_read:"test data"
INFO:main.cpp:63:test_file_read:  1
INFO:main.cpp:63:test_file_read:  2
INFO:main.cpp:63:test_file_read:  3
INFO:main.cpp:63:test_file_read:  6
INFO:main.cpp:63:test_file_read:  5
INFO:main.cpp:63:test_file_read:  4
INFO:main.cpp:40:main:FINISHED

The "main.cpp" source file that generated the example messages.

main.cpp

#include "u_json_decode.h"
#include "u_json_encode.h"
#include "u_log.h"

#include <fstream>
#include <ostream>
#include <streambuf>
#include <string>
#include <tuple>
#include <vector>

typedef std::vector<int> FILE_DATA;

typedef std::tuple<std::string, FILE_DATA> FILE_FORMAT;

std::ostream &operator<<(std::ostream &os, const FILE_DATA &dt) {
  for (auto s : dt) {
    os << "  " << s << std::endl;
  }
  return os;
}

std::ostream &operator<<(std::ostream &os, const FILE_FORMAT &dt) {
  os << std::get<0>(dt) << std::endl;
  os << std::get<1>(dt);
  return os;
}

void test_log();

void test_file_write();

void test_file_read();

int main(int argc, char **argv) {
  LINFO << "STARTED";
  test_log();
  test_file_write();
  test_file_read();
  LINFO << "FINISHED";
  return 0;
}

void test_log() {
  LINFO << "example of the log levels";
  LTODO << "example implementation needed";
  LDEBUG << "example debug message";
  LINFO << "example informational  message";
  LWARN << "example warning message";
  LERROR << "example error message";
}

void test_file_read() {
  std::ifstream input("test_out.json");
  std::string str_json((std::istreambuf_iterator<char>(input)),
                       std::istreambuf_iterator<char>());
  Json::Value root;
  Json::Reader reader;
  reader.parse(str_json, root);
  FILE_FORMAT data;
  UJSON::decode(root, data);
  LINFO << "data read back in from file";
  LINFO << data;
}

void test_file_write() {
  std::ofstream t("test_out.json");
  FILE_FORMAT data = {"\"test data\"", {1, 2, 3, 6, 5, 4}};
  LINFO << "data before encoding";
  LINFO << data;
  std::string out = UJSON::encode(data);
  LINFO << "encoded string written to file";
  LINFO << "  " << out;
  t << out;
  t.flush();
  t.close();
}

Measurement Data C++ Logging Routines