packages feed

fast-arithmetic-0.6.4.3: .atspkg/contrib/ats-includes-0.3.13/prelude/CATS/filebas.cats

/***********************************************************************/
/*                                                                     */
/*                         Applied Type System                         */
/*                                                                     */
/***********************************************************************/

/* (*
** ATS/Postiats - Unleashing the Potential of Types!
** Copyright (C) 2010-2015 Hongwei Xi, ATS Trustful Software, Inc.
** All rights reserved
**
** ATS is free software;  you can  redistribute it and/or modify it under
** the terms of  the GNU GENERAL PUBLIC LICENSE (GPL) as published by the
** Free Software Foundation; either version 3, or (at  your  option)  any
** later version.
**
** ATS 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  ATS;  see the  file COPYING.  If not, please write to the
** Free Software Foundation,  51 Franklin Street, Fifth Floor, Boston, MA
** 02110-1301, USA.
*) */

/* ****** ****** */

/*
(* Author: Hongwei Xi *)
(* Start time: January, 2013 *)
(* Authoremail: hwxiATcsDOTbuDOTedu *)
*/

/* ****** ****** */

/*
** Source:
** $PATSHOME/prelude/CATS/CODEGEN/filebas.atxt
** Time of generation: Wed Oct 10 21:08:57 2018
*/

/* ****** ****** */

#ifndef ATSLIB_PRELUDE_CATS_FILEBAS
#define ATSLIB_PRELUDE_CATS_FILEBAS

/* ****** ****** */

#include <stdio.h>
#include <string.h>
#include <sys/stat.h>

/* ****** ****** */

#define atspre_FILE_stdin stdin
#define atspre_FILE_stdout stdout
#define atspre_FILE_stderr stderr

/* ****** ****** */

#define atspre_file_mode_r "r"
#define atspre_file_mode_rr "r+"
#define atspre_file_mode_w "w"
#define atspre_file_mode_ww "w+"
#define atspre_file_mode_a "a"
#define atspre_file_mode_aa "a+"

/* ****** ****** */

ATSinline()
atstype_bool
atspre_test_file_exists
  (atstype_string path)
{
  int err;
  struct stat st ;
  err = stat((const char*)path, &st) ;
  return
  (err==0 ? atsbool_true : atsbool_false) ;
} // end of [atspre_test_file_exists]

/* ****** ****** */

ATSinline()
atstype_int
atspre_test_file_mode_fun
(
  atstype_string path, atstype_funptr pred
)
{
  int err;
  struct stat st ;
  err = stat ((const char*)path, &st) ;
  if (err < 0) return -1 ;
  return
  ((atstype_bool(*)(atstype_uint))(pred))(st.st_mode) ? 1 : 0 ;
}

/* ****** ****** */

ATSinline()
atstype_bool
atspre_test_file_isreg
  (atstype_string path)
{
  int err;
  struct stat st ;
  err = stat ((const char*)path, &st) ;
  if (err < 0) return -1 ;
  return (S_ISREG(st.st_mode)) ? 1 : 0 ;
} // end of [atspre_test_file_isreg]

/* ****** ****** */

ATSinline()
atstype_int
atspre_test_file_isdir
  (atstype_string path)
{
  int err;
  struct stat st ;
  err = stat ((const char*)path, &st) ;
  if (err < 0) return -1 ;
  return (S_ISDIR(st.st_mode)) ? 1 : 0 ;
} // end of [atspre_test_file_isdir]

/* ****** ****** */

ATSinline()
atstype_int
atspre_test_file_isblk
  (atstype_string path)
{
  int err;
  struct stat st ;
  err = stat ((const char*)path, &st) ;
  if (err < 0) return -1 ;
  return (S_ISBLK(st.st_mode)) ? 1 : 0 ;
} // end of [atspre_test_file_isblk]

/* ****** ****** */

ATSinline()
atstype_int
atspre_test_file_ischr
  (atstype_string path)
{
  int err;
  struct stat st ;
  err = stat ((const char*)path, &st) ;
  if (err < 0) return -1 ;
  return (S_ISCHR(st.st_mode)) ? 1 : 0 ;
} // end of [atspre_test_file_ischr]

/* ****** ****** */

ATSinline()
atstype_int
atspre_test_file_isfifo
  (atstype_string path)
{
  int err;
  struct stat st ;
  err = stat ((const char*)path, &st) ;
  if (err < 0) return -1 ;
  return (S_ISFIFO(st.st_mode)) ? 1 : 0 ;
} // end of [atspre_test_file_isfifo]

/* ****** ****** */

ATSinline()
atsvoid_t0ype
atspre_fileref_close
  (atstype_ref filr)
{
  int err ;
  err = fclose((FILE*)filr) ;
  if (err < 0) {
    fprintf(
      stderr
    , "exit(ATS): [atspre_fileref_close] failed.\n"
    ) ;
    exit(1) ;
  }
  return ;
} // end of [atspre_fileref_close]

