/*
 * Copyright (c) 1995 Gunther Schadow.  All rights reserved.
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "t2c.h"
#include <string.h>
#include <fstream.h>

void
output_inline(const char *owner, char *name, const char* type,
	      const struct decl_rec *decl)
{
  resetnames();

  char filename[strlen(name) + sizeof(".h") + 1];
  strcpy(filename, classname(name));
  strcat(filename, ".icc");
  fstream out(filename, ios::out);

  out
    << "/*" << endl
    << " * This file was generated by t2c. Copyright (c) 1995 Gunther Schadow."
      << endl
    << " *" << endl
    << " * " << owner << "." << name << " (" << type << ")" << endl
    << " */" << endl
    << endl
    << "#ifdef OUTLINE" << endl
    << "# define inline" << endl
    << "#endif" << endl
    << endl;

  const struct decl_rec *p;

  /*
   * set_...() methods
   */

  for(p = decl; p != NULL; p = p->next)
    {
      out
	<< "inline void" << endl
	<< classname(name) << "::set_" << p->name << "(" << p->type->c_type;

      if(p->size)
	if(strcmp(p->type->c_type, "char") == 0)
	  {
	    out
	      << " *" << "x)" << endl
	      << "{" << endl
	      << "  strncpy( V_" << p->name << ", x, S_" << p->name << ");"
		<< endl;
	  }
	else
	  {
	    cerr << "sorry, not implemented: arrays of non-chars" << endl;
	    exit(1);
	  }
      else
	out
	  << " x)" << endl
	  << "{" << endl
	  << "  V_" << p->name << " = x;" << endl;

      out
	<< "  I_" << p->name << " = I_NON_NULL;" << endl
	<< "}" << endl
	<< endl;
    }

  out
    << endl;
      
  /*
   * get_...() methods
   */

  for(p = decl; p != NULL; p = p->next)
    {
      out
	<< "inline " << p->type->c_type << ' ';
      if(p->size) out
	<< '*';
      out
	<< endl
	<< classname(name) << "::get_" << p->name << "()" << endl
	<< "{" << endl
	<< "  return V_" << p->name << ';' << endl
	<< "}" << endl
        << endl;
    }

  out
    << endl;
      
  /*
   * unset_...() methods
   */

  for(p = decl; p != NULL; p = p->next)
    {
      out
	<< "inline void" << endl
	<< classname(name) << "::unset_" << p->name << "()" << endl
	<< "{" << endl
	<< "  I_" << p->name << " = I_NULL;" << endl
	<< "}" << endl
	<< endl;
    }

  out
    << endl;
      
  /*
   * ..._is_null() methods
   */

  for(p = decl; p != NULL; p = p->next)
    {
      out
	<< "inline bool" << endl
        << classname(name) << "::" << p->name << "_is_null()" << endl
	<< "{" << endl
	<< "  return I_IS_NULL(I_" << p->name << ");" << endl
	<< "}" << endl
	<< endl;
    }

  out
    << endl;

  /*
   * owner
   */
  
  out
    << "inline const char *" << endl
    << classname(name) << "::owner(const char *x)" << endl
    << "{" << endl
    << "  const char *old = the_owner;" << endl
    << "  the_owner = x;" << endl
    << "  return old;" << endl
    << "}" << endl
    << endl
    << "inline const char *" << endl
    << classname(name) << "::owner()" << endl
    << "{" << endl
    << "  return the_owner;" << endl
    << "}" << endl
    << endl;

  /*
   * next()
   */

  out
    << "inline result" << endl
    << classname(name) << "::next()" << endl
    << "{" << endl
    << "  if(Database->fetch(Cursor))" << endl
    << "    return SUCCESS;" << endl
    << "  else" << endl
    << "    return FAIL;" << endl
    << "}" << endl
    << endl;

  out
    << "#ifdef OUTLINE" << endl
    << "# undef inline" << endl
    << "#endif" << endl
    << endl;
}
