added string splitter to digger, to handle comma delimited arguments

develop
mizipzor 2010-02-18 18:53:48 +01:00
parent 2d6a5e7428
commit b81d6d6744
1 changed files with 46 additions and 1 deletions

@ -23,6 +23,7 @@ using namespace std;
#include <DFTileTypes.h> #include <DFTileTypes.h>
#include <DFHackAPI.h> #include <DFHackAPI.h>
// counts the occurances of a certain element in a vector
int vec_count(vector<uint16_t>& vec, uint16_t t) int vec_count(vector<uint16_t>& vec, uint16_t t)
{ {
int count = 0; int count = 0;
@ -33,7 +34,27 @@ int vec_count(vector<uint16_t>& vec, uint16_t t)
} }
return count; return count;
} }
// splits a string on a certain char
//
// src is the string to split
// delim is the delimiter to split the string around
// tokens is filled with every occurance between delims
void string_split(vector<string>& tokens, const std::string& src, const std::string& delim)
{
std::string::size_type start = 0;
std::string::size_type end;
while (true)
{
end = src.find(delim, start);
tokens.push_back(src.substr(start, end - start));
if (end == std::string::npos) // last token handled
break;
start = end + delim.size(); // skip next delim
}
}
// calculates the manhattan distance between two coords
int manhattan_distance(int x, int y, int z, int xx, int yy, int zz) int manhattan_distance(int x, int y, int z, int xx, int yy, int zz)
{ {
return abs(x-xx)+abs(y-yy)+abs(z-zz); return abs(x-xx)+abs(y-yy)+abs(z-zz);
@ -179,6 +200,8 @@ int dig(DFHack::API& DF,
void test() void test()
{ {
//////////////////////////
// DigTarget
{ {
DigTarget dt( DigTarget dt(
20, 35, 16, 20, 35, 16,
@ -196,7 +219,6 @@ void test()
assert(dt.z == 16); assert(dt.z == 16);
assert(dt.source_distance == 35); assert(dt.source_distance == 35);
} }
{ {
DigTarget dt( DigTarget dt(
2, 4, 16, 2, 4, 16,
@ -215,6 +237,29 @@ void test()
assert(dt.z == 16); assert(dt.z == 16);
assert(dt.source_distance == 91); assert(dt.source_distance == 91);
} }
//////////////////////////
// string splitter
{
vector<string> tokens;
string src = "10,9,11";
string delim = ",";
string_split(tokens, src, delim);
assert(tokens.size() == 3);
assert(tokens[0] == "10");
assert(tokens[1] == "9");
assert(tokens[2] == "11");
}
{
vector<string> tokens;
string src = "10";
string delim = ",";
string_split(tokens, src, delim);
assert(tokens.size() == 1);
assert(tokens[0] == "10");
}
} }
int main (int argc, const char* argv[]) int main (int argc, const char* argv[])