← Back to team overview

linuxdcpp-team team mailing list archive

[Branch ~dcplusplus-team/dcplusplus/trunk] Rev 3123: r3122 continued, do away with C99 requirement; strncat vs strcat

 

------------------------------------------------------------
revno: 3123
committer: crise <crise@xxxxxxxxxx>
branch nick: trunk
timestamp: Sat 2012-11-10 05:44:55 +0200
message:
  r3122 continued, do away with C99 requirement; strncat vs strcat
added:
  plugins/Example/Dialog.c
  plugins/Example/Dialog.h
modified:
  plugins/Example/Plugin.c
  plugins/Example/Plugin.h
  plugins/Example/SConscript
  plugins/Example/pluginsdk.c
  pluginsdk/c/pluginsdk/Config.c
  pluginsdk/c/pluginsdk/Config.h


--
lp:dcplusplus
https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk

Your team Dcplusplus-team is subscribed to branch lp:dcplusplus.
To unsubscribe from this branch go to https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk/+edit-subscription
=== added file 'plugins/Example/Dialog.c'
--- plugins/Example/Dialog.c	1970-01-01 00:00:00 +0000
+++ plugins/Example/Dialog.c	2012-11-10 03:44:55 +0000
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2012 Jacek Sieka, arnetheduck on gmail point com
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include "stdafx.h"
+#include "Dialog.h"
+
+#ifndef __cplusplus
+# include <stdlib.h>
+# include <string.h>
+#else
+# include <cstdlib>
+# include <cstring>
+#endif
+
+#include <pluginsdk/Config.h>
+
+DCUtilsPtr utils = NULL;
+
+#ifdef _WIN32
+
+#include "resource.h"
+extern HINSTANCE hInst;
+
+BOOL onConfigInit(HWND hWnd) {
+	char* value = get_cfg("SendSuffix");
+	size_t len = strlen(value) + 1;
+	TCHAR* buf = (TCHAR*)memset(malloc(len * sizeof(TCHAR)), 0, len * sizeof(TCHAR));
+
+	utils->utf8_to_wcs(buf, value, len);
+	free(value);
+	value = NULL;
+
+	SetDlgItemText(hWnd, IDC_SUFFIX, buf);
+	SetWindowText(hWnd, _T(PLUGIN_NAME) _T(" Settings"));
+
+	free(buf);
+	return TRUE;
+}
+
+BOOL onConfigClose(HWND hWnd, UINT wID) {
+	if(wID == IDOK) {
+		int len = GetWindowTextLength(GetDlgItem(hWnd, IDC_SUFFIX)) + 1;
+		TCHAR* wbuf = (TCHAR*)memset(malloc(len * sizeof(TCHAR)), 0, len * sizeof(TCHAR));
+		char* value = (char*)memset(malloc(len), 0, len);
+
+		GetWindowText(GetDlgItem(hWnd, IDC_SUFFIX), wbuf, len);
+		utils->wcs_to_utf8(value, wbuf, len);
+		set_cfg("SendSuffix", value);
+
+		free(value);
+		free(wbuf);
+	}
+
+	EndDialog(hWnd, wID);
+	return FALSE;
+}
+
+INT_PTR CALLBACK configProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
+	UNREFERENCED_PARAMETER(lParam);
+	switch(uMsg) {
+		case WM_INITDIALOG:
+			return onConfigInit(hWnd);
+		case WM_COMMAND: {
+			switch(LOWORD(wParam)) {
+				case IDOK:
+				case IDCANCEL:
+				case IDCLOSE:
+					return onConfigClose(hWnd, LOWORD(wParam));
+			}
+		}
+	}
+	return FALSE;
+}
+
+Bool dialog_create(dcptr_t hWnd, DCCorePtr core) {
+	utils = (DCUtilsPtr)core->query_interface(DCINTF_DCPP_UTILS, DCINTF_DCPP_UTILS_VER);
+	if(utils) {
+		DialogBox(hInst, MAKEINTRESOURCE(IDD_PLUGINDLG), (HWND)hWnd, configProc);
+		return True;
+	}
+	return False;
+}
+
+#else
+
+Bool dialog_create(dcptr_t hWnd, DCCorePtr core) { return False; }
+
+#endif

