kmx.io/kv/kv.c

Branch

Thomas de Grivel d5556884c 2022-01-04T20:18:17
tests
/*
 * kv - key value text file format
 *
 * Copyright 2022 Thomas de Grivel
*/

#include <assert.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <kv.h>

typedef struct kv_paths s_kv_paths;

struct kv_paths {
  s_kv_path  *k;
  const char *v;
  size_t      vsz;
  s_kv_paths *next;
};

int on_kv (s_buffer *b, const char *v, size_t vsz)
{
  (void) b;
  (void) v;
  (void) vsz;
  printf("--- v: %s\n", v);
  return 0;
}

int read_file (const char *path, s_kv_paths *keys)
{
  s_buffer b;
  if (buffer_init(&b, keys) ||
      buffer_open(&b, path) ||
      kv_parse(&b, on_kv) ||
      buffer_close(&b))
    return -1;
  return 0;
}

s_kv_paths *kv_paths_new (s_kv_path *k, s_kv_paths *next)
{
  s_kv_paths *keys = calloc(sizeof(s_kv_paths), 1);
  if (keys) {
    keys->k = k;
    keys->next = next;
  }
  return keys;
}

s_kv_paths * read_keys (FILE *fp)
{
  (void) fp;
  return NULL;
}

int get (int argc, char **argv)
{
  s_kv_paths *keys = read_keys(stdin);
  int r = 0;
  while (argc--)
    if (read_file(*argv++, keys))
      r++;
  return r;
}

int buffer_read_files (s_buffer *b, int argc, char **argv)
{
  int r;
  if (buffer_init(b, NULL))
    return -1;
  while (argc--) {
    if (!strcmp(*argv, "-")) {
      if (buffer_fp(b, stdin, "stdin"))
        err(1, "stdin");
    }
    else
      if (buffer_open(b, *argv))
        err(1, "%s", *argv);
    while (!(r = buffer_fill(b)))
      ;
    if (r != -1)
      err(1, "%s", b->file_path);
    argv++;
  }
  return 0;
}

int quote (int argc, char **argv)
{
  s_buffer in;
  char    *q;
  size_t   q_size;
  int      r;
  if (buffer_read_files(&in, argc, argv))
    return -1;
  q = in.chars;
  q_size = in.wpos;
  if (!(r = kv_quote(&q, &q_size)) &&
      fwrite(q, q_size, 1, stdout) != 1)
    err(1, "stdout");
  fflush(stdout);
  free(q);
  return r;
}

int unquote (int argc, char **argv)
{
  s_buffer in;
  char    *str;
  size_t   str_size;
  int      r = 1;
  if (buffer_read_files(&in, argc, argv))
    return -1;
  str = in.chars;
  str_size = in.wpos;
  /*
  if (!(r = kv_unquote(&str, &str_size)) &&
      fwrite(str, str_size, 1, stdout) != 1)
    err(1, "stdout");
  */
  fflush(stdout);
  free(str);
  return r;
}

int usage (const char *argv0)
{
  printf("Usage: %s OP [FILE ...]\n"
         "OP can be one of :\n"
         "  -g | --get      Get keys from stdin and print values from "
         "files.\n"
         "  -q | --quote    Quote concatenated files.\n"
         "  -u | --unquote  Unquote concatenated files.\n",
         argv0);
  return 1;
}

int main (int argc, char **argv)
{
  const char *argv0;
  assert(argc > 0);
  assert(argv);
  argv0 = *argv;
  argc--;
  argv++;
  if (argc) {
    if (!strcmp(*argv, "-g") || !strcmp(*argv, "--get")) {
      argc--;
      argv++;
      return get(argc, argv);
    }
    if (!strcmp(*argv, "-q") || !strcmp(*argv, "--quote")) {
      argc--;
      argv++;
      return quote(argc, argv);
    }
    if (!strcmp(*argv, "-u") || !strcmp(*argv, "--unquote")) {
      argc--;
      argv++;
      return unquote(argc, argv);
    }
  }
  return usage(argv0);
}
Download