#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <mtool.h>

using namespace std;

bool messaget::copybody(FILE *outfile) const
 {
  return fwrite(data+headerlen, 1, len, outfile)!=len;
 }

bool messaget::copybody(std::ostream &out) const
 {
  return out.write(data+headerlen, len)==NULL;
 }

bool messaget::copy(FILE *outfile) const
 {
  return fwrite(data, 1, len+headerlen, outfile)!=len+headerlen;
 }

bool messaget::copy(std::ostream &out) const
 {
  return out.write(data, len+headerlen)==NULL;
 }

bool messaget::copyheader(FILE *outfile) const
 {
  return fwrite(data, 1, headerlen, outfile)!=headerlen;
 }

bool messaget::copyheader(std::ostream &out) const
 {
  return out.write(data, headerlen)==NULL;
 }

bool messaget::findheader(const char **start,
                          const char *id,
                          const char **text) const
 {
  char tempstr[200];
  const char *_id, *_text;

  while(TRUE)
   {
    if(parseline(start, &_id, &_text)) return TRUE;
    if(_id==NULL) return TRUE;
    copyheaderid(tempstr, _id, 199);

    if(stricmp(tempstr, id)==0)
     {
      *text=_text;
      return FALSE;
     }
   }
 }

bool messaget::findsingleheader(char *dest, const char *id, 
                                size_t maxlen) const
 {
  const char *start=data, *text;

  if(findheader(&start, id, &text))
   {
    dest[0]=0;
    return TRUE;
   }

  copyheadertext(dest, text, maxlen);

  return FALSE;
 }

bool messaget::findsingleheader(string &dest, const char *id) const
 {
  const char *start=data, *text;

  if(findheader(&start, id, &text))
   {
    dest="";
    return TRUE;
   }

  copyheadertext(dest, text);

  return FALSE;
 }

bool messaget::findsingleheader(string &dest, const string &id) const
 {
  return findsingleheader(dest, id.c_str());
 }

bool getline(messaget *m, char *line, size_t maxlen, const char **ptr)
 {
  *line=0;

  while(TRUE)
   {
    if(*ptr>=m->endofmail) return TRUE;

    if(**ptr=='\n')
     {
      (*ptr)++;
      return FALSE;
     }

    if(**ptr!='\r' && strlen(line)<(maxlen-1))
     {
      *line=**ptr;
      line++;
      *line=0;
     }

    (*ptr)++;
   }
 }

bool getline(const messaget &m, string &line, const char *&ptr)
 {
  line="";

  line.reserve(100);

  while(TRUE)
   {
    if(ptr>=m.endofmail) return TRUE;

    if(*ptr=='\n')
     {
      ptr++;
      return FALSE;
     }

    if(*ptr!='\r')
      line+=*ptr;

    ptr++;
   }
 }


syntax highlighted by Code2HTML, v. 0.9.1