Exemplo n.º 1
0
int nextServer(int64& seed, std::vector<Server*>& v, int oldIndex)
{
   CERR("oldindex: " << oldIndex);
   CERR("v.size(): " << v.size());
   typedef std::vector<Server*>::iterator iter;
   if(!(oldIndex >= 0 && unsigned(oldIndex) < v.size())
      /* || !v[oldIndex]->isReachable()*/){
      CERR("!("<< oldIndex << " >= 0 && " << 
           unsigned(oldIndex) << " < " << v.size() << ") ||");
      if(oldIndex >= 0 && unsigned(oldIndex) < v.size()){
         CERR(" ! (v[" << oldIndex << "]->isReachable() : " << 
              v[oldIndex]->isReachable() << ")");
      }
      //start searching from first group
      CERR("oldindex: " << oldIndex << ", will set to -1");
      oldIndex = -1;
   }
   sort(v.begin(), v.end(), ServerGroupLess());
   Server cmp("dummy", 12); //dummy server
   if(oldIndex >= 0){
      //set dummy's group to be the last used group.
      cmp.setGroup(v[oldIndex]->group());
      CERR("cmp.group: " << cmp.group());
   }
   int lastGroup = v.back()->group(); //group with highest number.
   CERR("lastgroup: " << lastGroup);
   std::pair<iter,iter> range;
   //find group to use
   while(cmp.group() <= lastGroup){
      //find range of reachables with same group as dummy.
      range = equal_range(v.begin(), v.end(), &cmp, ServerGroupLess());
      if(range.first != range.second) { //range is not empty
         CERR("range.first != range.second");
         CERR("more servers in list");
#define REACHABLE(iter) ((iter != v.end()) ? ((*iter)->isReachable()) : false)
#define HOST(iter) ((iter != v.end()) ? ((*iter)->getHostAndPort()) : "END")
#define GROUP(iter) ((iter != v.end()) ? ((*iter)->group()) : -1)
#define SERVER(iter) CERR(#iter << " : " << distance(v.begin(), iter) << " "<<\
                          HOST(iter) << " - " GROUP(iter) << " " <<\
                          REACHABLE(iter))
#define SERVER_RANGE(b,e) for(iter a_ = b; a_ != e; ++a_){SERVER(a_);}
         SERVER(v.begin());
         SERVER(range.first);
         SERVER(range.second);
         SERVER((v.end() - 1));
         SERVER_RANGE(v.begin(), v.end());
         CERR("begin -> first  : " << distance(v.begin(), range.first));
         CERR("first -> second : " << distance(range.first, range.second));
         CERR("second -> end   : " << distance(range.second, v.end()));
         iter nextgroup = find_if(range.second - 1, v.end(), 
                                  not1(bind2nd(mem_fun(&Server::isGroup),
                                               cmp.group())));
         SERVER(nextgroup);
         CERR("second -> nextgroup : "<<distance(range.second, nextgroup));
         if(distance(range.second, nextgroup) < SWITCH_GROUP_THRESHOLD){
            CERR("unreachables has not passed THRESHOLD");
            break;
         }
#undef SERVER_RANGE
#undef SERVER
#undef HOST
#undef REACHABLE
      }
      //range is empty or has too many unreachables, increase group by
      //one and try again.
      cmp.setGroup(cmp.group() + 1);
      CERR("cmp.group: " << cmp.group());      
   }
   //size of range,
   int num = distance(range.first, range.second);
   CERR("num: " << num);
   //index of first server in selected group
   int startIdx = distance(v.begin(), range.first);
   CERR("startIdx: " << startIdx);
   if(cmp.group() > lastGroup){
      //out of servers, sorry
      CERR("returning " << v.size() << ", which is illegal");
      return v.size();
   } else if(oldIndex >= 0 && v[oldIndex]->isReachable()){
      //step one step in this group.
      CERR("returning " << startIdx+(oldIndex+1)%num << ", oldIndex + 1");
      return startIdx + (oldIndex + 1) % num;
   } else {
      //srand is called in NavServerCom. It seems to be important to
      //call srand and rand in the same thread context.
#ifndef __SYMBIAN32__
      //random index in this group. 
      int ret = startIdx + (rand() / (RAND_MAX / num + 1));
#else
      int ret = startIdx + (Math::Rand(seed) / (KMaxTInt / num + 1));
#endif
      CERR("returning " << ret << ", a random value");
      return ret;
   }
}
Exemplo n.º 2
0
inline string &Settings::rtrim(string &s)
{
	s.erase(find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(), s.end());
	return s;
}
Exemplo n.º 3
0
// Remove trailing whitespace
void
Macro::value_rtrim()
{
	// Took me three hours to arrive at
	value.erase((find_if(value.rbegin(), value.rend(), not1(mem_fun_ref(&Ptoken::is_space)))).base(), value.end());
}
Exemplo n.º 4
0
 string string_ltrim( const string &str )
 {
     string s = str;
     s.erase(s.begin(), find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace))));
     return s;
 }