=== added file 'plugins/Example/Dialog.h'
--- plugins/Example/Dialog.h	1970-01-01 00:00:00 +0000
+++ plugins/Example/Dialog.h	2012-11-10 03:44:55 +0000
@@ -0,0 +1,25 @@
+# include "resource.h"
+/*
+ * Copyright (C) 2012 Jacek Sieka, arnetheduck on gmail point com
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef DIALOG_H
+#define DIALOG_H
+
+Bool dialog_create(dcptr_t hWnd, DCCorePtr core);
+
+#endif /* DIALOG_H */

=== modified file 'plugins/Example/Plugin.c'
--- plugins/Example/Plugin.c	2012-11-09 05:36:37 +0000
+++ plugins/Example/Plugin.c	2012-11-10 03:44:55 +0000
@@ -19,6 +19,8 @@
 #include "stdafx.h"
 #include "Plugin.h"
 
+#include "Dialog.h"
+
 #ifndef __cplusplus
 # include <stdio.h>
 # include <stdlib.h>
@@ -31,29 +33,14 @@
 
 #include <pluginsdk/Config.h>
 
-#ifdef _WIN32
-# include "resource.h"
-# ifdef _MSC_VER
-#  define snprintf _snprintf
-#  define snwprintf _snwprintf
-# endif
-#elif __GNUC__
-# define stricmp strcasecmp
-# define strnicmp strncasecmp
-#else
-# error No supported compiler found
-#endif
-
 /* Variables */
 DCCorePtr dcpp;
 
 DCHooksPtr hooks;
-DCConfigPtr config;
 DCLogPtr logging;
 
 DCHubPtr hub;
 DCTaggerPtr tagger;
-DCUtilsPtr utils = NULL;
 DCUIPtr ui = NULL;
 
 /* Hook subscription store */
@@ -80,17 +67,13 @@
 	dcpp = core;
 
 	hooks = (DCHooksPtr)core->query_interface(DCINTF_HOOKS, DCINTF_HOOKS_VER);
-	config = (DCConfigPtr)core->query_interface(DCINTF_CONFIG, DCINTF_CONFIG_VER);
 	logging = (DCLogPtr)core->query_interface(DCINTF_LOGGING, DCINTF_LOGGING_VER);
 
 	hub = (DCHubPtr)core->query_interface(DCINTF_DCPP_HUBS, DCINTF_DCPP_HUBS_VER);
 	tagger = (DCTaggerPtr)core->query_interface(DCINTF_DCPP_TAGGER, DCINTF_DCPP_TAGGER_VER);
-#ifdef _WIN32
-	utils = (DCUtilsPtr)core->query_interface(DCINTF_DCPP_UTILS, DCINTF_DCPP_UTILS_VER);
 	ui = (DCUIPtr)core->query_interface(DCINTF_DCPP_UI, DCINTF_DCPP_UI_VER); 
-#endif
 
-	if(!hooks || !config || !logging || !hub || !tagger)
+	if(!hooks || !init_cfg(core) || !logging || !hub || !tagger)
 		return False;
 
 	if(eventId == ON_INSTALL) {
@@ -158,18 +141,18 @@
 	} else if(stricmp(cmd->command, "send") == 0) {
 		size_t len = strlen(cmd->params);
 		if(len > 0) {
-			ConfigStrPtr suffix = get_cfg("SendSuffix");
-			size_t msgLen = len + strlen(suffix->value) + 2;
+			char* suffix = get_cfg("SendSuffix");
+			size_t msgLen = len + strlen(suffix) + 2;
 			char* text = (char*)memset(malloc(msgLen), 0, msgLen);
 
-			strcat(text, cmd->params);
+			strncat(text, cmd->params, len);
 			text[len] = ' ';
-			strcat(text, suffix->value);
+			strncat(text, suffix, msgLen - (len + 2));
 
 			hub->send_message(hHub, text, (strnicmp(text, "/me ", 4) == 0) ? True : False);
 
 			free(text);
-			free_cfg((ConfigValuePtr)suffix);
+			free(suffix);
 		} else {
 			hub->local_message(hHub, "You must supply a parameter!", MSG_SYSTEM);
 		}
@@ -224,70 +207,6 @@
 	return False;
 }
 
