<commit_before>/* bzflag
 * Copyright (c) 1993-2011 <NAME>
 *
 * This package is free software;  you can redistribute it and/or
 * modify it under the terms of the license found in the file
 * named COPYING that should have accompanied this file.
 *
 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#include "common.h"

/* system headers */
#include <string>
#include <string.h>

/* interface header */
#include "ServerItem.h"

/* common implementation headers */
#include "TextUtils.h"
#include "AnsiCodes.h"

/* local implementation headers */
#include "ServerListCache.h"


ServerItem::ServerItem() :  updateTime(0), cached(false), favorite(false)
{
}

void ServerItem::writeToFile(std::ostream& out) const
{
  char buffer[ServerListCache::max_string+1]; // ServerListCache::max_string is inherited from ServerListCache.h

  // write out desc.
  memset(buffer,0,sizeof(buffer));
  int copyLength = int(description.size() < ServerListCache::max_string ? description.size(): ServerListCache::max_string);
  strncpy(&buffer[0],description.c_str(),copyLength);
  out.write(buffer,sizeof(buffer));

  // write out name
  memset(buffer,0,sizeof(buffer));
  copyLength = int(name.size() < ServerListCache::max_string ? name.size(): ServerListCache::max_string);
  strncpy(&buffer[0],name.c_str(),copyLength);
  out.write(buffer,sizeof(buffer));

  // write out pingpacket
  ping.writeToFile(out);

  nboPackUByte(buffer, favorite);
  out.write(buffer, 1);

  // write out current time
  memset(buffer,0,sizeof(buffer));
  nboPackInt(buffer,(int32_t)updateTime);
  out.write(&buffer[0], 4);
}

bool ServerItem::readFromFile(std::istream& in, int subrevision)
{
  char buffer[ServerListCache::max_string+1];

  //read description
  memset(buffer,0,sizeof(buffer));
  in.read(buffer,sizeof(buffer));
  if ((size_t)in.gcount() < sizeof(buffer)) return false; // failed to read entire string
  description = buffer;

  //read name
  memset(buffer,0,sizeof(buffer));
  in.read(buffer,sizeof(buffer));
  if ((size_t)in.gcount() < sizeof(buffer)) return false; // failed to read entire string
  name = buffer;

  bool pingWorked = ping.readFromFile(in);
  if (!pingWorked) return false; // pingpacket failed to read

  // favorites introduced in subrevision 1
  if (subrevision >=1) {
    uint8_t fav;
    in.read(buffer, 1);
    nboUnpackUByte(buffer, fav);
    favorite = (fav != 0);
  }

  // read in time
  in.read(&buffer[0],4);
  if (in.gcount() < 4) return false;
  int32_t theTime;
  nboUnpackInt(&buffer[0],theTime);
  updateTime = (time_t) theTime;
  cached = true;
  return true;
}

// set the last updated time to now
void ServerItem::setUpdateTime()
{
  updateTime = getNow();
}

// get current age in minutes
time_t ServerItem::getAgeMinutes() const
{
  time_t time = (getNow() - updateTime)/(time_t)60;
  return time;
}

// get current age in seconds
time_t ServerItem::getAgeSeconds() const
{
  time_t time = (getNow() - updateTime);
  return time;
}

