Jump to content

Separate string


Guest filo

Recommended Posts

Hi,

I need help with separate string from config, something like this (example):

config:

areas = "5442,1024,3054,246"

player.cpp:

std::string areas = sWorld.getConfig(CONFIG_STRING_CUSTOM_AREAS);
if (pPlayer->GetAreaId() == areas)
{
...some code here...
}

but i dont know, how can I separate areas string for IF clause.

Thanks for help with this.

Link to comment
Share on other sites

There is a similar thing with vmaps with maps to ignore. In VMapManager, there is this code to split it:

    void VMapManager2::PreventMapsFromBeingUsed(const char* pMapIdString)
   {
       iIgnoreMapIds.clear();
       if (pMapIdString != NULL)
       {
           std::string map_str;
           std::stringstream map_ss;
           map_ss.str(std::string(pMapIdString));
           while (std::getline(map_ss, map_str, ','))
           {
               std::stringstream ss2(map_str);
               int map_num = -1;
               ss2 >> map_num;
               if (map_num >= 0)
               {
                   DETAIL_LOG("Ignoring Map %i for VMaps", map_num);
                   iIgnoreMapIds[map_num] = true;
                   // unload map in case it is loaded
                   unloadMap(map_num);
               }
           }
       }
   }

Link to comment
Share on other sites

If you look up iIgnoreMapIds, you will find this code:

    VMAPLoadResult VMapManager2::loadMap(const char* pBasePath, unsigned int pMapId, int x, int y)
   {
       VMAPLoadResult result = VMAP_LOAD_RESULT_IGNORED;
       if (isMapLoadingEnabled() && !iIgnoreMapIds.count(pMapId))
       {
           if (_loadMap(pMapId, pBasePath, x, y))
               result = VMAP_LOAD_RESULT_OK;
           else
               result = VMAP_LOAD_RESULT_ERROR;
       }
       return result;
   }

Link to comment
Share on other sites

  • 1 month later...

In case you or anyone else is still wondering about this, I have made another version with strchr to split the string. Here it is:

   m_AllowedMaps.clear();
   const char* allowedMaps = sConfig.GetStringDefault("Allowed.Maps", "").c_str();
   // if we actually have a string...
   if(strcmp(allowedMaps, "") != 0)
   {
       // next is a pointer to pos of next comma; prev is a pointer to last comma
       const char* next;
       // find first position of comma
       next = strchr(allowedMaps, ',');
       // no comma, insert only one entry
       if(!next)
           m_AllowedMaps.insert(atoi(allowedMaps));
       else
       {
           // start at the beginning
           const char* prev = allowedMaps;
           // buffer for the argument
           char* buffer;
           // length of the fragment
           size_t length;
           // while we still have more
           while(next != NULL)
           {
               // how big the string fragment will be; factor in null terminator (+ 1)
               length = next - prev + 1;
               // allocate enough room in the buffer to hold the string fragment
               buffer = new char[length];
               // copy the string fragment over
               strncpy(buffer, prev, length);
               // and don't forget the terminator
               buffer[length - 1] = '\\0';
               // insert the new entry into our set of banned maps
               m_AllowedMaps.insert(atoi(buffer));
               // set pos of last comma, + 1 char (i.e. the last comma) and get new pointer to next comma
               prev = next + sizeof(char);
               next = strchr(next + 1, ',');
               // deallocate buffer to prevent memory leaks
               delete buffer;
           }
           // once more for the last argument
           // we already have this pointer set so use it
           m_AllowedMaps.insert(atoi(prev));
       }
   }

In this case, it reads a string from the config and builds a set of map IDs (m_AllowedMaps is a std::set<uint32>) It has no problem with spacing, "0,530" and "0, 530" both work fine. I'm sure it could be easily made into a function.

Link to comment
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy Terms of Use