ROPO
Loading...
Searching...
No Matches
fmi_util.c
1
22#include <stdlib.h>
23#include <stdio.h>
24#include <string.h>
25#include <math.h>
26#include "fmi_util.h"
27#include "rave_alloc.h"
28
29char *fmi_util_comment=NULL;
30
31char fmi_util_command_line[FMI_UTIL_COMMAND_LINE_LEN];
32
33/* GENERAL argv UTILITY */
34char *argument(int argc, char **argv, char *target){
35 int i=0;
36 while (++i<argc)
37 if (!strcmp(argv[i],target)){
38 if (i!=argc-1) /* if just a flag */
39 return argv[i+1];
40 else
41 return argv[i];
42 }
43 return NULL;
44}
45
46/*int param(char *s, char *key,char *shortcut_key){ */
47int param(int argc,char **argv,int index,char *key){
48 if (index<1)
49 return 0;
50 if (index>=argc)
51 return 0;
52 /* fmi_error("param arg index overflow"); */
53 if (strcmp(argv[index],key)==0)
54 return 1;
55 else
56 return 0;
57}
58
59int paramn(int argc,char **argv,int index,char *key){
60 if (index<1)
61 return 0;
62 if (index>=argc)
63 return 0;
64 /* fmi_error("param arg index overflow"); */
65 if (strncmp(key,argv[index],strlen(key))==0)
66 return 1;
67 else
68 return 0;
69}
70
71int file_count(int argc, char **argv){
72 int i=0;
73 while (++i<argc)
74 if (argv[i][0]=='-')
75 if (strcmp(argv[i],"-")!=0)
76 return (i-1);
77 return (i-1);
78}
79
80char *extract_command_line(int argc, char **argv){
81 int i;
82 for (i=0;i<argc;i++)
83 if ((strlen(fmi_util_command_line)+strlen(argv[i]))<FMI_UTIL_COMMAND_LINE_LEN){
84 strcat(fmi_util_command_line,argv[i]);
85 strcat(fmi_util_command_line," ");}
86 return (fmi_util_command_line);
87}
88
89
90void trchr(char *string,char from,char to){
91 char *c;
92 if (string==NULL)
93 return;
94 for (c=string;*c!='\0';c++)
95 if (*c==from) *c=to;
96}
97
98/* DEBUGGING */
99int fmi_debug_level=0;
100
101void fmi_error(char *s){
102 fprintf(stderr,"fmi_error: ");
103 fprintf(stderr,"%s",s);
104 fprintf(stderr,"\n");
105 exit(-1);
106}
107
108void fmi_debug(int n,char *s){
109 int i;
110 if (n<=fmi_debug_level){
111 for (i=0;i<n;i++)
112 fputc(':',stderr);
113 fprintf(stderr," %s\n",s);
114 fflush(stderr);}
115}
116
117char *file_extension(char *filename){
118 return (strrchr(filename,'.'));
119}
120
121char *file_name(char *filename){
122 char *s;
123 s=strrchr(filename,'/');
124 if (s==NULL)
125 return (filename);
126 else
127 return (++s);
128}
129
130char *file_path(char *filename){
131 char *end,c,*path;
132 end=strrchr(filename,'/');
133 if (end==NULL)
134 return NULL;
135 end++;
136 c=*end;
137 *end='\0'; /* cut! (kludge) */
138 path=(char *)RAVE_MALLOC(strlen(filename)+1);
139 strcpy(path,filename);
140 *end=c; /* fix! (kludge) */
141 return path;
142}
143
144char *file_basename(char *filename){
145 char *start,*end,c,*basename;
146 start=strrchr(filename,'/');
147 end=strrchr(filename,'.');
148 if (start==NULL)
149 start=filename;
150 else
151 start++;
152 if (end!=NULL){
153 c=*end;
154 *end='\0';} /* cut! (kludge) */
155 basename=(char *)RAVE_MALLOC(strlen(start)+1);
156 strcpy(basename,start);
157 if (end!=NULL){
158 *end=c;}
159 return basename;
160}
161
162
163
164/* function g=gauss(a,x);
165 g=(1./((x.*x)/(a*a+0.000001)+1));
166*/
167
168/* function m=msigmoid(x,a)
169 m=a*x./(1+abs(a*x));
170*/
171
172/* BELL CURVE in [0,1], ORIGIN=0, BELL=0.5 AT A */
173float pseudo_gauss(float a,float x){
174 return (255.0*a*a/(a*a+x*x));
175}
176
177int pseudo_gauss_int(int a,int x){
178 return (255*a*a/(a*a+x*x));
179}
180 /* return (1.0 / (1+x*x/a/a));} */
181
182/* SIGMOID CURVE in [0,1], ORIGIN=0, A=DERIV AT ORGIN */
183float pseudo_sigmoid(float a,float x){
184 /* float xa=x/a; */
185 if (x>0.0)
186 return ( 255.0*x/(a+x) );
187 else
188 return ( 255.0*x/(a-x) );
189}
190
191int pseudo_sigmoid_int(int a,int x){
192 /* float xa=x/a; */
193 if (x>0)
194 return ( (255*x)/(a+x) );
195 else
196 return ( (255*x)/(a-x) );
197}
198
199/*#define pseudo_sigmoid(a,x) ((x)>0 ? 255*(x)/(a+(x)) : 255*(x)/(a-(x))) */
200/*#define pseudo_gauss(a,x) (255*(a)*(a)/((a)*(a)+(x)*(x))) */
201
202
203
204
205
206
207
208