Code: Select all
string := "1,10|2,20|4,40|3,30"
DllCall("dll\sortSelection", "Str", string, "Cdecl")
msgbox % string ; string now equals 4|3|2|1
The real function is actually sorting a list of items via each items associated fields/values. In this test example each item only has one field to sort by (value) so the passed string will have this form.
Code: Select all
item1ID,Item1Value|item2ID,item2Value ....
Each item is pipe delimited. Each field within an item is comma delimited. The function will sort the list and update the passed string with the sorted items IDs.
How should the below function be modified so as to allow it to be compiled as a DLL and called from AHK? (I would like to update the passed string)
Many thanks.
The c++ function:
Code: Select all
#include "StdAfx.h"
//#include <windows.h>
//#include <ostream>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;
struct item
{
int id;
int value;
item(string const& inputString) // inputString = comma delimited "id,value"
{
istringstream input(inputString);
string number;
for (int i=0; getline(input, number, ','); i++)
{
istringstream iss(number);
if (i == 0)
iss >> id;
else
iss >> value;
}
}
static bool compare(const item& lhs, const item& rhs) // might be able to use strcmp() to prevent conversions
{
if (lhs.value > rhs.value)
return 1;
return 0;
}
};
string sortSelection(string &inputString)
{
vector<item> items;
istringstream input(inputString);
string param;
// convert the pipe delimited string into the individual comma delimited param list and populate the vector.
while (getline(input, param, '|'))
items.push_back(item(param));
sort(items.begin(), items.end(), item::compare);
ostringstream outputString; //output string stream
// loop through the sorted vector and extract the ID values and convert these back into a pipe delimited string
for (int i=0; i < items.size(); i++)
outputString << (i != 0 ? "|" : "") << items[i].id; //use the string stream just like cout
return inputString = outputString.str(); //the str() function of the stream
}
int main()
{
//string param --> item1ID,Item1Value|item2ID,item2Value.....
// will return a string of IDs ranked by value (higher first). The byRef passed param will equal the return value.
// Should return 4|3|2|1
string myString = "1,10|2,20|4,40|3,30";
cout << sortSelection(myString) << endl << myString << endl;
system("pause");
return 0;
}