// -*- c++ -*-
//------------------------------------------------------------------------------
// $Id: CommonUtils.h,v 1.5 2003/09/06 03:35:52 vlg Exp $
//------------------------------------------------------------------------------
//                             CommonUtils.h
//------------------------------------------------------------------------------
//  Copyright (C) 1997-2003  Vladislav Grinchenko <vlg@users.sourceforge.net>
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Library General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------------
#ifndef COMMON_UTILS_H
#define COMMON_UTILS_H

#include <sstream>

#include <string> 
#include <vector>
using std::vector;
using std::string;

/** CommonUtils.h

   A random collection of unrelated static functions.
*/

namespace ASSA {
namespace Utils {

    /** 
	Split character string into tokens separated by the whitespace
	character (blank, tab, newline, formfeed, and carriage return).
	The vec_ vector is emptied out prior parsing string text_.
	
	@param text_  string of tokens to split
	@param vec_  vector with tokens extracted from the string str_
    */
    void split (const char* text_, std::vector<std::string>& vec_);

    /**
       Split input string into two parts separated by the separator 
       character.
       
       @param text_  Input string to split
       @param sep_   Separator character
       @param lhs_   Return left-hand side of the input string
       @param rhs_   Return right-hand side of the input string
       @return 0 on success; -1 if separator character was not found.
    */
    int split_pair (const string& text_, char sep_, string& lhs_, string& rhs_);

    /**
       Trim string from the beginning to the left of the delimiter.
       Delimiter is removed as well.
     
       @param text_  String to modify
       @param delim_ Delimiter character
       @return 0 on success; -1 on error
     */
    int ltrim (std::string& text_, const std::string& delim_);

    /**
       Trim string from the delimiter to the end of the string.
       Delimiter is removed as well.

       @param text_  String to modify
       @param delim_ Delimiter character
       @return 0 on success; -1 on error

     */
    int rtrim (std::string& text_, const std::string& delimiter_);

	/** 
		Trim white spaces and tabs from the beginning and the end of
		the text string.

		@param text_ String to trim
	*/
	void trim_sides (std::string& text_);

    /** 
     * Expand the passed string in_ by substituting environment
     * variable names for their values. Environment variables must be 
     * preceeded by dollar sign and optionally enclosed in parentheses:
     * $ENV_NAME, or $(ENV_NAME), or ${ENV_NAME}. $HOME is equivalent
     * to '~' or '~username'. If later is used, "username" is looked up
     * in the password file.
     */
    std::string strenv (const char* in_);

    /**
     * Get current working directory. 
     *
     * @return the current working directory on success, and an empty
     * string on failure with errno set to indicate the error occured.
     */
    std::string get_cwd_name ();


} // end namespace Utils
} // end namespace ASSA

#endif /* COMMON_UTILS_H */  