-#ifdef _WIN32
-/* Config dialog stuff */
-BOOL onConfigInit(HWND hWnd) {
-	ConfigStrPtr value = get_cfg("SendSuffix");
-	size_t len = strlen(value->value) + 1;
-	TCHAR* buf = (TCHAR*)memset(malloc(len * sizeof(TCHAR)), 0, len * sizeof(TCHAR));
-
-	utils->utf8_to_wcs(buf, value->value, len);
-	free_cfg((ConfigValuePtr)value);
-	value = NULL;
-
-	SetDlgItemText(hWnd, IDC_SUFFIX, buf);
-	SetWindowText(hWnd, _T(PLUGIN_NAME) _T(" Settings"));
-
-	free(buf);
-	return TRUE;
-}
-
-BOOL onConfigClose(HWND hWnd, UINT wID) {
-	if(wID == IDOK) {
-		int len = GetWindowTextLength(GetDlgItem(hWnd, IDC_SUFFIX)) + 1;
-		TCHAR* wbuf = (TCHAR*)memset(malloc(len * sizeof(TCHAR)), 0, len * sizeof(TCHAR));
-		char* value = (char*)memset(malloc(len), 0, len);
-
-		GetWindowText(GetDlgItem(hWnd, IDC_SUFFIX), wbuf, len);
-		utils->wcs_to_utf8(value, wbuf, len);
-		set_cfg("SendSuffix", value);
-
-		free(value);
-		free(wbuf);
-	}
-
-	EndDialog(hWnd, wID);
-	return FALSE;
-}
-
-INT_PTR CALLBACK configProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
-	UNREFERENCED_PARAMETER(lParam);
-	switch(uMsg) {
-		case WM_INITDIALOG:
-			return onConfigInit(hWnd);
-		case WM_COMMAND: {
-			switch(LOWORD(wParam)) {
-				case IDOK:
-				case IDCANCEL:
-				case IDCLOSE:
-					return onConfigClose(hWnd, LOWORD(wParam));
-			}
-		}
-	}
-	return FALSE;
-}
-#endif
-
-Bool onConfig(dcptr_t hWnd) {
-#ifdef _WIN32
-	if(utils) {
-		DialogBox(hInst, MAKEINTRESOURCE(IDD_PLUGINDLG), (HWND)hWnd, configProc);
-		return True;
-	}
-#endif
-	return False;
-}
-
 /* Plugin main function */
 Bool DCAPI pluginMain(PluginState state, DCCorePtr core, dcptr_t pData) {
 	switch(state) {
@@ -298,7 +217,8 @@
 		case ON_UNLOAD:
 			return onUnload();
 		case ON_CONFIGURE:
-			return onConfig(pData);
+			/* Note: core may be NULL for this call */
+			return dialog_create(pData, dcpp);
 		default: return False;
 	}
 }

=== modified file 'plugins/Example/Plugin.h'
--- plugins/Example/Plugin.h	2012-11-09 05:36:37 +0000
+++ plugins/Example/Plugin.h	2012-11-10 03:44:55 +0000
@@ -19,8 +19,14 @@
 #ifndef PLUGIN_H
 #define PLUGIN_H
 
-#ifdef _WIN32
-extern HINSTANCE hInst;
+#ifdef _MSC_VER
+# define snprintf _snprintf
+# define snwprintf _snwprintf
+#elif __GNUC__
+# define stricmp strcasecmp
+# define strnicmp strncasecmp
+#else
+# error No supported compiler found
 #endif
 
 /* Event handlers */

=== modified file 'plugins/Example/SConscript'
--- plugins/Example/SConscript	2012-11-09 05:36:37 +0000
+++ plugins/Example/SConscript	2012-11-10 03:44:55 +0000
@@ -9,7 +9,6 @@
 
 if 'g++' in env['LINK']:
 	env.Append(SHLINKFLAGS = '-Wl,--add-stdcall-alias')
-	env.Prepend(CCFLAGS = '-c -std=c99')
 elif 'link' in env['LINK'] and env['arch'] == 'x86':
 	sources.append('Plugin.def')
 

=== modified file 'plugins/Example/pluginsdk.c'
--- plugins/Example/pluginsdk.c	2012-11-09 05:36:37 +0000
+++ plugins/Example/pluginsdk.c	2012-11-10 03:44:55 +0000
@@ -3,4 +3,4 @@
 
 #include "version.h"
 
-#include <pluginsdk/Config.c>
\ No newline at end of file
+#include <pluginsdk/Config.c>

=== modified file 'pluginsdk/c/pluginsdk/Config.c'
--- pluginsdk/c/pluginsdk/Config.c	2012-11-09 05:36:37 +0000
+++ pluginsdk/c/pluginsdk/Config.c	2012-11-10 03:44:55 +0000
@@ -20,8 +20,45 @@
 
 #include "Config.h"
 
+#include <stdlib.h>
 #include <string.h>
 
