/common/app.c
/* Blerg is (C) 2011 The Dominion of Awesome, and is distributed under a
 * BSD-style license.  Please see the COPYING file for details.
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "app.h"

int parse_url_info(const char *url, struct url_info *info) {
	const char *c;
	int len;
	info->contents = 0;

	c = strchr(url, '/');
	if (c == NULL) {
		len = strlen(url);
	} else {
		len = c - url;
	}
	if (len == 0)
		return 0;
	if (len > MAX_TAG_LENGTH)
		len = MAX_TAG_LENGTH;
	memcpy(info->name, url, len);
	info->name[len] = 0;
	info->contents |= URL_INFO_NAME;

	if (c == NULL || c[1] == 0)
		return info->contents;

	info->record = strtoull(c + 1, NULL, 10);
	info->contents |= URL_INFO_RECORD;

	c = strchr(c, '-');
	if (c == NULL || c[1] == 0)
		return info->contents;

	info->record_to = strtoull(c + 1, NULL, 10);
	info->contents |= URL_INFO_RECORD_TO;

	return info->contents;
}

int parse_auth_cookie(const char *str, struct auth_cookie *cookie) {
	if (str == NULL)
		return 0;
	if (cookie == NULL)
		return 0;

	char *token_begin = strchr(str, '/');
	if (token_begin == NULL) {
		return 0;
	}
	int len = token_begin - str;
	if (len > MAX_TAG_LENGTH || len == 0) {
		return 0;
	}
	memcpy(cookie->name, str, len);
	cookie->name[len] = 0;

	token_begin++;
	len = strlen(token_begin);
	if (len != TOKEN_SIZE * 2) {
		return 0;
	}
	memcpy(cookie->token, token_begin, TOKEN_SIZE * 2);
	cookie->token[TOKEN_SIZE * 2] = 0;

	return 1;
}

uint64_t *make_sequential_list(uint64_t from, uint64_t to) {
	uint64_t len = to - from + 1;
	uint64_t *list = malloc(len * sizeof(uint64_t));
	uint64_t i;

	for (i = 0; i < len; i++) {
		list[i] = from + i;
	}

	return list;
}