// get a simple string which describes the age of item
std::string ServerItem::getAgeString() const
{
  std::string returnMe;
  char buffer [80];
  time_t age = getAgeMinutes();
  float fAge;
  if (age < 60){ // < 1 hr
    if (age < 1){
      time_t ageSecs = getAgeSeconds();
      sprintf(buffer,"%-3ld secs",(long)ageSecs);
    } else {
      sprintf(buffer,"%-3ld mins",(long)age);
    }
  } else { // >= 60 minutes
    if (age < (24*60)){ // < 24 hours & > 1 hr<commit_msg>(style) The scope of the variable 'fAge' can be reduced<commit_after>/* bzflag
 * Copyright (c) 1993-2011 <NAME>
 *
 * This package is free software;  you can redistribute it and/or
 * modify it under the terms of the license found in the file
 * named COPYING that should have accompanied this file.
 *
 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#include "common.h"

/* system headers */
#include <string>
#include <string.h>

/* interface header */
#include "ServerItem.h"

/* common implementation headers */
#include "TextUtils.h"
#include "AnsiCodes.h"

/* local implementation headers */
#include "ServerListCache.h"


ServerItem::ServerItem() :  updateTime(0), cached(false), favorite(false)
{
}

void ServerItem::writeToFile(std::ostream& out) const
{
  char buffer[ServerListCache::max_string+1]; // ServerListCache::max_string is inherited from ServerListCache.h

  // write out desc.
  memset(buffer,0,sizeof(buffer));
  int copyLength = int(description.size() < ServerListCache::max_string ? description.size(): ServerListCache::max_string);
  strncpy(&buffer[0],description.c_str(),copyLength);
  out.write(buffer,sizeof(buffer));

  // write out name
  memset(buffer,0,sizeof(buffer));
  copyLength = int(name.size() < ServerListCache::max_string ? name.size(): ServerListCache::max_string);
  strncpy(&buffer[0],name.c_str(),copyLength);
  out.write(buffer,sizeof(buffer));

  // write out pingpacket
  ping.writeToFile(out);

  nboPackUByte(buffer, favorite);
  out.write(buffer, 1);

  // write out current time
  memset(buffer,0,sizeof(buffer));
  nboPackInt(buffer,(int32_t)updateTime);
  out.write(&buffer[0], 4);
}

bool ServerItem::readFromFile(std::istream& in, int subrevision)
{
  char buffer[ServerListCache::max_string+1];

  //read description
  memset(buffer,0,sizeof(buffer));
  in.read(buffer,sizeof(buffer));
  if ((size_t)in.gcount() < sizeof(buffer)) return false; // failed to read entire string
  description = buffer;

  //read name
  memset(buffer,0,sizeof(buffer));
  in.read(buffer,sizeof(buffer));
  if ((size_t)in.gcount() < sizeof(buffer)) return false; // failed to read entire string
  name = buffer;

  bool pingWorked = ping.readFromFile(in);
  if (!pingWorked) return false; // pingpacket failed to read

  // favorites introduced in subrevision 1
  if (subrevision >=1) {
    uint8_t fav;
    in.read(buffer, 1);
    nboUnpackUByte(buffer, fav);
    favorite = (fav != 0);
  }

  // read in time
  in.read(&buffer[0],4);
  if (in.gcount() < 4) return false;
  int32_t theTime;
  nboUnpackInt(&buffer[0],theTime);
  updateTime = (time_t) theTime;
  cached = true;
  return true;
}

// set the last updated time to now
void ServerItem::setUpdateTime()
{
  updateTime = getNow();
}

// get current age in minutes
time_t ServerItem::getAgeMinutes() const
{
  time_t time = (getNow() - updateTime)/(time_t)60;
  return time;
}

// get current age in seconds
time_t ServerItem::getAgeSeconds() const
{
  time_t time = (getNow() - updateTime);
  return time;
}

// get a simple string which describes the age of item
std::string ServerItem::getAgeString() const
{
  std::string returnMe;
  char buffer [80];
  time_t age = getAgeMinutes();
  if (age < 60){ // < 1 hr
    if (age < 1){
      time_t ageSecs = getAgeSeconds();
      sprintf(buffer,"%-3ld secs",(long)ageSecs);
    } else {
      sprintf(buffer,"%-3ld mins",(long)age);
    }
  } else { // >= 60 minutes
    float fAge;
    if (age < (24*60)){ // < 24 hours & > 1 hr
{'fraction_non_alphanumeric': 0.14084117321527392, 'fraction_numerical': 0.01466519092418373, 'mean_word_length': 4.284356725146199, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3359720', 'n_tokens_mistral': 2461, 'n_tokens_neox': 2359, 'n_words': 638}