<commit_before>/* Copyright (C) 2014 <NAME>
 *
 * This file is part of Interfic.
 *
 * Interfic is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Interfic is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with Interfic.  If not, see
 * <http://www.gnu.org/licenses/>.
 */
#include <stdio.h>
#include <stdlib.h>

#include "libinterfic.h"

static const unsigned char MAGIC[] = { 0x49, 0x4E, 0x54, 0x45, 0x52, 0x46, 0x49, 0x43 };
static const unsigned char VERSION = 0;

#define MAX_OFFSET      ((1UL<<31) - 1)
#define HEADER_SIZE     (sizeof(MAGIC) + sizeof(VERSION))
#define TEXT_SIZE       1024
#define CHOICE_SIZE     256
#define PAGE_NUM_SIZE   3
#define PAGE_SIZE       (TEXT_SIZE + 4*(CHOICE_SIZE + PAGE_NUM_SIZE))
#define MAX_FIC_SIZE    (MAX_OFFSET + PAGE_SIZE - HEADER_SIZE)
const unsigned long MAX_PAGE_NUMBER = MAX_FIC_SIZE/PAGE_SIZE - 1;

extern unsigned discoverFreePages(struct free_page **const free_pages, unsigned long *const total_pages, FILE *const fp){
        if(fseek(fp, HEADER_SIZE, SEEK_SET)){
                fprintf(stderr, "Error seeking to page 0\n");
                return 1;
        }

        unsigned isEOF = 0;
        unsigned long page_num = 0;
        struct free_page **next_free_page = free_pages;
        do{
                char page_data[PAGE_SIZE] = {0};
                fread(page_data, sizeof(page_data), 1, fp);
                if(feof(fp)){
                        page_data[0] = '\0';
                        isEOF = 1;
                }else if(ferror(fp)){
                        fprintf(stderr, "Error trying to read page %lu\n", page_num);
                        return 1;
                }

                if(!page_data[0]){
                        struct free_page *tmp_page = malloc(sizeof(*tmp_page));
                        if(!tmp_page){
                                fprintf(stderr, "Unable to allocate memory for free pages list\n");
                                goto exit_page_malloc;
                        }
                        tmp_page->page_num = page_num;
                        tmp_page->next = NULL;

                        *next_free_page = tmp_page;
                        next_free_page = &(tmp_page->next);
                }

                page_num++;
        }while(!isEOF && page_num <= MAX_PAGE_NUMBER);

        *total_pages = (isEOF) ? page_num - 1 : page_num;<commit_msg>No need to initialize page_data since fread will populate<commit_after>/* Copyright (C) 2014 <NAME>
 *
 * This file is part of Interfic.
 *
 * Interfic is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Interfic is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with Interfic.  If not, see
 * <http://www.gnu.org/licenses/>.
 */
#include <stdio.h>
#include <stdlib.h>

#include "libinterfic.h"

static const unsigned char MAGIC[] = { 0x49, 0x4E, 0x54, 0x45, 0x52, 0x46, 0x49, 0x43 };
static const unsigned char VERSION = 0;

#define MAX_OFFSET      ((1UL<<31) - 1)
#define HEADER_SIZE     (sizeof(MAGIC) + sizeof(VERSION))
#define TEXT_SIZE       1024
#define CHOICE_SIZE     256
#define PAGE_NUM_SIZE   3
#define PAGE_SIZE       (TEXT_SIZE + 4*(CHOICE_SIZE + PAGE_NUM_SIZE))
#define MAX_FIC_SIZE    (MAX_OFFSET + PAGE_SIZE - HEADER_SIZE)
const unsigned long MAX_PAGE_NUMBER = MAX_FIC_SIZE/PAGE_SIZE - 1;

extern unsigned discoverFreePages(struct free_page **const free_pages, unsigned long *const total_pages, FILE *const fp){
        if(fseek(fp, HEADER_SIZE, SEEK_SET)){
                fprintf(stderr, "Error seeking to page 0\n");
                return 1;
        }

        unsigned isEOF = 0;
        unsigned long page_num = 0;
        struct free_page **next_free_page = free_pages;
        do{
                char page_data[PAGE_SIZE];
                fread(page_data, sizeof(page_data), 1, fp);
                if(feof(fp)){
                        page_data[0] = '\0';
                        isEOF = 1;
                }else if(ferror(fp)){
                        fprintf(stderr, "Error trying to read page %lu\n", page_num);
                        return 1;
                }

                if(!page_data[0]){
                        struct free_page *tmp_page = malloc(sizeof(*tmp_page));
                        if(!tmp_page){
                                fprintf(stderr, "Unable to allocate memory for free pages list\n");
                                goto exit_page_malloc;
                        }
                        tmp_page->page_num = page_num;
                        tmp_page->next = NULL;

                        *next_free_page = tmp_page;
                        next_free_page = &(tmp_page->next);
                }

                page_num++;
        }while(!isEOF && page_num <= MAX_PAGE_NUMBER);

        *total_pages = (isEOF) ? page_num - 1 : page_num;
{'fraction_non_alphanumeric': 0.090066456803078, 'fraction_numerical': 0.01906260930395243, 'mean_word_length': 1.995809324253536, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2177920', 'n_tokens_mistral': 1741, 'n_tokens_neox': 1608, 'n_words': 530}