/* ****** ****** */

ATSinline()
atsvoid_t0ype
atspre_fileref_flush
  (atstype_ref filr)
{
  int err ;
  err = fflush((FILE*)filr) ;
  if (err < 0) {
    fprintf(
      stderr
    , "exit(ATS): [atspre_fileref_fflush] failed.\n"
    ) ;
    exit(1) ;
  }
  return ;
} // end of [atspre_fileref_flush]

/* ****** ****** */

ATSinline()
atstype_ref
atspre_fileref_open_exn
  (atstype_string path, atstype_string fm)
{
  FILE* filr ;
  filr = fopen((char*)path, (char*)fm) ;
  if (!filr) {
    fprintf(
      stderr
    , "exit(ATS): [atspre_fileref_open_exn(%s, %s)] failed.\n"
    , (char*)path, (char*)fm
    ) ;
    exit(1) ;
  }
  return filr ;
} // end of [atspre_fileref_open]

/* ****** ****** */

ATSinline()
atstype_int
atspre_fileref_getc
  (atstype_ref filr)
  { return fgetc((FILE*)filr) ; }
// end of [atspre_fileref_getc]

/* ****** ****** */
//
ATSinline()
atsvoid_t0ype
atspre_fileref_putc
(
  atstype_ref filr, atstype_int c
) {
  fputc (c, (FILE*)filr) ; return ;
} // end of [atspre_fileref_putc]
//
#define \
atspre_fileref_putc_int atspre_fileref_putc
#define \
atspre_fileref_putc_char atspre_fileref_putc
//
/* ****** ****** */

ATSinline()
atsvoid_t0ype
atspre_fileref_puts
( atstype_ref fout
, atstype_ptr data)
{
  fputs((char*)data, (FILE*)fout) ; return ;
} // end of [atspre_fileref_puts]

/* ****** ****** */

ATSinline()
atstype_bool
atspre_fileref_is_eof
  (atstype_ref filr)
{
  int eof ;
  eof = feof((FILE*)filr) ;
  return
  (eof != 0 ? atsbool_true : atsbool_false) ;
} // end of [atspre_fileref_is_eof]

/* ****** ****** */

ATSinline()
atstype_bool
atspre_fileref_load_int
(atstype_ref finp, atstype_ref data)
{
  int n ;
  n = fscanf
      ( (FILE*)finp
      , "%i"
      , (atstype_int*)data) ;
  return
  (n==1 ? atsbool_true : atsbool_false) ;
} // end of [atspre_fileref_load_int]

ATSinline()
atstype_bool
atspre_fileref_load_uint
(atstype_ref finp, atstype_ref data)
{
  int n ;
  n = fscanf
      ( (FILE*)finp
      , "%u"
      , (atstype_uint*)data) ;
  return
  (n==1 ? atsbool_true : atsbool_false) ;
} // end of [atspre_fileref_load_uint]

/* ****** ****** */

ATSinline()
atstype_bool
atspre_fileref_load_lint
(atstype_ref finp, atstype_ref data)
{
  int n ;
  n = fscanf
      ( (FILE*)finp
      , "%li"
      , (atstype_lint*)data) ;
  return
  (n==1 ? atsbool_true : atsbool_false) ;
} // end of [atspre_fileref_load_lint]

ATSinline()
atstype_bool
atspre_fileref_load_ulint
(atstype_ref finp, atstype_ref data)
{
  int n ;
  n = fscanf
      ( (FILE*)finp
      , "%lu"
      , (atstype_ulint*)data) ;
  return
  (n==1 ? atsbool_true : atsbool_false) ;
} // end of [atspre_fileref_load_ulint]

/* ****** ****** */

ATSinline()
atstype_bool
atspre_fileref_load_float
(atstype_ref finp, atstype_ref data)
{
  int n ;
  n = fscanf
      ( (FILE*)finp
      , "%f"
      , (atstype_float*)data) ;
  return
  (n==1 ? atsbool_true : atsbool_false) ;
} // end of [atspre_fileref_load_float]

ATSinline()
atstype_bool
atspre_fileref_load_double
  (atstype_ref finp, atstype_ref data)
{
  int n ;
  n = fscanf
      ( (FILE*)finp
      , "%lf"
      , (atstype_double*)data) ;
  return
  (n==1 ? atsbool_true : atsbool_false) ;
} // end of [atspre_fileref_load_double]

/* ****** ****** */

extern
atstype_ptr
atspre_fileref_get_line_string_main2
(
  atstype_int bsz // int bsz
, atstype_ptr filp // FILE*filp
, atstype_ref nlen // (int)*nlen
) ; // endfun

/* ****** ****** */

#endif // ifndef ATSLIB_PRELUDE_CATS_FILEBAS

/* ****** ****** */

/* end of [filebas.cats] */