blob: 5cd0a908b448100cd6228603815b4445ede5da37 [file] [log] [blame]
/**
* @file common.c
* @author Michal Vasko <mvasko@cesnet.cz>
* @brief common internal definitions for libyang
*
* Copyright (c) 2018 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*/
#define _XOPEN_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include "common.h"
#include "tree_schema.h"
void *
ly_realloc(void *ptr, size_t size)
{
void *new_mem;
new_mem = realloc(ptr, size);
if (!new_mem) {
free(ptr);
}
return new_mem;
}
int
lysp_check_date(struct ly_ctx *ctx, const char *date, int date_len, const char *stmt)
{
int i;
struct tm tm, tm_;
char *r;
if (date_len != LY_REV_SIZE - 1) {
goto error;
}
/* check format */
for (i = 0; i < date_len; i++) {
if (i == 4 || i == 7) {
if (date[i] != '-') {
goto error;
}
} else if (!isdigit(date[i])) {
goto error;
}
}
/* check content, e.g. 2018-02-31 */
memset(&tm, 0, sizeof tm);
r = strptime(date, "%Y-%m-%d", &tm);
if (!r || r != &date[LY_REV_SIZE - 1]) {
goto error;
}
memcpy(&tm_, &tm, sizeof tm);
mktime(&tm_); /* mktime modifies tm_ if it refers invalid date */
if (tm.tm_mday != tm_.tm_mday) { /* e.g 2018-02-29 -> 2018-03-01 */
/* checking days is enough, since other errors
* have been checked by strptime() */
goto error;
}
return 0;
error:
LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_INVAL, date_len, date, stmt);
return -1;
}
int
lysp_get_data_line(const char *data, int fail_char)
{
int i, line = 1;
for (i = 0; i < fail_char; ++i) {
if (data[i] == '\n') {
++line;
}
}
return line;
}