Exemplo n.º 1
0
extern int binary_ladder_save(t_binary_ladder_types type, unsigned int paracount, t_cb_get_from_ladder _cb_get_from_ladder)
{ int results[10];
  int rank = 1;
  const char * ladder_name;
  const char * filename;
  int checksum, count;
  FILE * fp;

  if ((!(ladder_name = binary_ladder_type_to_filename(type))) || 
      (!(filename = create_filename(prefs_get_ladderdir(),ladder_name,"_LADDER"))))
  {
    eventlog(eventlog_level_error,__FUNCTION__,"NULL filename -  aborting");
    return -1;  
  }

  if (!(fp = fopen(filename,"wb")))
  {
    eventlog(eventlog_level_error,__FUNCTION__,"could not open file \"%s\" for writing (fopen: %s)",filename,pstrerror(errno));
    dispose_filename(filename);
    return -1;
  }

  results[0] = magick;
  fwrite(results,sizeof(int),1,fp); //write the magick int as header

  checksum = 0;

  while ((*_cb_get_from_ladder)(type,rank,results)!=-1)
  {
    fwrite(results,sizeof(int),paracount,fp);
    for (count=0;count<paracount;count++) checksum+=results[count];
    rank++;
  }

  //calculate a checksum over saved data

  results[0] = checksum;
  fwrite(results,sizeof(int),1,fp); // add checksum at the end

  fclose(fp);
  dispose_filename(filename);
  return 0;
}
Exemplo n.º 2
0
bool
LadderList::saveBinary()
{

  if (saved)
    return true;
  
  std::string filename = prefs_get_ladderdir();
  filename += "/";
  filename += ladderFilename;

  std::ofstream fp (filename.c_str(), std::ios::out | std::ios::binary);

  if (!(fp))
  {
    eventlog(eventlog_level_error,__FUNCTION__,"could not open file \"%s\" for writing (std::ofstream: %s)", filename.c_str(), std::strerror(errno));
    return false;
  }

  writedata(fp,magick); //write the magick int as header

  unsigned int checksum = 0;
  unsigned int results[4];

  for(LList::const_iterator lit(ladder.begin()); lit!=ladder.end(); lit++)
  {
    results[0] = lit->getUid();
    results[1] = lit->getSecondary();
    results[2] = lit->getPrimary();
    results[3] = 0;
    writedata(fp,results,4);
      
    //calculate a checksum over saved data
    for (int count=0;count<4;count++) checksum+=results[count];
  }

  writedata(fp,checksum); // add checksum at the en

  saved = true;
  eventlog(eventlog_level_info,__FUNCTION__,"successfully saved %s",filename.c_str());
  return true;
}
Exemplo n.º 3
0
bool
LadderList::loadBinary()
{
  std::string filename = prefs_get_ladderdir();
  filename += "/";
  filename += ladderFilename;

  std::ifstream fp (filename.c_str(), std::ios::in | std::ios::binary);

  if (!(fp))
  {
    eventlog(eventlog_level_info,__FUNCTION__,"could not open ladder file \"%s\" - maybe ladder still empty (std::ifstream: %s)",filename.c_str(),std::strerror(errno));
    return false;
  }

  unsigned int checksum=0;
  readdata(fp,checksum);
  
  if (checksum != magick)
  {
    eventlog(eventlog_level_error,__FUNCTION__,"%s not starting with magick",ladderFilename.c_str());
    return false;
  }

  struct stat sfile;
  if (stat(filename.c_str(), &sfile)<0)
  {
    eventlog(eventlog_level_error,__FUNCTION__,"failed to retrieve size of %s",ladderFilename.c_str());
    return false;
  }

  unsigned int filesize = sfile.st_size;
  if (filesize%sizeof(unsigned int)!=0)
  {
    eventlog(eventlog_level_error,__FUNCTION__,"%s has unexpected size of %u bytes (not multiple of sizeof(unsigned int)",ladderFilename.c_str(),filesize);
    return false;
  }

  unsigned int noe = (filesize)/sizeof(unsigned int) - 2;

  if (noe%4!=0)
  {
    eventlog(eventlog_level_error,__FUNCTION__,"%s has unexpected count of entries (%u) ",ladderFilename.c_str(),noe);
    return false;
  }

  checksum = 0;
  unsigned int values[4];
  unsigned int uid, primary, secondary,tertiary;
  
  while (noe>=4)
  {
	  readdata(fp,values, 4);
	  noe-=4;
	  
	  //handle differently dependant on ladderKey->ladderId
	  if (t_account* account =accountlist_find_account_by_uid(values[0]))
	  {
	     uid       = values[0];
	     primary   = values[2];
	     secondary = values[1];
	     tertiary  = values[3];
	     LadderReferencedObject reference(account);
             addEntry(uid, primary, secondary, tertiary, reference);
	  }else{
	    eventlog(eventlog_level_debug,__FUNCTION__,"no known entry for uid %u",values[0]);
	  }
    for (int count=0;count<4;count++) checksum+=values[count];
  }

  unsigned int filechecksum=0;
  readdata(fp,filechecksum);

  if (filechecksum!=checksum)
  {
    eventlog(eventlog_level_error,__FUNCTION__,"%s has invalid checksum... fall back to old loading mode",ladderFilename.c_str());
    ladder.clear();
    return false;
  }

  eventlog(eventlog_level_info,__FUNCTION__,"successfully loaded %s",filename.c_str());
  return true;
}
Exemplo n.º 4
0
extern t_binary_ladder_load_result binary_ladder_load(t_binary_ladder_types type, unsigned int paracount, t_cb_add_to_ladder _cb_add_to_ladder)
{ int values[10];
  const char * ladder_name;
  const char * filename;
  int checksum, count;
  FILE * fp;

  //TODO: load from file and if this fails return binary_ladder_load_failed
  //      then make sure ladder gets loaded somehow else (form accounts)
  //      compare checksum - and if it differs return load_invalid 
  //      then make sure ladder gets flushed and then loaded from accounts
  //      on success don't load from accounts

  if ((!(ladder_name = binary_ladder_type_to_filename(type))) || 
      (!(filename = create_filename(prefs_get_ladderdir(),ladder_name,"_LADDER"))))
  {
    eventlog(eventlog_level_error,__FUNCTION__,"NULL filename -  aborting");
    return load_failed;  
  }

  if (!(fp = fopen(filename,"rb")))
  {
    eventlog(eventlog_level_info,__FUNCTION__,"could not open ladder file \"%s\" - maybe ladder still empty",filename,pstrerror(errno));
    dispose_filename(filename);
    return load_failed;
  }

  if ((fread(values,sizeof(int),1,fp)!=1) ||  (values[0]!=magick))
  {
    eventlog(eventlog_level_error,__FUNCTION__,"ladder file not starting with the magick int");
    dispose_filename(filename);
    fclose(fp);
    return load_failed;
  }

  checksum = 0;

  while (fread(values,sizeof(int),paracount,fp)==paracount)
  {
    (*_cb_add_to_ladder)(type,values);
    for (count=0;count<paracount;count++) checksum+=values[count];
  }

  fread(values,sizeof(int),1,fp); 
  if (feof(fp)==0)
  {
    eventlog(eventlog_level_error,__FUNCTION__,"got data past end.. fall back to old loading mode");
    return illegal_checksum;

  }
  if (values[0]!=checksum)
  {
    eventlog(eventlog_level_error,__FUNCTION__,"ladder file has invalid checksum... fall back to old loading mode");
    return illegal_checksum;
  }

  fclose(fp);
  eventlog(eventlog_level_info,__FUNCTION__,"successfully loaded %s",filename);
  dispose_filename(filename);
  return load_success;

}