OT - Consulta sobre CGI en C
| From: | Pizzi Eduardo | Date: | Fri, 23 May 2003 07:47:59 +0000 |
| Subject: | OT - Consulta sobre CGI en C | ||
| Groups: | php.general.es | ||
| Request: | Send a blank email to general-es+get-36532@lists.php.net to get a copy of this message | ||
Antes que nada muchisimas gracias por leer el mail y de ser posible, tu
respuesta.
Arme el CGI en C que muestro mas abajo.
El tema es que quiero armar una funcion que reemplaze estas 3 lineas:
lng=strlen(pTOK); //-- Largo del token
fwrite(&lng, 1,sizeof (int), fp );
fwrite(pTOK,1,lng,fp);
por una simple llamada como esta:
writeRegister( pTOK ); //-- Grabo registro en file.
El tema es que tengo muy poca practiva con C y le estoy pifiando en el
pasaje de las variable y la declaracion en la funcion que quiero armar
y ya no se mas que hacer :-(
Esta es la idea de funcion:
void writeRegister(char *pToken )
{ int len=0;
len=strlen(pToken); //-- Largo del token
fwrite(&len, 1,sizeof (int), fp );
fwrite(pToken,1,len,fp);
}
Este es el CGI completo que quiero retocarL:
(LeeFormPost.C)
#include <stdio.h>
#include <stdlib.h> //-- Para getenv en form action
#include <string.h> //-- Para funcion atoi()
#define BUF_SIZE 4096 // Macro
#define BUF_FILE 1024 // Buffer para archivo
//sin parametros
#define PUT_HDR printf("content-type: text/html\n\n")
// declaracion de encabezados de funsiones
void requestForm(char StringPost[])
{
unsigned char buffer[BUF_SIZE];
char *pSRC; //Declaro puntero para duplicar buffer.
char *pTOK; //Para cada token. Este puntero no se free xq lo hace la
funsion strtok
int len,lng = 0;
FILE *fp; // Tampoco se librera. La unica que se libera es strdup()
porque
// me da memoria dinamicamente y no se hace cargo. Pero todas las
funsiones de libreria
// se hacen cargo de liberarla.
if ((len = atoi(StringPost) ) > 0 )
{
int sz = fread( (void*)buffer, 1, len, stdin );
buffer[sz]='\0';
printf("<hr>%s<hr>", buffer ); //-- Imprime toda
la cadena POST
pSRC = strdup(buffer); //-- Duplico buffer con \0
incluido para trabajar en ella.
// me da memoria dinamicamente y no se hace cargo. Debo
liberarla luego.
//-- Creo file
fp=fopen("frmRFile.txt","wb"); //BINARIO
//-- Parceo buscando & y/o =
//-- Primer Next.
pTOK = strtok(pSRC,"&="); // Token. ptok no se libera
porque se hace cargo la funsion.
while(pTOK) //-- != \0
{
if (stricmp(pTOK,"txtNombre")==0)
{
printf("Variable: \"%s\"", pTOK);
// writeRegister( pTOK ); //-- Grabo registro.
lng=strlen(pTOK); //-- Largo del token
fwrite(&lng, 1,sizeof (int), fp );
fwrite(pTOK,1,lng,fp);
pTOK = strtok(NULL,"&="); //
Next token.
printf("<br>Valor: \"%s\"<hr>", pTOK);
// writeRegister( pTOK ); //-- Grabo registro.
lng=strlen(pTOK); //-- Largo del token
fwrite(&lng, 1,sizeof (int), fp );
fwrite(pTOK,1,lng,fp);
}
if (stricmp(pTOK,"txtApellido")==0)
{
printf("Variable: \"%s\"", pTOK);
// writeRegister( pTOK ); //-- Grabo registro.
lng=strlen(pTOK); //-- Largo del token
fwrite(&lng, 1,sizeof (int), fp );
fwrite(pTOK,1,lng,fp);
pTOK = strtok(NULL,"&="); //
Next token.
printf("<br>Valor: \"%s\"<hr>", pTOK);
// writeRegister( pTOK ); //-- Grabo registro.
lng=strlen(pTOK); //-- Largo del token
fwrite(&lng, 1,sizeof (int), fp );
fwrite(pTOK,1,lng,fp);
}
pTOK = strtok(NULL,"&="); // Queda en
la ultima posicion y avanza.
} // while
//-- Me dio memoria dinamicamente... Debo liberarla.
free(pSRC);
fclose(fp);
} // if
}
/*
void writeRegister(char *pToken )
{ int len=0;
len=strlen(pToken); //-- Largo del token
fwrite(&len, 1,sizeof (int), fp );
fwrite(pToken,1,len,fp);
}
*/
int main(int argc, char *argv[] )
{
PUT_HDR;
printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01
Transitional//EN\">");
printf("<html><head><title>Formulario CGI</title>");
printf("<meta http-equiv=\"\" content=\"text/html; charset=iso-8859-1
\">");
printf("</head>");
printf("<body>");
requestForm( getenv("CONTENT_LENGTH") ); //-- Si POST imprime
contenido.
printf("<form name=\"form1\" method=\"post\"
action=\"%s\">",getenv
("SCRIPT_NAME")); //-- Nombre del CGI. AutoCall
printf("<center>");
printf(" <h1>Formulario de ingreso</h1>");
printf(" <table width=\"39%\" border=\"1\"
cellpadding=\"0\"
cellspacing=\"0\">");
printf(" <tr>");
printf(" <td width=\"34%\">Nombre</td>");
printf(" <td width=\"66%\"><input type=\"text\"
name=\"txtNombre\"></td>");
printf(" </tr>");
printf(" <tr>");
printf(" <td>Apellido</td>");
printf(" <td><input type=\"text\"
name=\"txtApellido\"></td>");
printf(" </tr>");
printf(" </table>");
printf(" <br><input type=\"submit\"
value=\"Enviar\">");
printf("</center>");
printf("</form></body></html>");
return 0;
}
!-- Eduardo Daniel Pizzi www.binco.com.ar edpizzi@binco.com.ar
NeoMail - Webmail that doesn't suck... as much.
http://neomail.sourceforge.net