/*
 * Copyright 2011 Steven Gribble
 *
 *  This file is part of the UW CSE 333 course project sequence
 *  (333proj).
 *
 *  333proj is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  333proj 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with 333proj.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _LEC9_EXERCISE3_LL_FILE_H_
#define _LEC9_EXERCISE3_LL_FILE_H_

// This header file declares some functions that we're adding
// to our linked list implementation.  The functions allow
// customers to write a linked list to a file, or read a linked
// list from a file.

#include "ll.h"
#include <stdint.h>

// Defines a function pointer used to convert a payload into
// a malloc'ed byte array during WriteToFile().  WriteToFile()
// will free the malloc'ed array when it is done with it.
typedef void *(*PayloadToByteArrayFnPtr)(void *payload, uint32_t *arraylen);

// Defines a function pointer used to convert a read byte array
// into a (possibly malloc'ed) payload during ReadFromFile().
// WriteTofile() will free the byte array.
typedef void *(*ByteArrayToPayloadFnPtr)(void *array, uint32_t arraylen);

// Pickles up and writes the list "list" to the file "filename",
// using "f" to convert payloads into fwrite'able byte arrays.
// Will create and/or truncate "filename".  Returns 1 on success,
// 0 on failure.
int WriteToFile(LinkedList list,
                char *filename,
                PayloadToByteArrayFnPtr f);

// Reads a list from file "filename", using "f" to convert byte
// arrays read from the file into payloads.  Returns a LinkedList,
// or NULL on failure.
LinkedList ReadFromFile(char *filename,
                        ByteArrayToPayloadFnPtr f);

#endif  // _LEC9_EXERCISE3_LL_FILE_H_