///
/// @file iofile.h
/// @authors k. headley
/// @date 01 mar 2020

/// Cross-platform Carbon Waveglider file utilities

/// @sa doxygen-examples.c for more examples of Doxygen markup


/////////////////////////
// Terms of use
/////////////////////////
/*
 Copyright Information
  
 Copyright 2002-2020 MBARI
 Monterey Bay Aquarium Research Institute, all rights reserved.
 
 Terms of Use
 
 This program 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. You can access the GPLv3 license at
 http://www.gnu.org/licenses/gpl-3.0.html
 
 This program 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
 (http://www.gnu.org/licenses/gpl-3.0.html)
 
 MBARI provides the documentation and software code "as is", with no warranty,
 express or implied, as to the software, title, non-infringement of third party
 rights, merchantability, or fitness for any particular purpose, the accuracy of
 the code, or the performance or results which you may obtain from its use. You
 assume the entire risk associated with use of the code, and you agree to be
 responsible for the entire cost of repair or servicing of the program with
 which you are using the code.
 
 In no event shall MBARI be liable for any damages, whether general, special,
 incidental or consequential damages, arising out of your use of the software,
 including, but not limited to, the loss or corruption of your data or damages
 of any kind resulting from use of the software, any prohibited use, or your
 inability to use the software. You agree to defend, indemnify and hold harmless
 MBARI and its officers, directors, and employees against any claim, loss,
 liability or expense, including attorneys' fees, resulting from loss of or
 damage to property or the injury to or death of any person arising out of the
 use of the software.
 
 The MBARI software is provided without obligation on the part of the
 Monterey Bay Aquarium Research Institute to assist in its use, correction,
 modification, or enhancement.
 
 MBARI assumes no responsibility or liability for any third party and/or
 commercial software required for the database or applications. Licensee agrees
 to obtain and maintain valid licenses for any additional third party software
 required.
 */

// Always do this
#ifndef IOFILE_H
#define IOFILE_H

/////////////////////////
// Includes 
/////////////////////////
#include "cwg-shared.h"
#if defined(__PIC24FJ256GA106__) || defined(__PIC24FJ256GA606__) || defined(__PIC24FJ1024GA606__)
#include "cwgsys.h"
#elif defined(__APPLE__) || defined(__unix__) || defined(__CYGWIN__)
#include <dirent.h>
#elif defined(__WIN32) || defined(__WIN64) 
#include <dirent.h>
#endif

/////////////////////////
// Macros
/////////////////////////
#define IOF_LESS_LINEBUF_LEN 256
#define G_FILEBUF_LEN 1024

/////////////////////////
// Type Definitions
/////////////////////////
typedef enum{
    FP_MODE_HEAD='H', 
    FP_MODE_TAIL='T'
}fp_mode_t;

typedef enum{
    XCAT_MODE_BYTES      =0x0001,
    XCAT_MODE_LINES      =0x0002,
    XCAT_MODE_TIME       =0x0004,
    XCAT_REF_TAIL        =0x0008,
    XCAT_TFMT_ISO        =0x0100,
    XCAT_TFMT_POSIX_MSEC =0x0200,
    XCAT_TFMT_POSIX_SEC  =0x0400,
    XCAT_MODE_FLAGS      =0x0007,
    XCAT_REF_FLAGS       =0x0008,
    XCAT_TFMT_FLAGS      =0x0700
}xcat_flags_t;

typedef struct xcat_config_s{
    xcat_flags_t bflags;
    xcat_flags_t eflags;
    char *start_tok;
    char *stop_tok;
    int verbose;
    // time range
    uint64_t range_t[2];
    // byte,line range/count
    uint32_t range_n[2];
}xcat_config_t;

#define XC_MODE(f) ((f&XCAT_MODE_FLAGS))
#define XC_TFMT(f) ((f&XCAT_TFMT_FLAGS))
#define XC_MODE_LINES(f) ((f&XCAT_MODE_LINES)>0)
#define XC_MODE_BYTES(f) ((f&XCAT_MODE_BYTES)>0)
#define XC_MODE_TIME(f) ((f&XCAT_MODE_TIME)>0)
#define XC_TFMT_ISO(f) ((f&XCAT_TFMT_ISO)>0)
#define XC_TFMT_MSEC(f) ((f&XCAT_TFMT_POSIX_MSEC)>0)
#define XC_TFMT_SEC(f) ((f&XCAT_TFMT_POSIX_SEC)>0)
#define XC_REF_TAIL(f) ((f&XCAT_REF_TAIL)>0)
#define XC_BUF_LEN 256
#define XC_TBUF_LEN 24

/////////////////////////
// Exports
/////////////////////////

#ifdef __cplusplus
extern "C" {
#endif
    
/// @brief Show file system status
/// @param arg sub-directory to scan(none for this project)
/// @return return_code_t
//return_code_t fcShowS_orig(char* arg); 
return_code_t dstat(int argc, char** argv); 

/// @brief list all files
/// @param arg sub-directory to scan(none for this project)
/// @return return_code_t
//return_code_t fcList_orig(char* arg);
return_code_t dlist(int argc, char** argv); 

#ifdef WITH_XCAT
    return_code_t xcat(char *arg, xcat_config_t *cfg);
    void xcat_help();
#else
	return_code_t fcat(char *arg, byte mode, size_t len);
#endif

/// @brief rename a file 
/// @param arg old name, new name
/// @returns errCode
//return_code_t fcRename_orig(char* arg);
return_code_t frename(int argc, char** argv); 
/// @brief delete a file
/// @param arg file name
/// @return return_code_t
//return_code_t fcUnlink_orig(char* arg);
return_code_t fdelete(int argc, char** argv); 
/// @brief copy a file
/// @param arg old name, new name
/// @return return_code_t
//return_code_t fcCopy_orig(char* arg);
return_code_t fcopy(int argc, char** argv); 

#define IOF_LESS_PAGE_LINES 40

/// @brief minimal file pager
/// @param fpath file path
/// @param page_rows number of lines per page
/// @return return_code_t
return_code_t fless(char *fpath, uint32_t page_rows) ;

#ifdef __cplusplus
}
#endif

#endif // THIS_FILE




