<commit_before>/* sdb - LGPLv3 - Copyright 2011-2014 - pancake */

#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include "sdb.h"

static int save = 0;
static Sdb *s = NULL;

static void terminate(int sig UNUSED) {
	if (!s) return;
	if (save) sdb_sync (s);
	sdb_free (s);
	exit (0);
}

static char *stdin_gets() {
	static char buf[96096];
	fgets (buf, sizeof (buf)-1, stdin);
	if (feof (stdin)) return NULL;
	buf[strlen (buf)-1] = 0;
	return strdup (buf);
}

#if USE_MMAN
static void syncronize(int sig UNUSED) {
	// TODO: must be in sdb_sync() or wat?
	Sdb *n;
	sdb_sync (s);
	n = sdb_new (s->path, s->name, s->lock);
	sdb_free (s);
	s = n;
}
#endif

static int sdb_dump (const char *db, int qf) {
	char *k, *v, *p;
	Sdb *s = sdb_new (NULL, db, 0);
	if (!s) return 1;
	sdb_dump_begin (s);
	while (sdb_dump_dupnext (s, &k, &v)) {
		if (qf && strchr (v, SDB_RS)) {
			for (p=v; *p; p++)
				if (*p==SDB_RS)
					*p = ',';
			printf ("[]%s=%s\n", k, v);
		} else {
			printf ("%s=%s\n", k, v);
		}
		free (k);
		free (v);
	}
	sdb_free (s);
	return 0;
}

static int createdb(const char *f) {
	char *line, *eq;
	s = sdb_new (NULL, f, 0);
	if (!s)
	if (!sdb_create (s)) {
		fprintf (stderr, "Cannot create database\n");
		return 1;
	}
	for (;(line = stdin_gets ());) {
		if ((eq = strchr (line, '='))) {
			*eq = 0;
			sdb_append (s, line, eq+1);
		}
		free (line);
	}
	sdb_finish (s);
	return 0;
}

static void showusage(int o) {
	printf ("usage: sdb [-hv] [-|db] []|[<script]|[-=]|[-+][(idx)key[?path|=value] ..]\n");
	exit (o);
}

static void showversion(void) {<commit_msg>Oops. Fix createdb<commit_after>/* sdb - LGPLv3 - Copyright 2011-2014 - pancake */

#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include "sdb.h"

static int save = 0;
static Sdb *s = NULL;

static void terminate(int sig UNUSED) {
	if (!s) return;
	if (save) sdb_sync (s);
	sdb_free (s);
	exit (0);
}

static char *stdin_gets() {
	static char buf[96096];
	fgets (buf, sizeof (buf)-1, stdin);
	if (feof (stdin)) return NULL;
	buf[strlen (buf)-1] = 0;
	return strdup (buf);
}

#if USE_MMAN
static void syncronize(int sig UNUSED) {
	// TODO: must be in sdb_sync() or wat?
	Sdb *n;
	sdb_sync (s);
	n = sdb_new (s->path, s->name, s->lock);
	sdb_free (s);
	s = n;
}
#endif

static int sdb_dump (const char *db, int qf) {
	char *k, *v, *p;
	Sdb *s = sdb_new (NULL, db, 0);
	if (!s) return 1;
	sdb_dump_begin (s);
	while (sdb_dump_dupnext (s, &k, &v)) {
		if (qf && strchr (v, SDB_RS)) {
			for (p=v; *p; p++)
				if (*p==SDB_RS)
					*p = ',';
			printf ("[]%s=%s\n", k, v);
		} else {
			printf ("%s=%s\n", k, v);
		}
		free (k);
		free (v);
	}
	sdb_free (s);
	return 0;
}

static int createdb(const char *f) {
	char *line, *eq;
	s = sdb_new (NULL, f, 0);
	if (!s || !sdb_create (s)) {
		fprintf (stderr, "Cannot create database\n");
		return 1;
	}
	for (;(line = stdin_gets ());) {
		if ((eq = strchr (line, '='))) {
			*eq = 0;
			sdb_append (s, line, eq+1);
		}
		free (line);
	}
	sdb_finish (s);
	return 0;
}

static void showusage(int o) {
	printf ("usage: sdb [-hv] [-|db] []|[<script]|[-=]|[-+][(idx)key[?path|=value] ..]\n");
	exit (o);
}

static void showversion(void) {
{'fraction_non_alphanumeric': 0.21446153846153845, 'fraction_numerical': 0.016615384615384615, 'mean_word_length': 3.375504710632571, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1812254', 'n_tokens_mistral': 1607, 'n_tokens_neox': 1508, 'n_words': 423}