示例#1
0
	std::string timestamp(time_t aTime, const std::string& aFormat)
	{
		tm* theTime = localtime(&aTime);
		std::string ret = aFormat;
		setlocale(LC_TIME, "");
		static timestamp_codes sCodes;
		sCodes.iTime = *theTime;
		parse_codes(ret, sCodes.iCodes);
		return ret;
	}
示例#2
0
文件: strings.c 项目: RJVB/xgraph
int ascanf_getstring ( ASCB_ARGLIST ) 
{ ASCB_FRAME_SHORT
  ascanf_Function *s1= NULL, *ret= NULL, *title= NULL, *msg= NULL;
  ascanf_Function *af= NULL;
  static ascanf_Function AF= {NULL};
  static char *AFname= "GetString-Static-StringPointer";
  int take_usage, maxlen= -1;
  FILE *fp= NULL;
	af= ⁡
	if( af->name ){
	  double oa= af->own_address;
		xfree(af->usage);
		memset( af, 0, sizeof(ascanf_Function) );
		af->own_address= oa;
	}
	else{
		af->usage= NULL;
		af->type= _ascanf_variable;
		af->internal= True;
		af->name= AFname;
		take_ascanf_address(af);
	}
	af->name= AFname;
	af->type= _ascanf_variable;
	af->internal= True;
	ascanf_arg_error= 0;
	if( ascanf_arguments> 0 ){
		if( !(s1= parse_ascanf_address(args[0], _ascanf_variable, "ascanf_getstring", (int) ascanf_verbose, &take_usage )) || 
			!take_usage
		){
			fprintf( StdErr, " (warning: 1st argument is not a valid stringpointer)== " );
			ret= s1= NULL;
			maxlen= (int) args[0];
		}
		else{
			ret= s1;
			maxlen= ret->value;
		}
	}
	if( ascanf_arguments> 1 ){
		if( !(msg= parse_ascanf_address(args[1], 0, "ascanf_getstring", (int) ascanf_verbose, NULL )) || 
			(msg->type== _ascanf_procedure || msg->type== _ascanf_function || !msg->usage )
		){
			fprintf( StdErr, " (warning: message (2nd) argument is not a valid string)== " );
			if( msg && msg->fp ){
				fp= msg->fp;
			}
			msg= NULL;
		}
		else if( msg && msg->fp ){
			fp= msg->fp;
		}
	}
	if( !fp && ascanf_arguments> 2 ){
		if( !(title= parse_ascanf_address(args[2], 0, "ascanf_getstring", (int) ascanf_verbose, NULL )) || 
			(title->type== _ascanf_procedure || title->type== _ascanf_function || !title->usage )
		){
			fprintf( StdErr, " (warning: title (3rd) argument is not a valid string)== " );
			title= NULL;
		}
	}
	if( ret ){
		af= ret;
	}
	af->is_address= af->take_address= True;
	af->is_usage= af->take_usage= True;
	if( !ascanf_SyntaxCheck ){
	  LocalWin *wi= aWindow(ActiveWin);
		if( maxlen> 0 && (wi || fp) ){
		  ALLOCA( buf, char, maxlen, blen );
		  char *nbuf;
			if( fp ){
				xfree( af->usage );
				if( (nbuf= fgets( buf, maxlen-1, fp )) ){
					af->usage= strdup(nbuf);
				}
				else{
					af->value= 0;
				}
			}
			else{
				if( s1 && s1->usage ){
					strncpy( buf, s1->usage, maxlen-1 );
					buf[maxlen-1]= '\0';
				}
				else{
					buf[0]= '\0';
				}
				if( wi && (nbuf= xtb_input_dialog( wi->window, buf, (*buf)? strlen(buf)* 1.5 : 80, maxlen,
						(msg)? parse_codes(msg->usage) : "Please enter a string",
						(title)? parse_codes(title->usage) :parse_codes( "#x01Request"),
						  /* should this one be modal??? */
						False,
						NULL, NULL, "Files", SimpleFileDialog_h_ptr, "Edit", SimpleEdit_h_ptr
					))
				){
					xfree( af->usage );
					if( ret ){
						ret->usage= strdup(nbuf);
					}
					else{
						af->usage= strdup(nbuf);
					}
					if( nbuf!= buf ){
						xfree(nbuf);
					}
				}
				else{
					af->value= 0;
				}
			}
		}
		else{
			ascanf_emsg= "invalid max. string length or no active window!";
			ascanf_arg_error= True;
		}
	}

	*result= af->own_address;
	return(!ascanf_arg_error);
}
示例#3
0
文件: AscanfCall.c 项目: RJVB/xgraph
static PyObject *AscanfCall( ascanf_Function *af, PyObject *arglist, long repeats, int asarray, int deref,
						PAO_Options *opts, char *caller )
{ int fargs= 0, aargc= 0, volatile_args= 0;
  double result= 0, *aresult=NULL;
  static double *AARGS= NULL;
  static char *ATYPE= NULL;
  static ascanf_Function *LAF= NULL;
  static size_t LAFN= 0;
  double *aargs= NULL;
  char *atype= NULL;
  ascanf_Function *laf= NULL, *af_array= NULL;
  size_t lafN= 0;
  PyObject *ret= NULL;
  static ascanf_Function *nDindex= NULL;
  int aV = ascanf_verbose;

	if( arglist ){
		if( PyList_Check(arglist) ){
			if( !(arglist= PyList_AsTuple(arglist)) ){
 				PyErr_SetString( XG_PythonError, "unexpected failure converting argument list to tuple" );
// 				PyErr_SetString( PyExc_RuntimeError, "unexpected failure converting argument list to tuple" );
				return(NULL);
			}
		}
		if( !PyTuple_Check(arglist) ){
			PyErr_SetString(
 				XG_PythonError,
// 				PyExc_SyntaxError,
				"arguments to the ascanf method should be passed as a tuple or list\n"
				" NB: a 1-element tuple is specified as (value , ) !!\n"
			);
			return(NULL);
		}

		aargc= PyTuple_Size(arglist);
	}
	else{
		aargc= 0;
	}
	if( !af ){
		goto PAC_ESCAPE;
	}
	if( af->type!= _ascanf_procedure && af->Nargs> 0 ){
	  /* procedures can have as many arguments as MaxArguments, which is probably too much to allocate here.
	   \ However, we know how many arguments a function can get (if all's well...), and we can assure that
	   \ it will have space for those arguments
	   \ 20061015: unless it also has MaxArguments, i.e. Nargs<0 ...
	   */
		fargs= af->Nargs;
	}
	{ long n= (aargc+fargs+1)*2;
		if( opts->call_reentrant ){
			lafN= n;
			aargs= (double*) calloc( lafN, sizeof(double) );
			atype= (char*)  calloc( lafN, sizeof(char) );
			if( !aargs || !atype || !(laf= (ascanf_Function*) calloc( lafN, sizeof(ascanf_Function) )) ){
				PyErr_NoMemory();
				return(NULL);
			}
		}
		else{
			if( !LAF ){
				LAFN= n;
				AARGS= (double*) calloc( LAFN, sizeof(double) );
				ATYPE= (char*) calloc( LAFN, sizeof(char) );
				if( !AARGS || !ATYPE || !(LAF= (ascanf_Function*) calloc( LAFN, sizeof(ascanf_Function) )) ){
					PyErr_NoMemory();
					return(NULL);
				}
			}
			else if( n> LAFN ){
				AARGS= (double*) realloc( AARGS, n * sizeof(double) );
				ATYPE= (char*) realloc( ATYPE, n * sizeof(char) );
				if( !AARGS || !ATYPE || !(LAF= (ascanf_Function*) realloc( LAF, n * sizeof(ascanf_Function) )) ){
					PyErr_NoMemory();
					return(NULL);
				}
				else{
					for( ; LAFN< n; LAFN++ ){
						AARGS[LAFN]= 0;
						memset( &LAF[LAFN], 0, sizeof(ascanf_Function) );
					}
				}
				LAFN= n;
			}
			aargs= AARGS;
			atype= ATYPE;
			laf= LAF;
			lafN= LAFN;
		}
	}

	{ int a= 0, i;
		if( opts->verbose > 1 ){
			ascanf_verbose = 1;
		}
		if( af->type== _ascanf_array ){
			if( !nDindex ){
				nDindex= Py_getNamedAscanfVariable("nDindex");
			}
			if( nDindex ){
				af_array= af;
				aargs[a]= (af->own_address)? af->own_address : take_ascanf_address(af);
				af= nDindex;
				a+= 1;
			}
		}
		for( i= 0; i< aargc; i++, a++ ){
		  PyObject *arg= PyTuple_GetItem(arglist, i);
		  ascanf_Function *aaf;

			if( PyFloat_Check(arg) ){
				aargs[a]= PyFloat_AsDouble(arg);
				atype[a]= 1;
			}
#ifdef USE_COBJ
			else if( PyCObject_Check(arg) ){
				if( (aaf= PyCObject_AsVoidPtr(arg)) && (PyCObject_GetDesc(arg)== aaf->function) ){
					aargs[a]= (aaf->own_address)? aaf->own_address : take_ascanf_address(aaf);
					atype[a]= 2;
				}
				else{
 					PyErr_SetString( XG_PythonError, "unsupported PyCObject type does not contain ascanf pointer" );
// 					PyErr_SetString( PyExc_TypeError, "unsupported PyCObject type does not contain ascanf pointer" );
					goto PAC_ESCAPE;
				}
			}
#else
			else if( PyAscanfObject_Check(arg) ){
				if( (aaf= PyAscanfObject_AsAscanfFunction(arg)) ){
					aargs[a]= (aaf->own_address)? aaf->own_address : take_ascanf_address(aaf);
					atype[a]= 2;
				}
				else{
 					PyErr_SetString( XG_PythonError, "invalid PyAscanfObject type does not contain ascanf pointer" );
// 					PyErr_SetString( PyExc_TypeError, "invalid PyAscanfObject type does not contain ascanf pointer" );
					goto PAC_ESCAPE;
				}
			}
#endif
			else if( PyInt_Check(arg) || PyLong_Check(arg) ){
				aargs[a]= PyInt_AsLong(arg);
				atype[a]= 3;
			}
			else if( PyBytes_Check(arg)
#ifdef IS_PY3K
				|| PyUnicode_Check(arg)
#endif
			){
			  static char *AFname= "AscanfCall-Static-StringPointer";
			  ascanf_Function *saf= &laf[a];
				memset( saf, 0, sizeof(ascanf_Function) );
				saf->type= _ascanf_variable;
				saf->function= ascanf_Variable;
				if( !(saf->name= PyObject_Name(arg)) ){
					saf->name= XGstrdup(AFname);
				}
				saf->is_address= saf->take_address= True;
				saf->is_usage= saf->take_usage= True;
				saf->internal= True;
#ifdef IS_PY3K
				if( PyUnicode_Check(arg) ){
				  PyObject *bytes = NULL;
				  char *str = NULL;
					PYUNIC_TOSTRING( arg, bytes, str );
					if( !str ){
						if( bytes ){
							Py_XDECREF(bytes);
						}
						goto PAC_ESCAPE;
					}
					saf->usage= parse_codes( XGstrdup(str) );
					Py_XDECREF(bytes);
				}
				else
#endif
				{
					saf->usage= parse_codes( XGstrdup( PyBytes_AsString(arg) ) );
				}
				aargs[a]= take_ascanf_address(saf);
				atype[a]= 4;
				if( i && af_array ){
					volatile_args+= 1;
				}
			}
			else if( PyArray_Check(arg)
				|| PyTuple_Check(arg)
				|| PyList_Check(arg)
			){
			  static char *AFname= "AscanfCall-Static-ArrayPointer";
			  ascanf_Function *saf= &laf[a];
			  PyArrayObject *parray;
				atype[a]= 6;
				if( PyList_Check(arg) ){
					if( !(arg= PyList_AsTuple(arg)) ){
 						PyErr_SetString( XG_PythonError, "unexpected failure converting argument to tuple" );
// 						PyErr_SetString( PyExc_RuntimeError, "unexpected failure converting argument to tuple" );
						goto PAC_ESCAPE;
/* 						return(NULL);	*/
					}
					else{
						atype[a]= 5;
					}
				}
				memset( saf, 0, sizeof(ascanf_Function) );
				saf->type= _ascanf_array;
				saf->function= ascanf_Variable;
				if( !(saf->name= PyObject_Name(arg)) ){
					saf->name= XGstrdup(AFname);
				}
				saf->is_address= saf->take_address= True;
				saf->internal= True;
				if( a ){
					saf->car= &laf[a-1];
				}
				else{
					saf->car= &laf[lafN-1];
				}
				if( PyTuple_Check(arg) ){
					saf->N= PyTuple_Size(arg);
					parray= NULL;
				}
				else{
					saf->N= PyArray_Size(arg);
					parray= (PyArrayObject*) arg;
					atype[a]= 7;
				}
				if( (saf->array= (double*) malloc( saf->N * sizeof(double) )) ){
				  int j;
					if( parray ){
					  PyArrayObject* xd= NULL;
					  double *PyArrayBuf= NULL;
					  PyArrayIterObject *it;
						if( (xd = (PyArrayObject*) PyArray_ContiguousFromObject( (PyObject*) arg, PyArray_DOUBLE, 0, 0 )) ){
							PyArrayBuf= (double*)PyArray_DATA(xd); /* size would be N*sizeof(double) */
						}
						else{
							it= (PyArrayIterObject*) PyArray_IterNew(arg);
						}
						if( PyArrayBuf ){
// 							for( j= 0; j< saf->N; j++ ){
// 								  /* 20061016: indices used to be i?!?! */
// 								saf->array[j]= PyArrayBuf[j];
// 							}
							if( saf->array != PyArrayBuf ){
								memcpy( saf->array, PyArrayBuf, saf->N * sizeof(double) );
							}
						}
						else{
							for( j= 0; j< saf->N; j++ ){
								saf->array[j]= PyFloat_AsDouble( PyArray_DESCR(parray)->f->getitem( it->dataptr, arg) );
								PyArray_ITER_NEXT(it);
							}
						}
						if( xd ){
							Py_XDECREF(xd);
						}
						else{
							Py_DECREF(it);
						}
					}
					else{
						for( j= 0; j< saf->N; j++ ){
							saf->array[j]= PyFloat_AsDouble( PyTuple_GetItem(arg,j) );
						}
					}
					aargs[a]= take_ascanf_address(saf);
					if( i && af_array ){
						volatile_args+= 1;
					}
				}
				else{
					PyErr_NoMemory();
					goto PAC_ESCAPE;
				}
			}
#if 0
			else{
 				PyErr_SetString( XG_PythonError, "arguments should be scalars, strings, arrays or ascanf pointers" );
// 				PyErr_SetString( PyExc_SyntaxError, "arguments should be scalars, strings, arrays or ascanf pointers" );
				goto PAC_ESCAPE;
			}
#else
			else{
			  static char *AFname= "AscanfCall-Static-PyObject";
			  ascanf_Function *saf= &laf[a];
				memset( saf, 0, sizeof(ascanf_Function) );
				saf->function= ascanf_Variable;
				saf->internal= True;
				saf= make_ascanf_python_object( saf, arg, "AscanfCall" );
				if( !saf->name ){
					if( saf->PyObject_Name ){
						saf->name= XGstrdup(saf->PyObject_Name);
					}
					else{
						saf->name= XGstrdup(AFname);
					}
				}
				aargs[a]= take_ascanf_address(saf);
				atype[a]= 4;
				if( i && af_array ){
					volatile_args+= 1;
				}
			}
#endif
		}
		if( a> aargc ){
			aargc= a;
		}
	}