예제 #1
0
int main(int argc, const char *argv[])
{
    Node first(10, 20);
    Node second(30, 40);
    Node third(100, 50);
    Node forth(100, 33);
    first.setNext(&second);
    second.setNext(&third);
    std::cout << first.getTail()->getValue() << std::endl;
    std::cout << second.getTail()->getValue() << std::endl;
    std::cout << third.getTail()->getValue() << std::endl;

    Hash h;
    h.put(10,20);
    h.put(30,40);
    h.put(10,30);
    h.put(30,50);
    std::cout << h.get(10,20) << std::endl;
    std::cout << h.get(11,20) << std::endl;
    std::cout << h.get(10,30) << std::endl;
    std::cout << h.get(31,100) << std::endl;
    std::cout << h.get(30,40) << std::endl;
    std::cout << h.get(30,50) << std::endl;

    return 0;
}
예제 #2
0
int main(){
 Hash<int>* h = new Hash<int>();

 h->insert("one", 1);

 std::cout << "inserted (\"one\", 1)\nresult: " << h->get("one") << std::endl;

 h->insert("one", 2);

 std::cout << "replaced 1 with 2\n" << h->get("one") << std::endl;

 h->insert("noe", 3);
 
 std::cout << "inserted (\"noe\", 3) (should create the same hash as \"one\", but with value of 3)\nresult: " << h->get("noe") << std::endl;

 h->insert("ZZZZZZzZzZzZzzzZUHcuhu", 239);
 h->insert("ZZZZZZzZzZZzzzzZUHcuhu", 13);

 std::cout << "inserted (\"ZZZZZZzZzZzZzzzZUHcuhu\", 239)\n" << h->get("ZZZZZZzZzZzZzzzZUHcuhu") << std::endl;
 
 h->remove("noe");

 std::cout << "removed (\"noe\"\n" << std::endl;
 h->print();

 return 0;
}
int main( int argc, char *argv[] ){

  for( unsigned char index=10; index > 0; index-- ){
    assert( hsh.set( int_to_str(index), index ) == index );
    assert( hsh.get(int_to_str(index)) == index );
  }

  for( unsigned char index=10; index; index-- ){
    assert( hsh.get( int_to_str(index) ) == index );
  }

  return 0x0000;  // EXIT_SUCCESS;
}
예제 #4
0
void testRemove() {
	Hash<int, int> h;

	for (vector<pair<int, int> >::const_iterator i = ints.begin(); i != ints.begin() + ints.size() / 2; ++i) {
		h.put(i->first, i->second);
	}

	for (vector<pair<int, int> >::const_iterator i = ints.begin(); i != ints.begin() + ints.size() / 2; ++i) {
		h.remove(i->first);
	}

	if (!h.isEmpty()) {
		cout << "testRemove failed\n";
	}

	for (vector<pair<int, int> >::const_iterator i = ints.begin(); i != ints.end(); ++i) {
		h.put(i->first, i->second);
	}

	for (map<int, int>::const_iterator i = uniqueInts.begin(); i != uniqueInts.end(); ++i) {
		if (h.get(i->first)->value != i->second) {
			cout << "Fail on remove test with number " << i->first << ". Expected: " << i->second << " Got: " << h.get(i->first)->value << endl;
		}
	}
}
예제 #5
0
void testAdd() {
	Hash<int, int> h;

	h.put(42, 11);

	if (h.get(42) == h.end()) {
		cout << "testAdd failed\n";
	}
}
예제 #6
0
int bfs(int x){
	//x is goal money
	queue<int> Q;
	Hash m;//money -> step
    m.put(0,0);
	Q.push(0);
	while(!Q.empty()){
		int h = Q.front();
		Q.pop();
		if(h==x) return m.get(x);
		for(int i=0;i<6;i++)
			for(int j=-1;j<=1;j+=2){
				int y = h+j*d[i];
				if(m.get(y)!=-1) continue;
				m.put(y,m.get(h)+1);
				Q.push(y);
			}
	}
	return -1;
}
예제 #7
0
void testManyStrings() {
	Hash<string, string> h;

	for (vector<pair<string, string> >::const_iterator i = strings.begin(); i != strings.end(); ++i) {
		h.put(i->first, i->second);
	}

	for (vector<pair<string, string> >::const_iterator i = strings.begin(); i != strings.end(); ++i) {
		if (h.get(i->first)->value != i->second) {
			cout << "Fail on string test with string " << i->first << ". Expected: " << i->second << " Got: " << h.get(i->first)->value << endl;
		}
	}
}
예제 #8
0
void testManyInts() {
	Hash<int, int> h;

	for (vector<pair<int, int> >::const_iterator i = ints.begin(); i != ints.end(); ++i) {
		h.put(i->first, i->second);
	}

	for (map<int, int>::const_iterator i = uniqueInts.begin(); i != uniqueInts.end(); ++i) {
		if (h.get(i->first)->value != i->second) {
			cout << "Fail on int test with number " << i->first << ". Expected: " << i->second << " Got: " << h.get(i->first)->value << endl;
		}
	}
}
예제 #9
0
void testReplace() {
	Hash<int, int> h;

	h.put(42, 11);
	h.put(32, 10);
	h.put(90, 3);
	h.put(42, 9);
	h.put(50, 3);

	if (h.get(42)->value != 9) {
		cout << "testReplace failed\n";
	}
}
void App::downscaleTextures(MeshBase* mesh)
{
    FW_ASSERT(mesh);
    Hash<const Image*, Texture> hash;
    for (int submeshIdx = 0; submeshIdx < mesh->numSubmeshes(); submeshIdx++)
    for (int textureIdx = 0; textureIdx < MeshBase::TextureType_Max; textureIdx++)
    {
        Texture& tex = mesh->material(submeshIdx).textures[textureIdx];
        if (tex.exists())
        {
            const Image* orig = tex.getImage();
            if (!hash.contains(orig))
            {
                Image* scaled = orig->downscale2x();
                hash.add(orig, (scaled) ? Texture(scaled, tex.getID()) : tex);
            }
            tex = hash.get(orig);
        }
    }
}
static ngx_int_t ngx_restfull_redis_handler(ngx_http_request_t *r)
{
  
  //TODO: deal with allocation problem - return 500
  ngx_chain_t   out;
  out.buf = create_response_buffer(r);;
  out.next = NULL;

  dd("command => %s", command(r));

  char* redis_command = command(r);
  define_content_type(r, "application/json; charset=utf-8");

  redisContext *c = redisConnect("127.0.0.1", 6379);

  if (c->err) {
    dd("Error: %s\n", c->errstr);
    write_to_buffer(out.buf, (u_char*) "Can't connect to redis", strlen("Can't connect to redis"));
  }

  Hash* params = get_params(r);
  
  redisReply *reply;

  if(!strcmp(redis_command, "get"))
  {
    reply = redisCommand(c,"GET %s", params->get(params, "key"));

    dd("Redis reply status: %d", reply->type);
    if(key_not_found(reply))
    {
      not_found(r, out.buf, (u_char*)"not found");
      return ngx_http_output_filter(r, &out);
    }
    dd("reply: %s", reply->str);

    u_char * result = copy(r->pool, reply->str, reply->len);
    write_to_buffer(out.buf, result, reply->len);
    write_header(r, NGX_HTTP_OK, reply->len);

  } else if (!strcmp(redis_command, "set"))
  {
    reply = redisCommand(c,"SET %s %s", params->get(params, "key"), params->get(params, "value"));

    dd("Redis reply status: %d", reply->type);
    if(key_not_found(reply))
    {
      not_found(r, out.buf, (u_char*)"not found");
      return ngx_http_output_filter(r, &out);
    }

    dd("Reply set %s -- %d", reply->str, reply->len);
    write_to_buffer(out.buf, (u_char*) reply->str, reply->len);
    write_header(r, NGX_HTTP_OK, reply->len);
  } else if(!strcmp(redis_command, "incr"))
  {
    reply = redisCommand(c,"INCR %s", params->get(params, "key"));

    dd("Redis reply status: %d", reply->type);

    int len = number_of_digits(reply->integer);

    dd("Reply INCR -- %d - len %d", (int)reply->integer, len);
    u_char* result = ngx_pcalloc(r->pool, sizeof(char)*len);
    sprintf((char*)result,"%d",(int)reply->integer);
    write_to_buffer(out.buf, result, len);
    write_header(r, NGX_HTTP_OK, len);
  }

  freeReplyObject(reply);
  redisFree(c);
  ngx_free(params);
    
  return ngx_http_output_filter(r, &out);
}
예제 #12
0
int main(int argc, char* argv[])
{
   LogWrapperType logwrapper(new btg::core::logger::logWrapper);

   commandLineArgumentHandler* cla = new commandLineArgumentHandler(GPD->sDEFAULT_DAEMON_CONFIG());
   cla->setup();
   if (!cla->parse(argc, argv))
      {
         delete cla;
         cla = 0;

         return BTG_ERROR_EXIT;
      }

   bool verboseFlag = cla->beVerbose();

   std::string configFile = GPD->sDEFAULT_DAEMON_CONFIG();
   if (cla->configFileSet())
      {
         configFile = cla->configFile();
      }
#if BTG_AUTH_DEBUG
   BTG_NOTICE(logwrapper, "Config: '" << configFile << "'");
#endif //BTG_AUTH_DEBUG

   std::string errorString;
   if (!btg::core::os::fileOperation::check(configFile, errorString, false))
      {
         BTG_FATAL_ERROR(logwrapper, "btgpasswd", "Unable to open config file '" << configFile << "': " << errorString);

         delete cla;
         cla = 0;

         return BTG_ERROR_EXIT;
      }

   btg::daemon::daemonConfiguration* config  = new btg::daemon::daemonConfiguration(logwrapper,
                                                                                    configFile);

   if (!config->read())
      {
         BTG_FATAL_ERROR(logwrapper, "btgpasswd", "Could not open configuration file '" << configFile << "'");

         delete config;
         config = 0;
         delete cla;
         cla = 0;

         return BTG_ERROR_EXIT;
      }

#if BTG_AUTH_DEBUG
   BTG_NOTICE(logwrapper, "Config read from '" << configFile << "'.");
#endif // BTG_AUTH_DEBUG

   // Open the passwd file, filename from the config file.

   std::string auth_file = config->getAuthFile();

   bool newfile = cla->createNewFile();

   if (newfile)
      {
         if (cla->getOperation() != commandLineArgumentHandler::OP_ADD)
            {
               // Only allow creating new file when an user is being added.
               newfile = false;
            }
      }

   // resolve relative paths etc.
   if (!btg::core::os::fileOperation::resolvePath(auth_file))
      {
         BTG_NOTICE(logwrapper, "Unable to resolve path '" << auth_file << "'");
      }

   if (!newfile)
      {
         if (!btg::core::os::fileOperation::check(auth_file, errorString, false))
            {
               BTG_FATAL_ERROR(logwrapper, "btgpasswd", "Could not open passwd file '" << auth_file << "': " << errorString);

               delete config;
               config = 0;
               delete cla;
               cla = 0;

               return BTG_ERROR_EXIT;
            }
      }

   passwordAuth* auth = new passwordAuth(logwrapper, auth_file, newfile);

   if (!auth->initialized())
      {
         BTG_FATAL_ERROR(logwrapper, "btgpasswd", "Unable to load passwd file '" << auth_file << "'.");

         delete auth;
         auth = 0;
         delete config;
         config = 0;
         delete cla;
         cla = 0;

         return BTG_ERROR_EXIT;
      }

   commandLineArgumentHandler::OPERATION operation = cla->getOperation();

   switch (operation)
      {
      case commandLineArgumentHandler::OP_ADD:
         {
            VERBOSE_LOG(logwrapper, verboseFlag, "Adding user.");

            std::string user;
            std::string password;
            std::string password_confirm;
            std::string temp_dir;
            std::string work_dir;
            std::string seed_dir;
            std::string dest_dir;
            bool        control_flag = false;
            std::string callback;

            if (!cla->getUsername(user))
               {
                  BTG_FATAL_ERROR(logwrapper, "btgpasswd", "Missing user name.");
                  break;
               }

            if (!cla->getTempDir(temp_dir))
               {
                  BTG_FATAL_ERROR(logwrapper, "btgpasswd", "Missing temporary directory.");
                  break;
               }

            if (!cla->getWorkDir(work_dir))
               {
                  BTG_FATAL_ERROR(logwrapper, "btgpasswd", "Missing working directory.");
                  break;
               }

            if (!cla->getSeedDir(seed_dir))
               {
                  BTG_FATAL_ERROR(logwrapper, "btgpasswd", "Missing seed directory.");
                  break;
               }

            if (!cla->getDestDir(dest_dir))
               {
                  BTG_FATAL_ERROR(logwrapper, "btgpasswd", "Missing destination directory.");
                  break;
               }

            // Make sure that the directories are unique.
            if (!uniqueDirectories(logwrapper,
                                   temp_dir, work_dir,
                                   seed_dir, dest_dir))
               {
                  BTG_FATAL_ERROR(logwrapper,
                                  "btgpasswd", "Provided directories are not unique. " << 
                                  "Make sure temporary/working/seed/destination directory is unique.");
                  break;
               }

            if (!cla->getControlFlag(control_flag))
               {
                  control_flag = false;
               }

            if (!cla->getCallback(callback))
               {
                  callback = Auth::callbackDisabled;
               }

            std::cout << "Enter password:"******"btgpasswd", "Reading password failed.");
                  break;
               }

            std::cout << "Confirm password:"******"btgpasswd", "Reading password failed.");
                  break;
               }

            // Make sure passwords are equal
            if (password != password_confirm)
               {
                  BTG_FATAL_ERROR(logwrapper, "btgpasswd", "Password missmatch.");
                  break;
               }


            Hash h;
            h.generate(password);
            std::string hashed;
            h.get(hashed);

            VERBOSE_LOG(logwrapper,
                        verboseFlag, "Adding user '" << user << "' with:" << GPD->sNEWLINE() <<
                        "hash '" << hashed << "'" << GPD->sNEWLINE() <<
                        "temp directory '" << temp_dir << "'" << GPD->sNEWLINE() <<
                        "work directory '" << work_dir << "'" << GPD->sNEWLINE() <<
                        "seed directory '" << seed_dir << "'" << GPD->sNEWLINE() <<
                        "destination directory '" << dest_dir << "'" << GPD->sNEWLINE() << 
                        "control flag '" << control_flag << "'" << GPD->sNEWLINE() <<
                        "callback '" << callback << "'" << GPD->sNEWLINE()
                        );

            // Create the directories used.

            if ((!createDirectory(logwrapper, verboseFlag, "temp", temp_dir)) ||
                (!createDirectory(logwrapper, verboseFlag, "work", work_dir)) ||
                (!createDirectory(logwrapper, verboseFlag, "seed", seed_dir)) ||
                (!createDirectory(logwrapper, verboseFlag, "dest", dest_dir))
                )
               {
                  // Abort, as some directories failed.
                  BTG_FATAL_ERROR(logwrapper, "btgpasswd", "Failed to create user directorys.");
                  break;
               }

            if (!auth->addUser(user, hashed, temp_dir, work_dir, seed_dir, dest_dir, control_flag, callback))
               {
                  BTG_FATAL_ERROR(logwrapper, "btgpasswd", "Unable to add user.");
                  break;
               }

            if (!auth->write())
               {
                  BTG_FATAL_ERROR(logwrapper, "btgpasswd", "Unable to save password file.");
                  break;
               }

            std::cout << "User '" << user << "' added." << std::endl;
            break;
         }
      case commandLineArgumentHandler::OP_MOD:
         {
            VERBOSE_LOG(logwrapper, verboseFlag, "Modifying user.");

            std::string user;
            std::string password;
            std::string password_confirm;
            std::string temp_dir;
            std::string work_dir;
            std::string seed_dir;
            std::string dest_dir;

            bool control_flag = false;

            std::string callback;

            if (!cla->getUsername(user))
               {
                  BTG_FATAL_ERROR(logwrapper, "btgpasswd", "Missing user name.");
                  break;
               }

            if (!auth->getControlFlag(user, control_flag))
               {
                  BTG_FATAL_ERROR(logwrapper, "btgpasswd", "Missing control flag.");
                  break;
               }

            bool any_argument = false;

            if (cla->readPassword())
               {
                  any_argument = true;
               }

            if (cla->getTempDir(temp_dir))
               {
                  any_argument = true;
               }

            if (cla->getWorkDir(work_dir))
               {
                  any_argument = true;
               }

            if (cla->getSeedDir(seed_dir))
               {
                  any_argument = true;
               }
            if (cla->getDestDir(dest_dir))
               {
                  any_argument = true;
               }

            if (cla->getControlFlag(control_flag))
               {
                  any_argument = true;
               }

            if (cla->getCallback(callback))
               {
                  any_argument = true;
               }

            if (!any_argument)
               {
                  BTG_FATAL_ERROR(logwrapper, "btgpasswd", "Nothing changed.");
                  break;
               }

            BTG_NOTICE(logwrapper,
                       "Mod user '" << user << "', using directories:" << GPD->sNEWLINE() <<
                       "temp directory '" << temp_dir << "'" << GPD->sNEWLINE() <<
                       "work directory '" << work_dir << "'" << GPD->sNEWLINE() <<
                       "seed directory '" << seed_dir << "'" << GPD->sNEWLINE() <<
                       "destination directory '" << dest_dir << "'" << GPD->sNEWLINE()
                       );
            std::string hashedPassword;
            if (cla->readPassword())
               {
                  std::cout << "Enter password:"******"btgpasswd", "Reading password failed.");
                        break;
                     }

                  std::cout << "Confirm password:"******"btgpasswd", "Reading password failed.");
                        break;
                     }

                  // Make sure passwords are equal
                  if (password != password_confirm)
                     {
                        BTG_FATAL_ERROR(logwrapper, "btgpasswd", "Password missmatch.");
                        break;
                     }

                  Hash h;
                  h.generate(password);
                  h.get(hashedPassword);
               }

            if (verboseFlag)
               {
                  std::string output;

                  output += "Modyfying user '" + user + "', setting:" + GPD->sNEWLINE();

                  if (password.size() > 0)
                     {
                        output += "hash '" + hashedPassword + "'" + GPD->sNEWLINE();
                     }

                  if (temp_dir.size() > 0)
                     {
                        output += "temp directory '" + temp_dir + "'" + GPD->sNEWLINE();
                     }

                  if (work_dir.size() > 0)
                     {
                        output += "work directory '" + work_dir + "'" + GPD->sNEWLINE();
                     }

                  if (seed_dir.size() > 0)
                     {
                        output += "seed directory '" + seed_dir + "'" + GPD->sNEWLINE();
                     }

                  if (dest_dir.size() > 0)
                     {
                        output += "destination directory '" + dest_dir + "'" + GPD->sNEWLINE();
                     }

                  if (control_flag)
                     {
                        output += "control flag '1'" + GPD->sNEWLINE();
                     }
                  else
                     {
                        output += "control flag '0'" + GPD->sNEWLINE();
                     }

                  if (callback.size() > 0)
                     {
                        output += "callback '" + callback + "'" + GPD->sNEWLINE();
                     }

                  VERBOSE_LOG(logwrapper, verboseFlag, output);
               } // verbose

            if (!auth->modUser(user, hashedPassword, temp_dir, work_dir, seed_dir, dest_dir, control_flag, callback))
               {
                  BTG_FATAL_ERROR(logwrapper, "btgpasswd", "Unable to modify user.");
                  break;
               }

            if (!auth->write())
               {
                  BTG_FATAL_ERROR(logwrapper, "btgpasswd", "Unable to save password file.");
                  break;
               }

            std::cout << "User '" << user << "' modified." << std::endl;
            break;
         }
      case commandLineArgumentHandler::OP_DEL:
         {
            VERBOSE_LOG(logwrapper, verboseFlag, "Deleting user.");

            std::string user;

            if (!cla->getUsername(user))
               {
                  BTG_FATAL_ERROR(logwrapper, "btgpasswd", "Missing user name.");
                  break;
               }

            if (!auth->delUser(user))
               {
                  BTG_FATAL_ERROR(logwrapper, "btgpasswd", "Unable to delete user.");
                  break;
               }

            if (!auth->write())
               {
                  BTG_FATAL_ERROR(logwrapper, "btgpasswd", "Unable to save password file.");
                  break;
               }

            std::cout << "Deleted user '" << user << "'." << std::endl;
            break;
         }
      case commandLineArgumentHandler::OP_LST:
         {
            std::cout << "Listing users." << std::endl;
            std::cout << auth->toString() << std::endl;
            break;
         }
      default:
         {
            BTG_FATAL_ERROR(logwrapper, "btgpasswd", "Unknown operation.");
         }
      }

   delete auth;
   auth = 0;

   delete config;
   config = 0;

   delete cla;
   cla = 0;

   return 0;
}
		gnutls_digest_algorithm_t GetHash() const { return hash.get(); }