<commit_before><?php
    
require_once "srvc_book_common.php";
require_once "utils_time.php";

function __impl_book_file_base_dir()
{
    return dirname(__FILE__) . "/../reservation/";
}

function __impl_book_file_dir_4_name($subdir)
{
    return __impl_book_file_base_dir() . "/" . $subdir;
}

function __impl_book_file_rticket_path($rticket)
{
    $dir = __impl_book_file_dir_4_name($rticket->visit_date);
    
    if (!is_dir($dir))
    {
        mkdir($dir);
    }
    
    $path = $dir . "/" . $rticket->visit_mins_slot . ".json";
    return $path;
}

define("KEY_FILE_JSON_RTICKET_COUNT",           "COUNT");
define("KEY_FILE_JSON_RTICKET_LIST",            "ALL");
function impl_book_do_reserve($rticket, $max_per_slot=10)
{
    $path = __impl_book_file_rticket_path($rticket);
    
    // read json from file
    if (file_exists($path))
    {
        $orig_json_str = file_get_contents($path);
    }
    else
    {
        $orig_json_str = '{ "' . KEY_FILE_JSON_RTICKET_COUNT . '" : 0, "' . KEY_FILE_JSON_RTICKET_LIST . '" : {} }';
    }
    
    $json = json_decode($orig_json_str, true);
    
    // check
    if ($json == null ||
        !array_key_exists(KEY_FILE_JSON_RTICKET_COUNT, $json) ||
        !array_key_exists(KEY_FILE_JSON_RTICKET_LIST, $json))
    {
        return BOOK_CODE_ERR_CORRUPT;
    }
    
    // check if full
    $count_rtickets = $json[KEY_FILE_JSON_RTICKET_COUNT];
    if ($count_rtickets + $rticket->num > $max_per_slot)
    {
        return BOOK_CODE_ERR_FULL;
    }
    
    // update json count & list
    $arr_rtickets = $json[KEY_FILE_JSON_RTICKET_LIST];
    if (!is_array($arr_rtickets))
    {
        return BOOK_CODE_ERR_CORRUPT;
    }
    
    array_push($arr_rtickets, $rticket->to_array());
    $json[KEY_FILE_JSON_RTICKET_COUNT] += $rticket->num;
    $json[KEY_FILE_JSON_RTICKET_LIST] = $arr_rtickets;
    $new_json_str = json_encode($json);
    
    // flush
    $handle_f = fopen($path, "w") or die ("ERROR to open $path!");
    fwrite($handle_f, $new_json_str);
    fclose($handle_f);
    
    return BOOK_CODE_OK;
}
    
function impl_book_query_schedule($prev_n, $next_n)
{
    $begin_day = time() - $prev_n * SEC_PER_DAY;
    
    for ($k = 0; $k < $prev_n + $next_n; $k++)
    {
        $cur = $begin_day + $k * SEC_PER_DAY;
        $dir = __impl_book_file_dir_4_name(date("Ymd", $cur));
        
        if (!isdir($dir))
        {
            continue;
        }
        
        $dh = opendir($dir);
        if ($dh == false)
        {
            continue;
        }
        
        while (($fname = readdir($dh)) != false)
        {
            $fpath = $dir . $fname;
            
            // skip folders
            if (isdir($fpath))
            {
                continue;
            }<commit_msg>fix error<commit_after><?php
    
require_once "srvc_book_common.php";
require_once "utils_time.php";

function __impl_book_file_base_dir()
{
    return dirname(__FILE__) . "/../reservation/";
}

function __impl_book_file_dir_4_name($subdir)
{
    return __impl_book_file_base_dir() . "/" . $subdir;
}

function __impl_book_file_rticket_path($rticket)
{
    $dir = __impl_book_file_dir_4_name($rticket->visit_date);
    
    if (!is_dir($dir))
    {
        mkdir($dir);
    }
    
    $path = $dir . "/" . $rticket->visit_mins_slot . ".json";
    return $path;
}

define("KEY_FILE_JSON_RTICKET_COUNT",           "COUNT");
define("KEY_FILE_JSON_RTICKET_LIST",            "ALL");
function impl_book_do_reserve($rticket, $max_per_slot=10)
{
    $path = __impl_book_file_rticket_path($rticket);
    
    // read json from file
    if (file_exists($path))
    {
        $orig_json_str = file_get_contents($path);
    }
    else
    {
        $orig_json_str = '{ "' . KEY_FILE_JSON_RTICKET_COUNT . '" : 0, "' . KEY_FILE_JSON_RTICKET_LIST . '" : {} }';
    }
    
    $json = json_decode($orig_json_str, true);
    
    // check
    if ($json == null ||
        !array_key_exists(KEY_FILE_JSON_RTICKET_COUNT, $json) ||
        !array_key_exists(KEY_FILE_JSON_RTICKET_LIST, $json))
    {
        return BOOK_CODE_ERR_CORRUPT;
    }
    
    // check if full
    $count_rtickets = $json[KEY_FILE_JSON_RTICKET_COUNT];
    if ($count_rtickets + $rticket->num > $max_per_slot)
    {
        return BOOK_CODE_ERR_FULL;
    }
    
    // update json count & list
    $arr_rtickets = $json[KEY_FILE_JSON_RTICKET_LIST];
    if (!is_array($arr_rtickets))
    {
        return BOOK_CODE_ERR_CORRUPT;
    }
    
    array_push($arr_rtickets, $rticket->to_array());
    $json[KEY_FILE_JSON_RTICKET_COUNT] += $rticket->num;
    $json[KEY_FILE_JSON_RTICKET_LIST] = $arr_rtickets;
    $new_json_str = json_encode($json);
    
    // flush
    $handle_f = fopen($path, "w") or die ("ERROR to open $path!");
    fwrite($handle_f, $new_json_str);
    fclose($handle_f);
    
    return BOOK_CODE_OK;
}
    
function impl_book_query_schedule($prev_n, $next_n)
{
    $begin_day = time() - $prev_n * SEC_PER_DAY;
    
    for ($k = 0; $k < $prev_n + $next_n; $k++)
    {
        $cur = $begin_day + $k * SEC_PER_DAY;
        $dir = __impl_book_file_dir_4_name(date("Ymd", $cur));
        
        if (!is_dir($dir))
        {
            continue;
        }
        
        $dh = opendir($dir);
        if ($dh == false)
        {
            continue;
        }
        
        while (($fname = readdir($dh)) != false)
        {
            $fpath = $dir . $fname;
            
            // skip folders
            if (isdir($fpath))
            {
                continue;
            }
{'fraction_non_alphanumeric': 0.13808060726549792, 'fraction_numerical': 0.0025302729080065064, 'mean_word_length': 2.3257211538461537, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '2052448', 'n_tokens_mistral': 2071, 'n_tokens_neox': 2045, 'n_words': 299}