#include "const.h"
#include <cstring>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cwchar>
#include <iostream>
#include <sstream>

// napisy zostaj na miejscu (w line), tylko wskazniki sa ustawian
// i zara dopisywane zera s dopisywane

inline
int parsetok(char* line, int* a, int* b, char** c, char** d, char** e, char** f)
{
  char* field;
  if((field=strtok(line,FIELD_SEP))!=NULL)
    *a=atoi(field); // nie sprawdzana poprawnosc
  else
    return 0;
  if((field=strtok(NULL,FIELD_SEP))!=NULL)
    *b=atoi(field); // nie sprawdzana poprawnosc
  else return 1;
  if((*c=strtok(NULL,FIELD_SEP))==NULL) return 2;
  if((*d=strtok(NULL,FIELD_SEP))==NULL) return 3;
  if((*e=strtok(NULL,FIELD_SEP))==NULL) return 4;
  if((*f=strtok(NULL,FIELD_SEP))==NULL) return 6;
  return 6;
}
// wchar_t version
int parsetok(wchar_t* line, int* a, int* b, wchar_t** c, wchar_t** d, wchar_t** e, wchar_t** f)
{
  wchar_t* field;
  if((field=wcstok(line,WFIELD_SEP,NULL))!=NULL)
  {
    std::wistringstream s(field);
    int i = 0;
    s >> i;
    *a=i; // nie sprawdzana poprawnosc
  }
  else
    return 0;
  if((field=wcstok(NULL,WFIELD_SEP,NULL))!=NULL)
  {
    std::wistringstream k(field);
    int j = 0;
    k >> j;
    *b=j; // nie sprawdzana poprawnosc
  }
  else return 1;
  if((*c=wcstok(NULL,WFIELD_SEP,NULL))==NULL) return 2;
  if((*d=wcstok(NULL,WFIELD_SEP,NULL))==NULL) return 3;
  if((*e=wcstok(NULL,WFIELD_SEP,NULL))==NULL) return 4;
  if((*f=wcstok(NULL,WFIELD_SEP,NULL))==NULL) return 6;
  return 6;
}
// napisy s kopiowane
inline
int scantok(const char* line, int* a, int* b, char* c, char* d, char* e=NULL, char* f=NULL)
{
  return sscanf(line," %d %d %s %s %s %s", a, b, c, d, e, f);
}

// wchar_t version
inline
int scantok(const wchar_t* line, int* a, int* b, wchar_t* c, wchar_t* d, wchar_t* e=NULL, wchar_t* f=NULL)
{
  return swscanf(line,L" %d %d %ls %ls %ls %ls", a, b, c, d, e, f);
}

inline
int printtok(char* line, int a, int b, char* c, char* d, char* e, char* f, char* parms)
{
  sprintf(line,"%04d %02d %s %s %s %s `%s\n", a, b, c, d, e, f, parms);
}

// wchar_t version
inline
int printtok(wchar_t* line, int a, int b, wchar_t* c, wchar_t* d, wchar_t* e, wchar_t* f, wchar_t* parms)
{
  swprintf(line,MAX_LINE,L"%04d %02d %ls %ls %ls %ls `%ls\n", a, b, c, d, e, f, parms);
}

inline
int printtok(char* line, int a, int b, char* c, char* d, char* e, char* f)
{
  sprintf(line,"%04d %02d %s %s %s %s\n", a, b, c, d, e, f);
}

// wchar_t version
inline
int printtok(wchar_t* line, int a, int b, wchar_t* c, wchar_t* d, wchar_t* e, wchar_t* f)
{
  swprintf(line,MAX_LINE,L"%04d %02d %ls %ls %ls %ls\n", a, b, c, d, e, f);
}

inline
int printtok(char* line, int a, int b, char* c, char* d)
{
  sprintf(line,"%04d %02d %s %s\n", a, b, c, d);
}

// wchar_t version
inline
int printtok(wchar_t* line, int a, int b, wchar_t* c, wchar_t* d)
{
  swprintf(line,MAX_LINE,L"%04d %02d %ls %ls\n", a, b, c, d);
}
