//------------------------------------------------------------------------------
// <copyright file="LqrSMACGetCmdInfo.cpp" company="LiquidRobotics">
//
// Copyright (c) 2014 Liquid Robotics, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// This software is associated with a release.  Liquid Robotics makes no
// representation that the design, interfaces or component distribution will
// remain unchanged in future releases.
//
// </copyright>
//
// <summary>
//   Implementation of some methods that return info about running processes
// </summary>
//
// <owner>Mike Cookson</owner>
//------------------------------------------------------------------------------

#include "LqrSMACGetCmdInfo.h"
#include <fstream>
#include <sstream>
#include <stdexcept>

using namespace std;

namespace LiquidRobotics {
namespace SMACFramework {
namespace GetCmdInfo {

static GCIString GetCmdName(const string& dirComponent, bool throwOnError)
{
	GCIString result = "unknown";
	stringstream errstm;

	try
	{
		stringstream namestm;
		namestm << "/proc/" << dirComponent << "/comm";

		const string fname = namestm.str();
		string line;
		ifstream myfile(fname.c_str());

		if (myfile.is_open())
		{
			if (myfile.good())
			{
				getline(myfile, line);

				if (line.length())
					result = line;
				else
					errstm << fname << " is empty";
			}
			else
				errstm << "I/O error with " << fname;

			myfile.close();
		}
		else
			errstm << "Failed to open " << fname;

	}
	catch(...)
	{
		if (throwOnError)
			throw;
	}

	if (throwOnError)
	{
		const string err = errstm.str();

		if (err.length())
			throw runtime_error(err);
	}

	return result;
}

SHARED_LIB_PUBLIC GCIString GetCmdName(bool throwOnError)
{
	GCIString result = GetCmdName("self", throwOnError);
	return result;
}

SHARED_LIB_PUBLIC GCIString GetCmdName(const pid_t& pid, bool throwOnError)
{
	stringstream stm;
	stm << pid;

	const string pidstr = stm.str();
	GCIString result = GetCmdName(pidstr, throwOnError);
	return result;
}


}
}
}
