Main Page   Compound List   File List   Header Files   Sources   Compound Members   File Members  

smpcgi.c

00001 
00020 
00021 #include "smpcgi.h"
00022 #include <stdlib.h>
00023 
00024 static FormItem far* tags[100];
00025 static int allTags = 0;
00026 static int cgientrance = 0; /* got a value greater than 0 if CGIEntrance was called */
00027 static nextTagCounter = 0;
00028 
00029 int CGIEntrance(rpCgiPtr CgiRequest)
00030 {
00031     unsigned int nameLength; /* space to alloc for the name array of a tag */
00032     long valueLength; /* space to alloc for the value array of a tag */
00033     int moreTags;
00034 
00035     char far     * nextBuffer   = NULL;
00036     char far     * FormNamePtr  = NULL;
00037     char far     * FormValuePtr = NULL;
00038     FormItem far * formitem     = NULL;
00039 
00040     if (cgientrance) { 
00041     /* there was a call of CGIEntrance without a CGIExit in the past
00042      * So we try to avoid that more and more mem is allocated
00043      */
00044         return 0;
00045     }
00046 
00047     cgientrance = 1; /* so we mark that CGIEntrance was called */
00048     allTags = 0;
00049     moreTags = 0;
00050     nameLength = 40;
00051     valueLength = strlen(CgiRequest->fArgumentBufferPtr);
00052 
00053     if (valueLength) { /* we have args */
00054         moreTags = 1;
00055         FormNamePtr=(char far *)malloc(sizeof(char )*nameLength);
00056         FormValuePtr=(char far *)malloc(sizeof(char )*valueLength);
00057         formitem=(FormItem far *)malloc(sizeof(FormItem));
00058         
00059         if((FormNamePtr!=NULL) && (FormValuePtr!=NULL) && (formitem!=NULL)) {
00060             formitem->NamePtr  = FormNamePtr;
00061             formitem->ValuePtr = FormValuePtr;
00062 
00063             CGIGetFirstFormItem(CgiRequest->fArgumentBufferPtr, formitem);
00064             
00065             tags[allTags] = formitem; /* first tag into the tag array */
00066             allTags++;
00067 
00068             nextBuffer = CgiRequest->fArgumentBufferPtr; 
00069             
00070             while (moreTags) {
00071                 FormNamePtr=(char far *)malloc(sizeof(char )*nameLength);
00072                 FormValuePtr=(char far *)malloc(sizeof(char )*valueLength);
00073                 formitem=(FormItem far *)malloc(sizeof(FormItem));
00074 
00075                 if((FormNamePtr!=NULL) && (FormValuePtr!=NULL) && (formitem!=NULL)) {
00076                     formitem->NamePtr  = FormNamePtr;
00077                     formitem->ValuePtr = FormValuePtr;
00078                     
00079                     nextBuffer = CGIGetNextFormItem(nextBuffer, &formitem);
00080                     if (formitem != NULL) { /* there is a next tag */
00081                         tags[allTags] = formitem;
00082                         allTags++;
00083                     } else { /* there's no next tag - must freeing the last malloc's */
00084                         free(FormNamePtr);
00085                         free(FormValuePtr);
00086                         free(formitem);
00087                         moreTags = 0;
00088                     }
00089                 } else {
00090                     return 0; /* there was an error with malloc */
00091                 }
00092             }
00093         } else {
00094           return 0; /* there was an error with malloc */
00095         }
00096     }
00097     return 1; /* all's gone fine */
00098 }
00099 
00100 void CGIExit()
00101 {
00102     int i;
00103 
00104     for(i = 0; i < allTags; i++) { /* clean the mem */
00105         free(tags[i]->NamePtr);
00106         free(tags[i]->ValuePtr );
00107         free(tags[i]);
00108     }
00109 
00110     cgientrance = 0;
00111     nextTagCounter = 0;
00112     allTags = 0;
00113 }
00114 
00115 int size() 
00116 {
00117     return allTags;
00118 }
00119 
00120 FormItem far* getFirstTag()
00121 {
00122     if ( allTags > 0 ) {
00123         if ( nextTagCounter == 0 ) { /* after first call of getFirstTag - getnextTag returns the second element from tag[..] */
00124             nextTagCounter = 1;            
00125         }
00126         return tags[0];
00127     } else {
00128         return NULL;
00129     }
00130 }
00131 
00132 FormItem far* getNextTag()
00133 {
00134     FormItem far* tempFi;
00135 
00136     if ( allTags > 0 ) {
00137         if ( !(nextTagCounter < allTags) ) { /* there must be an end */
00138             return NULL;
00139         }
00140         if ( nextTagCounter > 0 ) { /* normal work */
00141             tempFi = tags[nextTagCounter];
00142             nextTagCounter++;
00143             return tempFi;
00144         } else { /* here you call getNextTag first and not getFirstTag */
00145             return getFirstTag();
00146         }
00147     } else {
00148         return NULL;
00149     }
00150 }
00151 
00152 FormItem far* getTagAt(int tag)
00153 {
00154     if ( (tag >= 0) && (tag < allTags) ) {
00155         return tags[tag];
00156     } else {
00157         return NULL;
00158     }
00159 }

Generated at Fri Jun 9 21:00:19 2000 for CGIAPI by doxygen 1.0.0 written by Dimitri van Heesch, © 1997-1999