/* 
 * © 2007-2008 MySQL AB, 2008-2009 Sun Microsystems, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#ifndef _BE_COMMON_H_
#define _BE_COMMON_H_

#include <cstring>
#include <vector>
#include <string>
#include <glib.h>

#ifndef _WIN32
#include <stdexcept>
#include <stdarg.h>
#endif

#include <string.h>

#include "wbpublic_public_interface.h"

#define _(s) s

namespace bec {

  std::string WBPUBLICBACKEND_PUBLIC_FUNC fmt(const char *fmt, ...);

#define DATETIME_FMT "%Y-%m-%d %H:%M"
  std::string WBPUBLICBACKEND_PUBLIC_FUNC fmttime(time_t t= 0, const char *fmt= "%b %d, %Y");

  std::string WBPUBLICBACKEND_PUBLIC_FUNC escape_sql_string(const std::string &text);
  
  std::string WBPUBLICBACKEND_PUBLIC_FUNC replace_string(const std::string &s,
                            const std::string &from,
                            const std::string &to);

  std::string WBPUBLICBACKEND_PUBLIC_FUNC rtrim(const std::string &value);

  // replaces a variable from a string in format %variable%
  // a filter can be passed to the variable as in %variable|filter%
  // supported filters are upper, lower and capitalize
  std::string WBPUBLICBACKEND_PUBLIC_FUNC replace_variable(const std::string &format, const std::string &variable, const std::string &value);
  
  std::string WBPUBLICBACKEND_PUBLIC_FUNC append_extension_if_needed(const std::string &path,
                                                              const std::string &ext);

  std::string WBPUBLICBACKEND_PUBLIC_FUNC normalize_path(const std::string path);

  inline bool has_prefix(const std::string &str, const std::string &pref)
  {
    if (strncmp(str.c_str(), pref.c_str(), pref.length())==0)
      return true;
    return false;
  }


  inline bool has_suffix(const std::string &str, const std::string &suf)
  {
    if (suf.length() < str.length() && strncmp(str.c_str()+str.length()-suf.length(), suf.c_str(), suf.length())==0)
      return true;
    return false;
  }


  std::string WBPUBLICBACKEND_PUBLIC_FUNC make_path(const std::string &prefix, const std::string &file);

  inline std::string pathlist_append(const std::string &l, const std::string &s)
  {
    if (l.empty())
      return s;
    return l+G_SEARCHPATH_SEPARATOR+s;
  }

  inline std::string pathlist_prepend(const std::string &l, const std::string &s)
  {
    if (l.empty())
      return s;
    return s+G_SEARCHPATH_SEPARATOR+l;
  }

  std::vector<std::string> WBPUBLICBACKEND_PUBLIC_FUNC split_string(const std::string &s, const std::string &sep, int count=0);

  //! \brief RAII style GMutex locker/unlocker  
  class WBPUBLICBACKEND_PUBLIC_FUNC GMutexLock
  {
      GMutex    *mutex;
    public:
      GMutexLock(GMutex* mtx) : mutex(mtx)  {   g_mutex_lock(mtx);     }
      ~GMutexLock()                         {   g_mutex_unlock(mutex); }
  };

  //! \brief RAII style GStaticMutex locker/unlocker  
  class WBPUBLICBACKEND_PUBLIC_FUNC GStaticMutexLock // Note: must compile with strict-aliasing disabled (-fno-strict-aliasing in gcc)
  {
      GStaticMutex    &mutex;
    public:
    GStaticMutexLock(GStaticMutex& mtx);
    ~GStaticMutexLock();
  };
  
};

#endif /* _BE_COMMON_H_ */
