/* $Id: f,v 1.1.1.1 2008/11/28 17:57:26 kiesling Exp $ -*-c-*-**/

/*
  This file is part of ctalk.
  Copyright  2005-2008 Robert Kiesling, ctalk@ctalklang.org.
  Permission is granted to copy this software provided that this copyright
  notice is included in all source code modules.

  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 2 of the License, or
  (at your option) any later version.

  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.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/*
 *  Templates for C library functions fabs, fabsf, fopen, and fscanf.
 */

#define fabs \n\
\n\
cFabs (double d) { \n\
  char buf[MAXLABEL]; \n\
  sprintf (buf, "%f", fabs (__ctalk_to_c_double (ARG(0)))) ; \n\
  return __ctalkCreateObjectInit ("result", "Float", \n\
     "Magnitude", LOCAL_VAR, buf); \n\
} \n\



#define fabsf \n\
cFabsf (double d) { \n\
  char buf[MAXLABEL]; \n\
  sprintf (buf, "%f", fabsf (__ctalk_to_c_double (ARG(0)))) ; \n\
  return __ctalkCreateObjectInit ("result", "Float", \n\
     "Magnitude", LOCAL_VAR, buf); \n\
}\n

#define fopen \n\
cFopen (char *path, char *mode) { \n\
  char buf[MAXLABEL]; \n\
  FILE *f; \n\
  OBJECT *file_object = NULL; \n\
\n\
  if (!strcmp (__ctalk_to_c_char_ptr (ARG(0)), "r")) { \n\
    if ((f = fopen (__ctalk_to_c_char_ptr (ARG(1)),  \n\
		    __ctalk_to_c_char_ptr (ARG(0)))) != NULL) { \n\
      sprintf (buf, "0x%p", (void *)f); \n\
      file_object =  \n\
	__ctalkCreateObjectInit ("result", "ReadFileStream", "FileStream", \n\
				    LOCAL_VAR, buf); \n\
    } \n\
  } \n\
\n\
  if (!strcmp (__ctalk_to_c_char_ptr (ARG(0)), "w") || \n\
      !strcmp (__ctalk_to_c_char_ptr (ARG(0)), "a")) { \n\
    if ((f = fopen (__ctalk_to_c_char_ptr (ARG(1)), "a")) != NULL) { \n\
      sprintf (buf, "0x%p", (void *)f); \n\
      file_object = \n\
	__ctalkCreateObjectInit ("result", "WriteFileStream", "FileStream", \n\
				    LOCAL_VAR, buf); \n\
    } \n\
  } \n\
\n\
  if (!strcmp (__ctalk_to_c_char_ptr (ARG(0)), "r+") || \n\
      !strcmp (__ctalk_to_c_char_ptr (ARG(0)), "w+") || \n\
      !strcmp (__ctalk_to_c_char_ptr (ARG(0)), "a+")) { \n\
    if ((f = fopen (__ctalk_to_c_char_ptr (ARG(1)), "a+")) != NULL) { \n\
      sprintf (buf, "0x%p", (void *)f); \n\
      file_object = \n\
	__ctalkCreateObjectInit ("result", "ReadWriteStream", "FileStream",\n\
				    LOCAL_VAR, buf);\n\
    }\n\
  }\n\
\n\
  if (!file_object) {\n\
    if (!__ctalkErrnoInternal ()) {\n\
      __ctalkExceptionInternal (NULL, unknown_file_mode_x, \n\
				      __ctalk_to_c_char_ptr (mode));\n\
    } else {\n\
      __ctalkSysErrExceptionInternal (NULL, __ctalkErrnoInternal (), \n\
	__ctalk_to_c_char_ptr(path));\n\
    }\n\
  }\n\
\n\
  return file_object;\n\
}\n\

#define fprintf \n\
cFprintf (FILE *f, char *fmt,...) {\n\
  EXPR_PARSER *parser;  \n\
  OBJECT *result_object; \n\
  OBJECT *arg0; \n\
  parser = __ctalkGetExprParserAt (__ctalkGetExprParserPtr ()); \n\
  if (strcmp (parser->e_method->args[0]->__o_classname, "Expr") && \n\
      strcmp (parser->e_method->args[0]->__o_classname, "WriteFileStream")){\n\
   _warning \n\
    ("cFprintf: Stream argument is not a WriteFileStream or FileStream expression object.\n");\n\
    return NULL; \n\
  } \n\
  result_object = \n\
    __ctalkLibcFnWithMethodVarArgs ((int (*)())fprintf, parser -> e_method, "Integer"); \n\
  return result_object; \n\
}\n\

#define fscanf \n\
cFscanf (FILE *s, char *fmt,...) {\n\
  EXPR_PARSER *parser;  \n\
  OBJECT *result_object; \n\
  parser = __ctalkGetExprParserAt (__ctalkGetExprParserPtr ()); \n\
  result_object = \n\
    __ctalkLibcFnWithMethodVarArgs ((int (*)())fscanf, parser -> e_method, "Integer"); \n\
  return result_object; \n\
}\n\

