/* fr.cpp L-C-F Resonate frequency calculator [Linux CGI version] by Dale Heatherington Sept 6 2000 This version is designed as a CGI program to allow it to run from a HTTP server where it can be accessed by Web Browsers. It outputs HTML formated text. Input is from the Browsers FORM. Note: The input to this program comes from environment variable "QUERRY_STRING" which is supplied by the www server. */ #include #include #include #include #include #include #include #define HZ 1 #define KHZ 1000 #define MHZ 1000000 #define PF 1e-12 #define NF 1e-9 #define UF 1e-6 #define UH 1e-6 #define MH 1e-3 #define FREQ 1 #define IND 2 #define CAP 3 //-------------- // These globals supply the program input data double funits, cunits, lunits; double ind, cap, freq; int taskID; char ch; char *sz_funits; char *sz_cunits; char *sz_lunits; const double PI=3.141592; //------------------------------------------------------------- double normalize(double val, double scale) { return val * scale; } //------------------------------------------------------------- double calcFreq(double C, double L) { double f = 1 / (2*PI*sqrt(L*C)); return f; } //---------------------------------------------------------------- double calcInd(double F, double C) { double l = 1 / (4*PI*PI*F*F*C); return l; } //--------------------------------------------------------------- double calcCap(double F, double L) { double c = 1 / (4*PI*PI*F*F*L); return c; } //------------------------------------------------------------ //query_string format : name=value&name2=value2&name3=value3&.....name10=value10 void ProcQuery(char *cp) { char* arg; char* string; char* name; float val=0.0; char *s = strtok(cp,"&"); //s = name,value pair into s while(s != NULL) { name = s; arg = strchr(s,'='); *arg = (char)NULL; arg++; string = arg; //cout << name << " " << string << "
\n"; sscanf(arg,"%f",&val); if (strcmp(name,"funits") == 0){ if(strcmp(string,"Hz" ) == 0) funits = HZ; if(strcmp(string,"kHz") == 0) funits = KHZ; if(strcmp(string,"mHz") == 0) funits = MHZ; sz_funits = strdup(string); } if (strcmp(name,"cunits") == 0){ if(strcmp(string,"pF" ) == 0) cunits = PF; if(strcmp(string,"nF") == 0) cunits = NF; if(strcmp(string,"uF") == 0) cunits = UF; sz_cunits = strdup(string); } if (strcmp(name,"lunits") == 0){ if(strcmp(string,"uH" ) == 0) lunits = UH; if(strcmp(string,"mH") == 0) lunits = MH; if(strcmp(string,"H") == 0) lunits = 1; sz_lunits = strdup(string); } if (strcmp(name,"CALC") == 0){ if(strcmp(string,"Resonant+Frequency" ) == 0) taskID = FREQ; if(strcmp(string,"Inductance") == 0) taskID = IND; if(strcmp(string,"Capacitance") == 0) taskID = CAP; } if (strcmp(name,"C") == 0) cap = (double)val; // Capacitance if (strcmp(name,"L") == 0) ind = (double)val; // Inductance if (strcmp(name,"FR") == 0) freq = (double)val; // Frequency s = strtok(NULL,"&"); //get next name,value pair } } //------------------------------------------------------------- //This is a http CGI program. Input is from the environment variable QUERY_STRING. int main(int argc, char *argv[]) { char *cp; cap = ind = freq = 0.0; char *query_string = getenv("QUERY_STRING"); if (query_string == NULL) query_string = ""; cout << "Content-Type: text/html" << endl << endl << "" << ""; ProcQuery(query_string); //Parse the query string switch(taskID){ case FREQ: freq = calcFreq(normalize(cap,cunits), normalize(ind,lunits)) / funits; break; case IND: ind = calcInd(normalize(freq,funits), normalize(cap,cunits)) / lunits ; break; case CAP: cap = calcCap(normalize(freq,funits), normalize(ind,lunits)) / cunits ; break; default: cout << "fr: CGI ERROR " ; } if(taskID > 0){ cout << "" << "" << "" << "" << "" << "
Results
Capacitance" << cap << " " << sz_cunits << "
Inductance" << ind << " " << sz_lunits << "
Frequency" << freq << " " << sz_funits << "
"; } return 0; }