+DCConfigPtr config = NULL;
+
+Bool DCAPI init_cfg(DCCorePtr core) {
+	config = (DCConfigPtr)core->query_interface(DCINTF_CONFIG, DCINTF_CONFIG_VER);
+	return config ? True : False;
+}
+
+char* DCAPI get_cfg(const char* name) {
+	ConfigStrPtr val = (ConfigStrPtr)config->get_cfg(PLUGIN_GUID, name, CFG_TYPE_STRING);
+	int len = strlen(val->value) + 1;
+	char* value = (char*)memset(malloc(len), 0, len);
+
+	strncpy(value, val->value, len);
+	config->release((ConfigValuePtr)val);
+
+	return value;
+}
+
+int32_t DCAPI get_cfg_int(const char* name) {
+	ConfigIntPtr val = (ConfigIntPtr)config->get_cfg(PLUGIN_GUID, name, CFG_TYPE_INT);
+	int32_t value = val->value;
+
+	config->release((ConfigValuePtr)val);
+
+	return value;
+}
+
+int64_t DCAPI get_cfg_int64(const char* name) {
+	ConfigInt64Ptr val = (ConfigInt64Ptr)config->get_cfg(PLUGIN_GUID, name, CFG_TYPE_INT64);
+	int64_t value = val->value;
+
+	config->release((ConfigValuePtr)val);
+
+	return value;
+}
+
 void DCAPI set_cfg(const char* name, const char* value) {
 	ConfigStr val;
 	memset(&val, 0, sizeof(ConfigStr));
@@ -49,9 +86,7 @@
 	config->set_cfg(PLUGIN_GUID, name, (ConfigValuePtr)&val);
 }
 
-extern ConfigStrPtr DCAPI get_cfg(const char* name);
-extern ConfigIntPtr DCAPI get_cfg_int(const char* name);
-extern ConfigInt64Ptr DCAPI get_cfg_int64(const char* name);
+ConfigValuePtr DCAPI get_core_cfg(const char* name) { return config->get_cfg("CoreSetup", name, CFG_TYPE_UNKNOWN); }
+void DCAPI free_core_cfg(ConfigValuePtr val) { config->release(val); }
 
-extern ConfigValuePtr DCAPI get_core_cfg(const char* name);
-extern void DCAPI free_cfg(ConfigValuePtr val);
\ No newline at end of file
+const char* DCAPI get_cfg_path(PathType path) { return config->get_path(path); }

=== modified file 'pluginsdk/c/pluginsdk/Config.h'
--- pluginsdk/c/pluginsdk/Config.h	2012-11-09 05:36:37 +0000
+++ pluginsdk/c/pluginsdk/Config.h	2012-11-10 03:44:55 +0000
@@ -25,17 +25,19 @@
 
 #include <pluginsdk/PluginDefs.h>
 
-extern DCConfigPtr config;
+Bool DCAPI init_cfg(DCCorePtr core);
+
+char* DCAPI get_cfg(const char* name);
+int32_t DCAPI get_cfg_int(const char* name);
+int64_t DCAPI get_cfg_int64(const char* name);
 
 void DCAPI set_cfg(const char* name, const char* value);
 void DCAPI set_cfg_int(const char* name, int32_t value);
 void DCAPI set_cfg_int64(const char* name, int64_t value);
 
-inline ConfigStrPtr DCAPI get_cfg(const char* name) { return (ConfigStrPtr)config->get_cfg(PLUGIN_GUID, name, CFG_TYPE_STRING); }
-inline ConfigIntPtr DCAPI get_cfg_int(const char* name) { return (ConfigIntPtr)config->get_cfg(PLUGIN_GUID, name, CFG_TYPE_INT); }
-inline ConfigInt64Ptr DCAPI get_cfg_int64(const char* name) { return (ConfigInt64Ptr)config->get_cfg(PLUGIN_GUID, name, CFG_TYPE_INT64); }
-
-inline ConfigValuePtr DCAPI get_core_cfg(const char* name) { return config->get_cfg("CoreSetup", name, CFG_TYPE_UNKNOWN); }
-inline void DCAPI free_cfg(ConfigValuePtr val) { config->release(val); }
-
-#endif
\ No newline at end of file
+ConfigValuePtr DCAPI get_core_cfg(const char* name);
+void DCAPI free_core_cfg(ConfigValuePtr val);
+
+const char* DCAPI get_cfg_path(PathType path);
+
+#endif