/* * $Id: tools.cpp,v 1.3 2007/03/02 13:02:42 ozawa Exp $ * * Copyright 2003- ONGS Inc. All rights reserved. * * author: Masanori OZAWA (ozawa@ongs.co.jp) * version: $Revision: 1.3 $ * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY ONGS INC ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL ONGS INC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * The views and conclusions contained in the software and documentation are * those of the authors and should not be interpreted as representing official * policies, either expressed or implied, of the ONGS Inc. * */ #include "../include/fusenshi.h" #include #include static char *_temp_name(const char *pBaseDir, const char *pPrefix); /** * ユーザのホームディレクトリへのパスを取得します。 * * @return ユーザのホームディレクトリへのパス。 * 取得したパスの改変/解放はしないでください。 */ const char* getHomeDir() { char *homedir = getenv("HOME"); if (!homedir || 0 > strlen(homedir)) { #ifdef DEBUG fprintf(stderr, "%s: can't get home directory by getenv().\n", APP_NAME); #endif return NULL; } return homedir; } /** * アプリケーション用のディレクトリへのパスを取得します。 * * @param pAppName アプリケーション名を指定します(NULL Terminated) * @return アプリケーション用のディレクトリへのパス。 * ディレクトリ名の終端は pAppName の終端と同じです。 * 取得したパスの改変/解放はしないでください。 */ const char* getAppDir(const char *pAppName) { static char buf[PATH_MAX +1]; const char *pHome = getHomeDir(); if (pHome) { memset(buf, '\0', sizeof(buf)); strncpy(buf, pHome, sizeof(buf) -1); strncat(buf, "/.", sizeof(buf) -1); strncat(buf, pAppName, sizeof(buf) -1); return buf; } return NULL; } /** * 付箋紙用のディレクトリが存在しない場合は、新規に構築します。 */ void createFusenshiDir() { const char *pDirPath = NULL; struct stat hdir; // 設定ディレクトリ名を作成 pDirPath = getAppDir(APP_NAME); // fusenshi用の設定ディレクトリの有無を確認 if (pDirPath && 0 > stat(pDirPath, &hdir)) { // ディレクトリを作成 mkdir(pDirPath, 0700); } } /** * 新規付箋紙ファイル名を取得します。 * * @return 新規付箋紙ファイルのファイル名。 * ファイルの構築は行いません。 * 不要になったタイミングで free() してください。 */ char* createNewFusenshiFileName() { const char *pDirPath = NULL; char *pResult = NULL; // スレッドセーフ用の同期処理 static GStaticMutex mutex = G_STATIC_MUTEX_INIT; g_static_mutex_lock(&mutex); // 設定ディレクトリ名を作成 pDirPath = getAppDir(APP_NAME); if (pDirPath) { // fusenshi用の設定ディレクトリの有無を確認 createFusenshiDir(); // 新規の空データファイル名を構築 pResult = _temp_name(pDirPath, "fusenshi."); } // スレッドセーフ用の同期処理 g_static_mutex_unlock(&mutex); // 新規の空データファイル名を構築 return pResult; } /** * 一時ファイル名を取得します。 * * @param ディレクトリパス * @param 拡張子 * @return 一時ファイル名。不要になったタイミングで free() してください。 */ static char *_temp_name(const char *pBaseDir, const char *pPrefix) { int count = 0; int length = strlen(pBaseDir); char *pTemp = NULL; char *pResult = NULL; char *pDir = (char *)malloc(length +2); struct stat hstat; if (0 >= length) { return NULL; } if (!pDir) { return NULL; } strcpy(pDir, pBaseDir); if (pDir[length -1] != '/') { strcat(pDir, "/"); } pTemp = (char *)malloc(strlen(pDir) + strlen(pPrefix) + 10); pResult = (char *)malloc(strlen(pDir) + strlen(pPrefix) + 10); if (!pTemp || !pResult) { free(pDir); if (pTemp) free(pTemp); if (pResult) free(pResult); return NULL; } strcpy(pTemp, pDir); strcat(pTemp, pPrefix); for (count = 1; MAX_FUSENSHI_NUM >= count; count++) { sprintf(pResult, "%s%d", pTemp, count); if (0 > stat(pResult, &hstat)) { break; } } if (MAX_FUSENSHI_NUM < count) { free(pResult); pResult = NULL; } free(pTemp); free(pDir); return pResult; } /** * 文字列の前後の空白を除去します。 * * @param szSrc 除去する前の文字列 * @return 前後の空白を除去した文字列 */ Glib::ustring strTrim(Glib::ustring& szSrc) { Glib::ustring str = strTrimLeft(szSrc); return strTrimRight(str); } /** * 文字列の前の空白を除去します。 * * @param szSrc 除去する前の文字列 * @return 前の空白を除去した文字列 */ Glib::ustring strTrimLeft(Glib::ustring& szSrc) { size_t firstPos = szSrc.find_first_not_of(" \t\r\n"); size_t length = szSrc.length(); if (0 > firstPos) firstPos = 0; else if (length < firstPos) firstPos = length; return szSrc.substr(firstPos); } /** * 文字列の後の空白を除去します。 * * @param szSrc 除去する前の文字列 * @return 後の空白を除去した文字列 */ Glib::ustring strTrimRight(Glib::ustring& szSrc) { size_t lastPos = szSrc.find_last_not_of(" \t\r\n"); size_t length = szSrc.length(); if (0 > lastPos) lastPos = length; else if (length <= lastPos) lastPos = length -1; return szSrc.substr(0, lastPos +1); } /** * IPアドレスの文字列表現を取得します。 * * @param nAddr IPアドレス * @return IPアドレスの文字列表現。 */ Glib::ustring IP2STR(long nAddr) { typedef union _ADDR { long laddr; struct { unsigned char b1; unsigned char b2; unsigned char b3; unsigned char b4; } byte; } ADDR; ADDR addr = {nAddr}; Glib::ustring result; gchar buf[32]; memset(buf, '\0', sizeof(buf)); #if _BYTE_ORDER == _LITTLE_ENDIAN snprintf(buf, sizeof(buf) -1, "%d.%d.%d.%d", addr.byte.b1, addr.byte.b2, addr.byte.b3, addr.byte.b4); #else /*_BYTE_ORDER == _BIG_ENDIAN */ snprintf(buf, sizeof(buf) -1, "%d.%d.%d.%d", addr.byte.b4, addr.byte.b3, addr.byte.b2, addr.byte.b1); #endif result = buf; return result; } /** * 文字列から色を生成し、設定します。 * * @param pColor GdkColor構造体 * @param pValue 色情報を保持する文字列 * @return 設定に成功した場合は true を戻す。 */ bool string2color(GdkColor *pColor, const char *pValue) { if (!pColor || !pValue) { throw "Null Pointer Exception."; } bool result = false; // gtk標準方式の色を解析 if (gdk_color_parse(pValue, pColor)) { result = true; } // 従来(0.7.0以前)の方式の色を解析 else if (6 == strlen(pValue)) { Glib::ustring buf = pValue; buf.insert(0, "#"); result = gdk_color_parse(buf.c_str(), pColor); } return result; } /** * 色を表現する文字列を取得します。 * * @param pColor GdkColor構造体 * @param szBuffer 色文字列を格納するバッファ */ void color2string(const GdkColor *pColor, Glib::ustring& szBuffer) { char buf[32] = {0}; if (!pColor) { throw "Null Pointer Exception."; } snprintf(buf, sizeof(buf) -1, "#%04X%04X%04X", pColor->red, pColor->green, pColor->blue); szBuffer = buf; }