#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <google/template.h>

extern "C" {
void *ct_load_template(const char *filename, int option) {
  return google::Template::GetTemplate(filename, (google::Strip) option);
}

void *ct_new_dict() {
  return new google::TemplateDictionary("");
}

void ct_set_string(google::TemplateDictionary *dict, const char *key,
                   const char *value) {
  dict->SetValue(key, value);
}

void ct_set_bytes(google::TemplateDictionary *dict, const char *key, const char *bytes,
                  const int length) {
  dict->SetValue(key, std::string(bytes, length));
}

void *ct_new_section(google::TemplateDictionary *dict, const char *key) {
  return dict->AddSectionDictionary(key);
}

void *ct_new_included(google::TemplateDictionary *dict, const char *key) {
  return dict->AddIncludeDictionary(key);
}

void ct_set_filename(google::TemplateDictionary *dict, const char *filename) {
  dict->SetFilename(filename);
}

char *ct_expand(google::Template *tmpl, google::TemplateDictionary *dict) {
  std::string result;
  tmpl->Expand(&result, dict);
  char *ret = (char *) malloc(result.size() + 1);
  memcpy(ret, result.c_str(), result.size() + 1);

  return ret;
}

void ct_set_root(const char *path) {
  google::Template::SetTemplateRootDirectory(path);
}

}
