#ifndef WIN32
#include <unistd.h>
#endif

#include <winport.h>

#include <general.h>
#include <libdbox.h>
#include <logf.h>
#include <lock.h>
#include <profile.h>

// **********************************************************************
// *                                                                    *
// *                                                                    *
// *                                                                    *
// **********************************************************************

void pt::setname(const std::string &_name)
 {
  strmaxcpy(name, _name.c_str(), TEXTLEN-1);
 }

void pt::copy_from(pt *profile)
 {
  clear();

  strmaxcpy(name, profile->name, TEXTLEN-1);
  pconfig=profile->pconfig;

  setfilename();

  for(unsigned i=0; i<profile->data_elements; i++)
    set(profile->data[i].id, profile->data[i].data);
 }

bool pt::read()
 {
  clear();

  if(name[0]==0) return TRUE;

  setfilename();

  return readfile(filename);
 }

bool pt::write()
 {
  char tmpfilename[DIRLEN];

  if(!locked)
    logf.internalerror("pt::write", "profile not locked");

  if(name[0]==0) return TRUE;

  setfilename();

  snprintf(tmpfilename, DIRLEN, "%s.tmp", filename);

  if(writefile(tmpfilename))
   {
    unlink(tmpfilename);
    char errormsg[DIRLEN];
    snprintf(errormsg, DIRLEN, "writefile failed \"%s\"", tmpfilename);
    logf.internalerror("pt::write", errormsg);
    // will not return
    return TRUE;
   }

  if(rename(tmpfilename, filename)!=0)
   {
    unlink(tmpfilename);
    logf.internalerror("pt::write", "rename failed");
    // will not return
    return TRUE;
   }

  return FALSE;
 }

bool pt::lock()
 {
  return lock("");
 }
 
bool pt::lock(const char *module)
 {
  setfilename();

  if(locked)
    logf.internalerror("pt::lock", "profile locked");

  if(locktype[0]==0)
    logf.internalerror("pt::lock", "no lock type");

  if(name[0]==0)
    logf.internalerror("pt::lock", "no lock name");

  if(::lock(locktype, name, pconfig->boxname, module)) return TRUE;

  locked=TRUE;

  read();

  return FALSE;
 }

bool pt::unlock()
 {
  if(!locked) 
    logf.internalerror("pt::unlock", "profile not locked");

  if(::unlock(locktype, name, pconfig->boxname)) return TRUE;

  locked=FALSE;

  return FALSE;
 }

const char *pt::getpath() const
 {
  return path;
 }
  
const char *pt::getfilename() const
 {
  return filename;
 }
  
void pt::initialize()
 {
  textprofilet::initialize();
  pconfig=&config;
  locked=FALSE;
  filename[0]=name[0]=path[0]=locktype[0]=0;
 }

pt::pt()
 {
  initialize();
 }

pt::pt(const char *_name)
 {
  initialize();
  strmaxcpy(name, _name, TEXTLEN-1);
 }

pt::~pt()
 {
  //textprofilet::~textprofilet();
 }

const char *pt::do_get(const char *id) const
 {
  if(stricmp(id, "name")==0)
    return name;

  return textprofilet::do_get(id);
 }

char *pt::do_get_ptr(const char *id)
 {
  if(stricmp(id, "name")==0)
    return name;

  return textprofilet::do_get_ptr(id);  
 }

void pt::do_set(const char *id, const char *text)
 {
  if(stricmp(id, "name")==0)
    return;

  textprofilet::do_set(id, text);
 }


syntax highlighted by Code2HTML, v. 0.9.1