Exemplo n.º 5
0
bool CustomCodeGenerator::Generate(const FileDescriptor* file,
                                   const string& parameter,
                                   compiler::GeneratorContext* generator_context,
                                   string* error) const
{
    string outfilename = file->name().substr(0, file->name().size() - strlen(".proto"));
    string outheaderfilename =  outfilename + (".pb.rpc.h");
    string outsrcfilename =  outfilename + (".pb.rpc.cc");

    string includefilename = outfilename + ".pb.h";

    string::size_type slastpos = 0;
    string::size_type spos;
    vector<string> ns;
    string spacket = file->package();
    do
    {
        spos = spacket.find(".", slastpos);
        ns.push_back(spacket.substr(slastpos, spos - slastpos));

        slastpos = spos + 1;
    } while (spos != string::npos);

    string identifier;
    for (size_t i=0; i<ns.size(); ++i)
        identifier += ns[i] + "_";
    identifier += outfilename;
    identifier.erase(remove_if(identifier.begin(), identifier.end(), not1(ptr_fun(::is_identifier))), identifier.end());
    identifier += "_rpc";

    scoped_ptr<io::ZeroCopyOutputStream> outputh(generator_context->Open(outheaderfilename));
    scoped_ptr<io::ZeroCopyOutputStream> outputcc(generator_context->Open(outsrcfilename));

    io::Printer printer(outputh.get(), '$');
    io::Printer ccprinter(outputcc.get(), '$');

    printer.Print(
        "// Generated by the protocol buffer compiler.  DO NOT EDIT!\n"
        "// source: $filename$\n"
        "\n"
        "#ifndef PROTOBUF_$filename_identifier$__INCLUDED\n"
        "#define PROTOBUF_$filename_identifier$__INCLUDED\n"
        "\n"
        "\n",
        "filename", file->name(),
        "filename_identifier", identifier);

    printer.Print("#include \"$header$\"\n"
            "#include <string>\n"
            "#include <memory>\n",
            "header", includefilename);
    printer.Print("\n");

    ccprinter.Print(
        "// Generated by the protocol buffer compiler.  DO NOT EDIT!\n"
        "// source: $filename$\n"
        "\n",
        "filename", file->name());
    ccprinter.Print("#include \"$header$\"\n"
            "#include <boost/pool/singleton_pool.hpp>\n",
            "header", outheaderfilename);
    ccprinter.Print("\n");

    for (size_t i=0; i<ns.size(); ++i)
    {
        printer.Print("namespace $part$ {\n",
                      "part", ns[i]);

        ccprinter.Print("namespace $part$ {\n",
                        "part", ns[i]);
    }

    printer.Print("\n\n");
    ccprinter.Print("\n\n");

    const EnumDescriptor* emu_cmdids = NULL;
    for (int i=0; i< file->enum_type_count(); ++i)
    {
        if (file->enum_type(i)->name() == "CmdIDs")
            emu_cmdids = file->enum_type(i);
    }

    for (int i = 0; i < file->service_count(); ++i)
    {
        for (int j=0; j < file->service(i)->method_count(); ++j)
        {
            const MethodDescriptor* m = file->service(i)->method(j);

            printer.Print(
                "class $RPCNAME$\n"
                "{\n"
                "public:\n",
                "RPCNAME", m->name());
            printer.Indent();

            printer.Print(
                "typedef $REQNAME$ RequestType;\n"
                "typedef $RESPNAME$ ResponseType;\n"
                "\n",
                "REQNAME", m->input_type()->name(),
                "RESPNAME", m->output_type()->name());

            string cmdid;
            if (emu_cmdids)
            {
                const EnumValueDescriptor* ev = emu_cmdids->FindValueByName("CmdID_" + m->name());
                if (ev)
                    cmdid = ev->name();
            }

            printer.Print(
                "static const ::std::string& ns() { return ns_; }\n"
                "static const ::std::string& name() { return name_; }\n"
                "static const ::std::string& full_name() { return full_name_; }\n"
                "static bool has_cmd_id() { return $HAS_CMDID$; }\n"
                "static uint32_t cmd_id() { return $CMDID$; }\n"
                "\n"
                "static RequestType& request();\n"
                "static ResponseType& response();\n"
                "\n",
                "HAS_CMDID", cmdid.empty() ? "false" : "true",
                "CMDID", cmdid.empty() ? "0U" : cmdid
               );
            printer.Outdent();
            printer.Print("private:\n");
            printer.Indent();
            printer.Print("static ::std::string ns_;\n"
            		      "static ::std::string name_;\n"
      		              "static ::std::string full_name_;\n");
            printer.Outdent();
            printer.Print("};\n\n\n");

            ccprinter.Print(
                "// $RPCNAME$\n"
                "::std::string $RPCNAME$::ns_ = \"$SERVICENAME$\";\n"
                "::std::string $RPCNAME$::name_ = \"$RPCNAME$\";\n"
                "::std::string $RPCNAME$::full_name_ = \"$SERVICENAME$.$RPCNAME$\";\n",
                "SERVICENAME", m->service()->name(),
                "RPCNAME", m->name());
            ccprinter.Print("\n\n");
        }

    }

    if (!ns.empty())
    {
        for (size_t i=0; i<ns.size(); ++i)
        {
            printer.Print("} // namespace $part$\n",
                          "part", ns[ns.size()-i-1]);
            ccprinter.Print("} // namespace $part$\n",
                            "part", ns[ns.size()-i-1]);
        }
    }
    printer.Print("\n#endif // PROTOBUF_$filename_identifier$__INCLUDED\n",
                  "filename_identifier", identifier);


    return true;
}
wstring StringUtilities::RightTrim(wstring str)
{
    str.erase(find_if(str.rbegin(), str.rend(), not1(std::ptr_fun<int, int>(std::isspace))).base(), str.end());
    return str;
}
Exemplo n.º 7
0
const wchar_t*
ctype<wchar_t>::do_scan_not(mask m,
                            const wchar_t* low, const wchar_t* high) const
{
  return find_if(low, high, not1(_Ctype_w_is_mask(m, ctype<char>::classic_table())));
}
Exemplo n.º 8
0
const wchar_t*
ctype_byname<wchar_t>
  ::do_scan_not(ctype_base::mask  m, const wchar_t* low, const wchar_t* high) const
{
  return find_if(low, high, not1(_Ctype_byname_w_is_mask(m, _M_ctype)));
}
Exemplo n.º 9
0
 // trim from start (in place)
 void ltrim(string &s) {
     s.erase(s.begin(), find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace))));
 }
