/* $Id: s,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
*/

/*
 *  Template for C library functions scanf, sprintf, sqrt, sscanf, strcat, 
 *  strchr, strcmp, strcpy, strlen, strncat, strncmp, and strncpy.
 */

#define scanf \n\
cScanf (char *__fmt,...) {\n\
  EXPR_PARSER *parser;  \n\
  OBJECT *result_object; \n\
  parser = __ctalkGetExprParserAt (__ctalkGetExprParserPtr ()); \n\
  result_object = \n\
    __ctalkLibcFnWithMethodVarArgs ((int (*)())scanf, parser -> e_method, "Integer"); \n\
  return result_object; \n\
}\n\

#define sprintf \n\
cSprintf (char *__s, char *__fmt,...) {\n\
  EXPR_PARSER *parser;  \n\
  OBJECT *result_object; \n\
  parser = __ctalkGetExprParserAt (__ctalkGetExprParserPtr ()); \n\
  result_object = \n\
    __ctalkLibcFnWithMethodVarArgs ((int (*)())sprintf, parser -> e_method, "Integer"); \n\
  return result_object; \n\
}\n\

#define sscanf \n\
cSscanf (char *__s, char *__fmt,...) {\n\
  EXPR_PARSER *parser;  \n\
  OBJECT *result_object; \n\
  parser = __ctalkGetExprParserAt (__ctalkGetExprParserPtr ()); \n\
  result_object = \n\
    __ctalkLibcFnWithMethodVarArgs ((int (*)())sscanf, parser -> e_method, "Integer"); \n\
  return result_object; \n\
}\n\

#define strcat \n\
cStrcat (char *__s1, char *__s2) { \n\
  if (__ctalkIsObject (ARG(0))) \n\
    strcat (__ctalk_to_c_char_ptr(ARG(1)),__ctalk_to_c_char_ptr (ARG(0))); \n\
  else \n\
    strcat ((char *)ARG(1), (char *)ARG(0)); \n\
  return ARG(1);\n\
}

#define strchr \n\
cStrchr (char *__s1, char __c) { \n\
  char *p; \n\
  if (__ctalkIsObject (ARG(0))) \n\
    p = strchr (__ctalk_to_c_char_ptr(ARG(1)),__ctalk_to_c_char (ARG(0))); \n\
  else \n\
    p = strchr ((char *)ARG(1), ((char *)ARG(0))[0]); \n\
  return __ctalkCreateObjectInit ("result", "String", \n\
		       "Character", LOCAL_VAR, p); \n\
}

#define strcasecmp \n\
cStrcasecmp (char *__s1, char *__s2) { \n\
  int r; \n\
  char s[255]; \n\
  if (__ctalkIsObject (ARG(0))) \n\
    r = strcasecmp (__ctalk_to_c_char_ptr(ARG(1)),__ctalk_to_c_char_ptr (ARG(0))); \n\
  else \n\
    r = strcasecmp ((char *)ARG(1), (char *)ARG(0)); \n\
  __ctalkDecimalIntegerToASCII (r, s); \n\
  return __ctalkCreateObjectInit ("result", "Integer", \n\
		       "Magnitude", LOCAL_VAR, s); \n\
}

#define strcmp \n\
cStrcmp (char *__s1, char *__s2) { \n\
  int r; \n\
  char s[255]; \n\
  if (__ctalkIsObject (ARG(0))) \n\
    r = strcmp (__ctalk_to_c_char_ptr(ARG(1)),__ctalk_to_c_char_ptr (ARG(0))); \n\
  else \n\
    r = strcmp ((char *)ARG(1), (char *)ARG(0)); \n\
  __ctalkDecimalIntegerToASCII (r, s); \n\
  return __ctalkCreateObjectInit ("result", "Integer", \n\
		       "Magnitude", LOCAL_VAR, s); \n\
}

#define strcpy \n\
cStrcpy (char *__s1, char *__s2) { \n\
  if (__ctalkIsObject (ARG(0))) \n\
    strcpy (__ctalk_to_c_char_ptr(ARG(1)),__ctalk_to_c_char_ptr (ARG(0))); \n\
  else \n\
    strcpy ((char *)ARG(1), (char *)ARG(0)); \n\
  return ARG(1);\n\
}

#define strncat \n\
cStrncat (char *__s1, char *__s2, int __n) { \n\
  if (__ctalkIsObject (ARG(0))) \n\
    strncat (__ctalk_to_c_char_ptr(ARG(2)),__ctalk_to_c_char_ptr (ARG(1)), __ctalk_to_c_int(ARG(0))); \n\
  else \n\
    strncat ((char *)ARG(2), (char *)ARG(1), (int)ARG(0)); \n\
  return ARG(2);\n\
}

#define strncasecmp \n\
cStrncasecmp (char *__s1, char *__s2, int __n) { \n\
  int r; \n\
  char s[255]; \n\
  if (__ctalkIsObject (ARG(0))) \n\
    r = strncasecmp (__ctalk_to_c_char_ptr(ARG(2)),__ctalk_to_c_char_ptr (ARG(1)), __ctalk_to_c_int(ARG(0))); \n\
  else \n\
    r = strncasecmp ((char *)ARG(2), (char *)ARG(1), (int)ARG(0)); \n\
  __ctalkDecimalIntegerToASCII (r, s); \n\
  return __ctalkCreateObjectInit ("result", "Integer", \n\
		       "Magnitude", LOCAL_VAR, s); \n\
}

#define strncmp \n\
cStrncmp (char *__s1, char *__s2, int __n) { \n\
  int r; \n\
  char s[255]; \n\
  if (__ctalkIsObject (ARG(0))) \n\
    r = strncmp (__ctalk_to_c_char_ptr(ARG(2)),__ctalk_to_c_char_ptr (ARG(1)), __ctalk_to_c_int(ARG(0))); \n\
  else \n\
    r = strncmp ((char *)ARG(2), (char *)ARG(1), (int)ARG(0)); \n\
  __ctalkDecimalIntegerToASCII (r, s); \n\
  return __ctalkCreateObjectInit ("result", "Integer", \n\
		       "Magnitude", LOCAL_VAR, s); \n\
}

#define strncpy \n\
cStrncpy (char *__s1, char *__s2, int __n) { \n\
  if (__ctalkIsObject (ARG(0))) \n\
    strncpy (__ctalk_to_c_char_ptr(ARG(2)),__ctalk_to_c_char_ptr (ARG(1)), __ctalk_to_c_int(ARG(0))); \n\
  else \n\
    strncpy ((char *)ARG(2), (char *)ARG(1), (int)ARG(0)); \n\
  return ARG(2);\n\
}

#define strlen \n\
cStrlen (void *__s) {\n\
  char buf[MAXLABEL]; \n\
  if (__ctalkIsObject (ARG(0))) \n\
    sprintf (buf, "%d", strlen (__ctalk_to_c_char_ptr (ARG(0)))); \n\
  else \n\
    sprintf (buf, "%d", strlen ((char *)ARG(0))); \n\
  return __ctalkCreateObjectInit ("result", "Integer", \n\
		       "Magnitude", LOCAL_VAR, buf); \n\
} \n\


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

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

#define strerror \n\
cStrerror (int _e) { \n\
  char buf[MAXLABEL]; \n\
  strcpy (buf, strerror(_e)); \n\
  return __ctalkCreateObjectInit ("result", "Float", \n\
                                  "Magnitude", LOCAL_VAR, buf); \n\
}
