Пример #1
0
bool TestStreams()
{
    const char * rgszTestFile[3] = {
        "test1-input.ini",
        "test1-output.ini",
        "test1-expected.ini"
    };

    Test oTest("TestStreams");

    CSimpleIniA ini;
    ini.SetUnicode(true);
    ini.SetMultiKey(true);
    ini.SetMultiLine(true);

    // load the file
    try {
        std::ifstream infile;
        infile.open(rgszTestFile[0], std::ifstream::in | std::ifstream::binary);
        if (ini.Load(infile) < 0) throw false;
        infile.close();
    }
    catch (...) {
        return oTest.Failure("Failed to load file");
    }

    // standard contents test
    //if (!StandardContentsTest(ini, oTest)) {
    //    return false;
    //}

    // save the file
    try {
        std::ofstream outfile;
        outfile.open(rgszTestFile[1], std::ofstream::out | std::ofstream::binary);
        if (ini.Save(outfile, true) < 0) throw false;
        outfile.close();
    }
    catch (...) {
        return oTest.Failure("Failed to save file");
    }

    // file comparison test
    if (!FileComparisonTest(rgszTestFile[1], rgszTestFile[2])) {
        return oTest.Failure("Failed file comparison");
    }
    if (!FileLoadTest(rgszTestFile[1], rgszTestFile[2])) {
        return oTest.Failure("Failed file load comparison");
    }

    return oTest.Success();
}
Пример #2
0
int main(int argc, const char *argv[])
{
    CSimpleIniA ini;
    ini.SetUnicode();
    ini.LoadFile("myfile.ini");
    const char * pVal = ini.GetValue("section", "key", "default");
    ini.SetValue("section", "key", "newvalue");

    std::string strData;
    ini.Save(strData);    
    printf("strData.c_str(): %s\n", strData.c_str());

    return 0;
}
Пример #3
0
// Main
int main(int argc, char **argv)
{
	// Check the number of command line arguments
	if(argc < 2)
	{
		cout << "Try " << argv[0] << " -h or --help" << endl;
		return 1;
	}

	try
	{
		TCLAP::CmdLine cmd("Push notifications to your phone easily.", ' ', "0.3");

		// Values
		TCLAP::ValueArg<int> idArg("i","id","ID of the device.",false,0,"number");
		cmd.add(idArg);

		// Switches
		TCLAP::SwitchArg tokenSwitch("t", "token", "Request your token.", cmd, false);
		TCLAP::SwitchArg listSwitch("l", "list", "List all your devices.", cmd, false);
		TCLAP::SwitchArg pipeSwitch("p", "pipe", "Input via pipe.", cmd, false);
		TCLAP::SwitchArg verifySwitch("v","verify","Checks if token is still valid.", cmd, false);


		// add unlabeled argument
		TCLAP::UnlabeledValueArg<string> noLabelMessage("message", "The notification you want to send.", false, "message", "message");
		cmd.add(noLabelMessage);


    	// Parse the argv array.
    	cmd.parse(argc, argv);


		// Variables
		string message;
		int id;
		CSimpleIniA iniReader;
		iniReader.SetUnicode();


		// Request token
		if(tokenSwitch.getValue())
		{
			string username, password, token;

			// Read username
			cout << "Username: "******"Password: "******"pusher", "username", username.c_str());
			iniReader.SetValue("pusher", "appToken", token.c_str());

			// Check if file is writable
			if(iniReader.SaveFile(CONFIG_FILE) < 0)
			{
				cout << "Try running pusher as root or save the following in "
					<< CONFIG_FILE << "\n" << endl;

				// Data
				string strData;
				iniReader.Save(strData);
				cout << strData << endl;

				return 1;
			}


			cout << "Success!" << endl;
			return 0;
		}



		// Check if reading of config is possible
		if(iniReader.LoadFile(CONFIG_FILE) < 0)
		{
			throw PusherError("You need to login first.");
		}

		string username = iniReader.GetValue("pusher", "username", "");
		string appToken = iniReader.GetValue("pusher", "appToken", "");

		if(username.empty() || appToken.empty())
		{
			throw PusherError("You need to login first.");
		}



		// Loading values
		PushHandler pusherInstance(username, appToken);



		// Verify token
		if(verifySwitch.getValue())
		{
			if(pusherInstance.verifyToken())
			{
				cout << "appToken is valid" << endl;
				return 0;
			}
			else
			{
				cout << "appToken is invalid" << endl;
				return 1;
			}
		}



		// List devices
		if(listSwitch.getValue())
		{
			vector<PushHandler::Device> devices;
			devices = pusherInstance.getDevices();

			unsigned int titleLength = 5;
			unsigned int modelLength = 5;
			unsigned int idLength = 2;

			for(unsigned int i = 0; i < devices.size(); i++)
			{
				if(devices[i].title.length() > titleLength) { titleLength = devices[i].title.length(); }
				if(devices[i].model.length() > modelLength) { modelLength = devices[i].model.length(); }
				if(devices[i].id.length() > idLength) { idLength = devices[i].id.length(); }
			}

			cout
				<< "ID\033[" << (idLength - 2 + 2) << "C"
				<< "Title\033[" << (titleLength - 5 + 2) << "C"
				<< "Model" << endl;

			for(unsigned int x = 0; x < (titleLength + modelLength + idLength + 4); x++) { cout << "-"; }
			cout << endl;

			for(unsigned int i = 0; i < devices.size(); i++)
			{
				cout
					<< devices[i].id << "\033[" << (idLength - devices[i].id.length() + 2) << "C"
					<< devices[i].title << "\033[" << (titleLength - devices[i].title.length() + 2) << "C"
					<< devices[i].model << endl;
			}

			return 0;
		}



		// Device id
		if(idArg.getValue() != 0)
		{
			id = idArg.getValue();
		}
		else
		{
			return 1;
		}



		// Load message
		if(pipeSwitch.getValue())
		{
			string pipeBuffer;

			while(getline(cin, pipeBuffer))
			{
				message += "\n";
				message += pipeBuffer;
			}

			message.erase(0,1);
		}
		else
		{
			message = noLabelMessage.getValue();
		}

		// Send the message
		stringstream stringID;
		stringID << id;

		pusherInstance.sendToDevice(stringID.str(), message);
	}