topical media & game development

talk show tell print

lib-ch-example-sample-calculator-cgi.cgi / cgi



  #!/bin/ch
  /* Copyright (c) 2001 by SoftIntegration, Inc. All Rights Reserved */
  /* Web Calculator source code written in Ch */
  
  include <cgi.h>
  include <stdio.h>
  include <stdlib.h>
  
  void errorHandler(char *reason) {
      class CResponse Response;
  
      Response.begin();
      Response.title("Web Calculator");
  
      fprintf stdout << ENDFILE
        <H3>Web Calculator Failed</H3>
        Your mathematical expression has not been submitted to Web Calculator
         because reason.
         <A HREF="/chhtml/toolkit/demos/cgi/sample/calculator_cgi.html">Try again.</A>
         <p>
         
<A HREF="http://www.softintegration.com" target="_top"> <img src="/chhtml/images/poweredbych.gif" alt="Powered by Ch"></A> ENDFILE Response.end(); exit(0); } void calculate_it(char *xx, char *yy, char *exprr) { class CResponse Response; double x, y, expr; int status; FILE *stream; char name[L_tmpnam], fullname[512]; string_t message; /* x = streval(xx); y = streval(yy); */ x = strtod(xx, NULL); y = strtod(yy, NULL); /* capture any error message from strparse() */ tmpnam(name); if defined(_WIN32_) if(getenv("TMP")) { strcpy(fullname, getenv("TMP")); strcat(fullname, name); } else strcpy(fullname, name); else strcpy(fullname, name); endif stream = freopen(fullname, "w", stderr); /* rediect stderr to fullname */ if(stream==NULL) errorHandler(stradd("cannot redirect stderr stream to file ", fullname)); status = strparse(exprr); fclose(stream); if(status) { message = `cat fullname`; remove(fullname); errorHandler(stradd("invalid value for expression.<br> ", message)); } else { remove(fullname); expr = streval(exprr); } Response.begin(); Response.title("Web Calculator"); printf("x = \%s\n<br>", xx); printf("y = \%s\n<br>", yy); printf("\%s = \%f\n<p>", exprr, expr); Response.end(); } int main() { class CRequest Request; int num; chchar *x, *y, *expr; x = Request.getForm("x"); if(!x) errorHandler("you didn't input x value"); else if(!isnum(x)) errorHandler("x is not a valid number"); y = Request.getForm("y"); if(!y) errorHandler("you didn't input y value"); else if(!isnum(y)) errorHandler("y is not a valid number"); expr = Request.getForm("expr"); if(!expr) errorHandler ("you didn't input mathematical expression"); calculate_it(x, y, expr); }


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.