Exemplo n.º 10
0
void Shop::browse_fullscreen(squadst& customers, int& buyer) const
{
   unsigned page = 0;

   std::vector<ShopOption*> available_options = options_;
   available_options.erase (remove_if (available_options.begin(),
				       available_options.end(),
				       not1 (mem_fun (&ShopOption::display))),
			    available_options.end());


   do
   {
      erase();
      set_color(COLOR_WHITE,COLOR_BLACK,0);
      move(0,0);
      addstr("What will ");
      addstr(customers.squad[buyer]->name);
      addstr(" buy?");
      move(1,0);
      addstr("----PRODUCT NAME-----------------------PRICE------------------------------------");
      int yline = 2;

      //Write wares and prices
      for(unsigned p = page * 19; p < available_options.size () && p < page * 19 + 19; p++)
      {
         if (available_options[p]->is_available())
            set_color(COLOR_WHITE,COLOR_BLACK,0);
         else
            set_color(COLOR_BLACK,COLOR_BLACK,1);
         move(yline,0);
         addch('A' + yline - 2);
         addstr(" - ");
         addstr(available_options[p]->get_description_fullscreen().c_str());
         ++yline;
      }

      set_color(COLOR_WHITE,COLOR_BLACK,0);
      move(22,0);
      addstr("Press a Letter to select an option"); //allow customize "option"? -XML
      move(23,0);
      addpagestr();
      move(24,0);
      addstr(("Enter - " + std::string(customers.squad[buyer]->name) + " " + exit_).c_str());

      refresh();

      int c = getch();
      translategetch(c);

      //PAGE UP
      if ((c == interface_pgup || c == KEY_UP || c == KEY_LEFT) && page > 0)
         page--;
      //PAGE DOWN
      if ((c == interface_pgdn || c == KEY_DOWN || c == KEY_RIGHT)
          && (page + 1) * 19 < available_options.size())
         page++;

      if (c >= 'a' && c <= 's')
      {
         int p = page * 19 + static_cast<int>(c-'a');
         if (p < (int)available_options.size() && available_options[p]->is_available())
            available_options[p]->choose(customers, buyer);
         break;
      }

      if (c == 10)
         break;
   } while (true);
}
Exemplo n.º 11
0
// http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring/217605#217605
// trim from start
static inline string& ltrim(string& s) {
    s.erase(s.begin(),
            find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace))));
    return s;
}
Exemplo n.º 12
0
void Shop::browse_halfscreen(squadst& customers, int& buyer) const
{
   unsigned page = 0;
   const unsigned max_entries_per_page = 20;

   std::vector<ShopOption*> available_options = options_;

   available_options.erase (remove_if (available_options.begin(),
				       available_options.end(),
				       not1 (mem_fun (&ShopOption::display))),
			    available_options.end());

   int partysize = 0;
   for (int p = 0; p < 6; ++p)
   {
      if(customers.squad[p] != NULL)
      {
         partysize++;
      }
   }

   do
   {
      erase();
      set_color(COLOR_WHITE,COLOR_BLACK,0);

      locheader();
      printparty();

      move(8,45);
      addstr("Buyer: ");
      addstr(customers.squad[buyer]->name);

      //Write wares and prices
      int yline = 10;
      int column = 1;
      int taken_letters = 0;
      for (unsigned p = page * (max_entries_per_page - 1);
           p < available_options.size() && p < page * (max_entries_per_page - 1) + max_entries_per_page;
           ++p)
      {
         if (available_options[p]->is_available())
            set_color(COLOR_WHITE,COLOR_BLACK,0);
         else
            set_color(COLOR_BLACK,COLOR_BLACK,1);
         if (column == 1)
            move(yline,1);
         else
            move(yline,40);

         if (available_options[p]->letter_defined_)
            addch(available_options[p]->showletter());
         else
         {
            // Find an available letter to use for this ware.
            bool done = false;
            while (taken_letters < 27 && !done)
            {
               done = true;
               if ('a' + taken_letters == 'b' || // Letters used by the shop UI are disallowed.
                   'a' + taken_letters == 'e' ||
                   ('a' + taken_letters == 's' && allow_selling_) ||
                   ('a' + taken_letters == 'm' && sell_masks_))
               {
                  ++taken_letters;
                  done = false;
                  continue;
               }
               for (unsigned i = 0; i < available_options.size(); ++i)
               {
                  if (available_options[i]->letter_defined_ &&
                      'a' + taken_letters == available_options[i]->letter_)
                  {
                     ++taken_letters;
                     done = false;
                     break;
                  }
               }
            }
            available_options[p]->letter_ = 'a' + taken_letters;
            addch('A' + taken_letters);
            ++taken_letters;
         }

         addstr(" - ");
         addstr(available_options[p]->get_description_halfscreen().c_str());
         if (column == 1)
            column = 2;
         else
         {
            ++yline;
            column = 1;
         }
      }
      if (sell_masks_)
      {
         move(yline,1+(column-1)*39);
         if (ledger.get_funds() >= 15)
            set_color(COLOR_WHITE,COLOR_BLACK,0);
         else
            set_color(COLOR_BLACK,COLOR_BLACK,1);
         addstr("M - Buy a Mask                ($15)");
      }
      if (column == 2)
         ++yline;

      set_color(COLOR_WHITE,COLOR_BLACK,0);
      move(yline,1);
      addstr("E - Look over Equipment");
      ++yline;

      if (allow_selling_)
      {
         move(yline,1);
         if (location[customers.squad[0]->base]->loot.size() > 0)
            set_color(COLOR_WHITE,COLOR_BLACK,0);
         else
            set_color(COLOR_BLACK,COLOR_BLACK,1);
         addstr("S - Sell something");
         ++yline;
      }

      ++yline;

      if (party_status != -1)
         set_color(COLOR_WHITE,COLOR_BLACK,0);
      else
         set_color(COLOR_BLACK,COLOR_BLACK,1);
      move(yline,1);
      addstr("0 - Show the squad's Liberal status");

      if (partysize > 0 && (party_status == -1 || partysize > 1))
         set_color(COLOR_WHITE,COLOR_BLACK,0);
      else
         set_color(COLOR_BLACK,COLOR_BLACK,1);
      move(yline++,40);
      addstr("# - Check the status of a squad Liberal");

      if (partysize >= 2)
         set_color(COLOR_WHITE,COLOR_BLACK,0);
      else
         set_color(COLOR_BLACK,COLOR_BLACK,1);
      move(yline,1);
      addstr("B - Choose a buyer");

      set_color(COLOR_WHITE,COLOR_BLACK,0);
      move(yline,40);
      addstr("Enter - ");
      addstr(exit_.c_str());



      int c = getch();
      translategetch(c);

      for (unsigned i = 0; i < available_options.size(); ++i)
      {
         if (c == available_options[i]->letter_)
         {
            available_options[i]->choose(customers, buyer);
            break;
         }
      }

      if(c == 'e' && customers.squad[0]->base != -1)
         equip(location[customers.squad[0]->base]->loot, -1);
      else if (c == 's' && allow_selling_
               && location[customers.squad[0]->base]->loot.size() > 0)
         sell_loot(customers);
      else if (c == 'm' && sell_masks_ && ledger.get_funds() >= 15)
      {
         maskselect(*customers.squad[buyer]);
      }
      else if (c == 'b')
         choose_buyer(customers, buyer);
      else if (c == '0')
         party_status=-1;
      else if ( c >= '1' && c <= '6')
      {
         if (customers.squad[c-'1'] != NULL)
         {
            if (party_status == c - '1')
               fullstatus(party_status);
            else
               party_status = c - '1';
         }
      }
      else if (c == 10)
         break;

   } while (true);
}
Exemplo n.º 13
0
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// trim from end
string &rtrim(string &s) {
	s.erase(find_if(s.rbegin(), s.rend(), not1(std::ptr_fun<int, int>(isspace))).base(), s.end());
	return s;
}
Exemplo n.º 14
0
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Function name: ltrim()
// Purpose: trim whitespace from the start of the string
// Inputs:
//		- szBuffer: string to trim
// Output: reference to the newly trimmed string
// Notes:
//
string &ltrim(string &szBuffer) {
	szBuffer.erase(szBuffer.begin(), find_if(szBuffer.begin(), szBuffer.end(), not1(ptr_fun<int, int>(isspace))));
    return szBuffer;
}
Exemplo n.º 15
0
void utility::string_utility::LeftTrim(std::string& s)
{
	s.erase(s.begin(), find_if(s.begin(), s.end(), not1(std::ptr_fun<int, int>(std::isspace))));
}
Exemplo n.º 16
0
 // trim from end (in place)
 void rtrim(string &s) {
     s.erase(find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(), s.end());
 }
Exemplo n.º 17
0
void utility::string_utility::RightTrim(std::string& s)
{
	s.erase(find_if(s.rbegin(), s.rend(), not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
}
wstring StringUtilities::LeftTrim(wstring str)
{
    str.erase(str.begin(), find_if(str.begin(), str.end(), not1(std::ptr_fun<int, int>(std::isspace))));
    return str;
}