rrd_jinterface.c

来自「opennms得相关源码 请大家看看」· C语言 代码 · 共 626 行 · 第 1/2 页

C
626
字号
/* This file is part of the OpenNMS(R) Application. OpenNMS(R) is Copyright (C) 2002-2003 The OpenNMS Group, Inc.  All rights reserved. OpenNMS(R) is a derivative work, containing both original code, included code and modified code that was published under the GNU General Public License. Copyrights for modified  and included code are below. OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc. For more information contact:       OpenNMS Licensing       <license@opennms.org>      http://www.opennms.org/      http://www.opennms.com/*//*****************************************************************************   jRRD - Java Interface to Tobias Oetiker's RRDtool   RRDtool 1.0.28  Copyright Tobias Oetiker, 1997 - 2000     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 ***************************************************************************** * rrd_jinterface.c  Implementation of Java native method for calling *                   RRD functions (rrd_create(), rrd_update(), etc...) *                   from within a Java application. *  *                   Base code taken and modified from rrd_tool.c from  *                   RRDtool 1.0.28 distribution. * * rrd_jinterface.c,v 1.1.1.1 2001/11/11 17:34:38 ben Exp *****************************************************************************/#include <jni.h>#include <unistd.h>#if defined(__APPLE_CC__) || defined(__bsdi__) || defined(__FreeBSD__)#include <sys/malloc.h>#else#include <malloc.h>#endif#include <string.h>#include "rrd.h"#pragma export on#include "rrd_jinterface.h"#pragma export reset#if !defined(__SOLARIS__) && !defined(__APPLE_CC__) && !defined(__bsdi__) && !defined(__FreeBSD__)#include "getopt.h"#endif#define FALSE 0#define TRUE 1#undef DEBUG /* Enable/Disable debugging *//*---------------------------------------------------------------------------  *  * Description:  *	Parses provided command string and determines total number of  *     arguments.  Assumes that argument is space-delimited.  Exception  *     to this rule is made for double quotes.  When a double quote is  *     found all chars up to the next double quote will be kept together  *     as a single argument.  This was done to allow for filenames with  *     spaces.  *  * Args:  *		command		Command to parse.  *   * Return:  *		int	    	Argument count  *   ---------------------------------------------------------------------------*/staticint getArgumentCount(char* command) {	int dquoted = FALSE;	int count = 0;	int ii = 0;		for (ii=0; ii<strlen(command)+1; ii++) {		if (command[ii] == '\"') {			dquoted = dquoted ? FALSE : TRUE;		}		else if(dquoted)		{			continue;		}		else if(command[ii] == ' ')		{			count++;						// trim off the remaining white space			//			while(command[ii+1] == ' ')				ii++;		}		else if (command[ii] == '\0')		{			count++;		}	}		return count;}/*---------------------------------------------------------------------------  *  * Description:  *	Parses provided command string and builds an array of strings  *     consisting of each of the individual arguments which make up the   *     command.  Assumes that each argument is space-delimited.  Exception  *     to this rule is made for double quotes.  When a double quote is  *     found all chars up to the next double quote will be kept together  *     as a single argument.  This was done to allow for filenames with  *     spaces.  *  * Args:  *		char *command		Command to parse.  * 		char **argv		Array of character pointers which  *                                       will point to each parsed argument.  *   * Return:  *		int	    	Argument count  *   ---------------------------------------------------------------------------*/staticint buildArgList(char* command, char **argv) {	int dquoted = FALSE;	int count = 0;	char *token = NULL;	int  ii = 0;	int  x = 0;	char *buffer = (char *)malloc(strlen(command)+1);		// get working copy of the command string	strcpy(buffer, command);#ifdef DEBUG	printf("buildArgList(): command: '%s'\n", command);#endif	// iterate over the command string and parse out each	// argument/token	for (ii=0; ii<strlen(command)+1; ii++) {#ifdef DEBUG		printf("processing char: '%c'\n", command[ii]);#endif		if (command[ii] == '\"') {#ifdef DEBUG			printf("hit double quote.\n");#endif			dquoted = dquoted ? FALSE : TRUE;		}		else if(dquoted)		{#ifdef DEBUG			printf("double quote is true.\n");#endif			buffer[x++] = command[ii];		}		else if(command[ii] == ' ')		{#ifdef DEBUG			printf("hit space, creating token.\n");#endif						// null terminate string			buffer[x] = '\0';#ifdef DEBUG			printf("buffer=%s\n", buffer);#endif						// allocate storage for new token string			token = (char *)malloc(strlen(buffer)+1);				strcpy(token, buffer);#ifdef DEBUG			printf("token: %s\n", token);#endif						// Assign next argv pointer to the token			argv[count] = token;						// increment arg count			count++;						// trim off the remaining white space			//			while(command[ii+1] == ' ')				ii++;							// reset buffer index			x = 0;		}		else if (command[ii] == '\0')		{#ifdef DEBUG			printf("hit null, creating token.\n");#endif			// terminate buffer			buffer[x] = '\0';						/// allocate storage for new token string			token = (char *)malloc(strlen(buffer)+1);				strcpy(token, buffer);#ifdef DEBUG			printf("token: %s\n", token);#endif						// Assign next argv pointer to the token			argv[count] = token;						// increment arg count			count++;		}		else		{#ifdef DEBUG			printf("normal char.\n");#endif			buffer[x++] = command[ii];		}	}		free(buffer);	return count;} /*---------------------------------------------------------------------------  * JNI Function (as defined in machine-generated file 'rrd_jinterface.h')  *		Java_org_opennms_netmgt_rrd_Interface_launch()   *   * Description:  *	JNI method implementation which takes a pre-formatted RRD command  *     string, builds an argument list array of strings and calls the   *     appropriate RRD function with the arg list.  Does exactly what  *     RRDtool.exe does only from within a Java application as opposed  *     to the command line.    *   *		Sample command string:  *			"fetch test.rrd AVERAGE --start N --end N"  *   *     The above command would cause rrd_fetch() to be called with an argument  *     list consisting of the tokenized elements of the command string.  *       *     Please refer to the appropriate RRDtool documentation for additional  *     information on the various RRDtool commands and their arguments.  *  * Supported RRDtool Functions:  *    rrd_create()  *    rrd_update()  *    rrd_fetch()  *  * Args:  *		*env	Java execution environment reference  *		obj		Reference to the org.opennms.netmgt.rrd.Interface class  *		javaCmdStr	RRDtool style command string  *   * Return:  *		jobjectArray    Array of Java String objects where:  * 				  array[0] - NULL if successful or error text if failure.  * 				  array[1] - Space delimited list of data source names  * 						contained in the RRD file.  * 				  array[2]..array[n] - The "fetched" data.  Refer to   * 						the appropriate RRDtool documentation for   * 						the format of the output from the RRDtool   * 						fetch command.  * 				Will return NULL if memory allocation fails  *   ---------------------------------------------------------------------------*/JNIEXPORT jobjectArray JNICALLJava_org_opennms_netmgt_rrd_Interface_launch(JNIEnv *env, jobject obj, jstring javaCmdStr) {	/* Local vars */	jbyte *nativeCmdStr;	char *token;	char **argv;	int  argc=0;	int ii,jj;	int len;	jstring utf_str;	jclass  clazz;	jobjectArray resultsArray;	int bytesWritten;	jclass	clazzOutOfMem;	/* initialize args */	argv = NULL;	/* find out of memory error just in case */	clazzOutOfMem = (*env)->FindClass(env, "java/lang/OutOfMemoryError");	if(clazzOutOfMem == NULL || (*env)->ExceptionOccurred(env) != NULL)		return NULL;	/* get length of command string */	len = (*env)->GetStringUTFLength(env, javaCmdStr);	if((*env)->ExceptionOccurred(env) != NULL)		return NULL;	/* allocate memory to hold native command string */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?