char *QuoteCharsInStr(char *Buffer, const char *String, const char *QuoteChars) { char *RetStr=NULL; const char *sptr, *cptr; size_t olen=0; RetStr=CopyStr(Buffer,""); if (! String) return(RetStr); for (sptr=String; *sptr !='\0'; sptr++) { for (cptr=QuoteChars; *cptr !='\0'; cptr++) { if (*sptr==*cptr) { RetStr=AddCharToBuffer(RetStr,olen, '\\'); olen++; break; } } RetStr=AddCharToBuffer(RetStr,olen,*sptr); olen++; } return(RetStr); }
char *HTTPQuoteChars(char *RetBuff, char *Str, char *CharList) { char *RetStr=NULL, *Token=NULL, *ptr; int olen=0, ilen; RetStr=CopyStr(RetStr,""); ilen=StrLen(Str); for (ptr=Str; ptr < (Str+ilen); ptr++) { if (strchr(CharList,*ptr)) { Token=FormatStr(Token,"%%%02X",*ptr); RetStr=CatStr(RetStr,Token); olen+=StrLen(Token); } else { RetStr=AddCharToBuffer(RetStr,olen,*ptr); olen++; } } RetStr[olen]='\0'; DestroyString(Token); return(RetStr); }
char *PadStr(char*Dest, char Pad, int PadLen) { char *NewStr=NULL; int i, len, abslen; if (PadLen==0) return(Dest); if (PadLen < 0) abslen=0-PadLen; else abslen=PadLen; for (i=0; i < abslen; i++) NewStr=AddCharToBuffer(NewStr,i,Pad); if (PadLen > 0) { Dest=(CatStr(Dest, NewStr)); DestroyString(NewStr); } //Negative values mean pad in front of text else { NewStr=CatStr(NewStr,Dest); //NewStr is the replacement for Dest, so we destroy Dest DestroyString(Dest); Dest=NewStr; } return(Dest); }
char *HTTPUnQuote(char *RetBuff, char *Str) { char *RetStr=NULL, *Token=NULL, *ptr; int olen=0, ilen; RetStr=CopyStr(RetStr,""); ilen=StrLen(Str); for (ptr=Str; ptr < (Str+ilen); ptr++) { switch (*ptr) { case '+': RetStr=AddCharToBuffer(RetStr,olen,' '); olen++; break; case '%': ptr++; Token=CopyStrLen(Token,ptr,2); ptr++; //not +=2, as we will increment again RetStr=AddCharToBuffer(RetStr,olen,strtol(Token,NULL,16) & 0xFF); olen++; break; default: RetStr=AddCharToBuffer(RetStr,olen,*ptr); olen++; break; } } DestroyString(Token); return(RetStr); }
void NetworkCommandBuffer::Think() { SDLNet_SocketSet set; set = SDLNet_AllocSocketSet ( 1 ); if ( !set ) { RaiseSocketError ( "SDLNet_AllocSocketSet" ); } SocketSetEncapsulator free_on_quit ( set ); if ( SDLNet_TCP_AddSocket ( set, socket ) < 0 ) { RaiseSocketError ( "SDLNet_TCP_AddSocket" ); } int numready = SDLNet_CheckSockets ( set, 0 ); for ( ; numready == 1; numready = SDLNet_CheckSockets ( set, 0 ) ) { char msg; int nbread = SDLNet_TCP_Recv ( socket, &msg, 1 ); if ( nbread < 0 ) { RaiseSocketError ( "SDLNet_TCP_Recv: " ); } else if ( nbread == 0 ) { std::cout << "SDLNet_TCP_Recv: Unexpected read of size 0\n"; throw ReadSocketError(); } if ( debug_all_message ) { if ( msg < 32 ) std::cout << "Read: 0x" << ( int ) msg << "\n"; else std::cout << "Read: " << msg << "\n"; } if (current_command_size > 0) AddCharToBuffer ( msg ); else AddCharToCommandSize( msg ); } if ( numready < 0 ) { std::cout << "SDLNet_CheckSockets: " << SDLNet_GetError() << "\n"; perror ( "SDLNet_CheckSockets" ); throw ReadSocketError(); } }
char *TerminalReadText(char *RetStr, int Flags, STREAM *S) { int inchar, len=0; char outchar; inchar=STREAMReadChar(S); while (inchar != EOF) { if (Flags & TERM_SHOWTEXT) outchar=inchar & 0xFF; if (Flags & TERM_SHOWSTARS) { if ((outchar & 0xFF) =='\n') outchar=inchar & 0xFF; else if ((outchar & 0xFF) =='\b') outchar=inchar & 0xFF; else outchar='*'; } if (Flags & TERM_SHOWTEXTSTARS) { switch (inchar) { case '\n': case '\r': case 0x08: break; default: if (len > 0) STREAMWriteString("\x08*",S); break; } outchar=inchar & 0xFF; } STREAMWriteBytes(S, &outchar,1); STREAMFlush(S); if (inchar == '\n') break; if (inchar == '\r') break; RetStr=AddCharToBuffer(RetStr,len++, inchar & 0xFF); inchar=STREAMReadChar(S); } return(RetStr); }
char *EnquoteStr(char *Out, const char *In) { const char *ptr; char *ReturnStr=NULL; size_t len=0; ptr=In; ReturnStr=CopyStr(Out,""); while (ptr && (*ptr != '\0')) { switch (*ptr) { case '\\': case '"': case '\'': ReturnStr=AddCharToBuffer(ReturnStr,len,'\\'); len++; ReturnStr=AddCharToBuffer(ReturnStr,len, *ptr); len++; break; case '\r': ReturnStr=AddCharToBuffer(ReturnStr,len,'\\'); len++; ReturnStr=AddCharToBuffer(ReturnStr,len, 'r'); len++; break; case '\n': ReturnStr=AddCharToBuffer(ReturnStr,len,'\\'); len++; ReturnStr=AddCharToBuffer(ReturnStr,len, 'n'); len++; break; default: ReturnStr=AddCharToBuffer(ReturnStr,len, *ptr); len++; break; } ptr++; } return(ReturnStr); }
char *STREAMReadToMultiTerminator(char *Buffer, STREAM *S, char *Terms) { int inchar, len=0; char *Tempptr; Tempptr=CopyStr(Buffer,""); inchar=STREAMReadChar(S); //All error conditions are negative, but '0' can just mean //no more data to read while (inchar > -1) { //if ((len % 100)== 0) Tempptr=realloc(Tempptr,(len/100 +1) *100 +2); //*(Tempptr+len)=inchar; if (inchar > 0) { Tempptr=AddCharToBuffer(Tempptr,len,(char) inchar); len++; if (strchr(Terms,inchar)) break; } inchar=STREAMReadChar(S); } *(Tempptr+len)='\0'; //if ((inchar==EOF) && (errno==ECONNREFUSED)) return(Tempptr); if ( ((inchar==EOF) || (inchar==STREAM_DATA_ERROR)) && (StrLen(Tempptr)==0) ) { free(Tempptr); return(NULL); } return(Tempptr); }
char *FILEReadLine(char *RetStr, FILE *f, char Term) { int inchar, len=0; RetStr=CopyStr(RetStr,""); inchar=fgetc(f); while ((inchar != Term) && (inchar != EOF)) { RetStr=AddCharToBuffer(RetStr,len++,inchar & 0xFF); inchar=fgetc(f); } if ((inchar==EOF) && (StrLen(RetStr)==0)) { Destroy(RetStr); return(NULL); } return(RetStr); }
void ExtractVarsReadVar(const char **Fmt, const char **Msg, ListNode *Vars) { const char *FmtPtr, *MsgPtr; char *VarName=NULL; int len=0; ListNode *Node; FmtPtr=*Fmt; if (*FmtPtr=='(') FmtPtr++; while (*FmtPtr != ')') { VarName=AddCharToBuffer(VarName,len,*FmtPtr); len++; FmtPtr++; } if (*FmtPtr==')') FmtPtr++; MsgPtr=*Msg; while ((*MsgPtr !=0) && (*MsgPtr != *FmtPtr)) { if (*MsgPtr=='"') { do { MsgPtr++; } while ((*MsgPtr != '"') && (*MsgPtr != 0)); } MsgPtr++; } Node=ListFindNamedItem(Vars,VarName); if (Node) Node->Item=(void *) CopyStrLen((char *) Node->Item, *Msg, MsgPtr-*Msg); else Node=ListAddNamedItem(Vars,VarName,CopyStrLen(NULL, *Msg, MsgPtr-*Msg)); *Fmt=FmtPtr; *Msg=MsgPtr; DestroyString(VarName); }
char *STREAMReadToTerminator(char *Buffer, STREAM *S,unsigned char Term) { int inchar, len=0; char *Tempptr; Tempptr=CopyStr(Buffer,""); inchar=STREAMReadChar(S); while (inchar != EOF) { //if ((len % 100)== 0) Tempptr=realloc(Tempptr,(len/100 +1) *100 +2); //*(Tempptr+len)=inchar; if (inchar >= 0) { Tempptr=AddCharToBuffer(Tempptr,len,(char) inchar); len++; } else if (inchar !=STREAM_NODATA) break; if (inchar==Term) break; inchar=STREAMReadChar(S); } *(Tempptr+len)='\0'; //if ((inchar==EOF) && (errno==ECONNREFUSED)) return(Tempptr); if ( ((inchar==EOF) || (inchar==STREAM_DATA_ERROR)) && (StrLen(Tempptr)==0) ) { free(Tempptr); return(NULL); } return(Tempptr); }
char *XMLGetTag(char *Input, char **Namespace, char **TagType, char **TagData) { char *ptr, *tptr; int len=0, InTag=FALSE, TagHasName=FALSE; if (! Input) return(NULL); if (*Input=='\0') return(NULL); ptr=Input; //This ensures we are never dealing with nulls if (! *TagType) *TagType=CopyStr(*TagType,""); if (! *TagData) *TagData=CopyStr(*TagData,""); if (*ptr=='<') { ptr++; while (isspace(*ptr)) ptr++; len=0; InTag=TRUE; TagHasName=TRUE; //if we start with a slash tag, then add that to the tag name if (*ptr=='/') { *TagType=AddCharToBuffer(*TagType,len,*ptr); len++; ptr++; } while (InTag) { switch (*ptr) { //These all cause us to end. NONE OF THEM REQUIRE ptr++ //ptr++ will happen when we read tag data case '>': case '\0': case ' ': case ' ': case '\n': InTag=FALSE; break; //If a namespace return value is supplied, break the name up into namespace and //tag. Otherwise don't case ':': if (Namespace) { tptr=*TagType; if (*tptr=='/') { tptr++; len=1; } else len=0; *Namespace=CopyStr(*Namespace,tptr); } else { *TagType=AddCharToBuffer(*TagType,len,*ptr); len++; } ptr++; break; case '\r': ptr++; break; default: *TagType=AddCharToBuffer(*TagType,len,*ptr); len++; ptr++; break; } } } //End of Parse TagName. Strip any '/' tptr=*TagType; if ((len > 0) && (tptr[len-1]=='/')) tptr[len-1]='\0'; tptr[len]='\0'; while (isspace(*ptr)) ptr++; len=0; InTag=TRUE; while (InTag) { switch (*ptr) { //End of tag, skip '>' and fall through case '>': ptr++; //Start of next tag or end of text case '<': case '\0': InTag=FALSE; break; //Quotes! case '\'': case '\"': //Somewhat ugly code. If we're dealing with an actual tag, then TagHasName //will be set. This means we're dealing with data within a tag and quotes mean something. //Otherwise we are dealing with text outside of tags and we just add the char to //TagData and continue if (TagHasName) { tptr=ptr; while (*tptr != *ptr) { *TagData=AddCharToBuffer(*TagData,len,*ptr); len++; ptr++; } } *TagData=AddCharToBuffer(*TagData,len,*ptr); len++; ptr++; break; default: *TagData=AddCharToBuffer(*TagData,len,*ptr); len++; ptr++; break; } } //End of Parse TagData. Strip any '/' tptr=*TagData; if ((len > 0) && (tptr[len-1]=='/')) tptr[len-1]='\0'; tptr[len]='\0'; strlwr(*TagType); while (isspace(*ptr)) ptr++; return(ptr); }
char *HtmlUnQuote(char *RetStr, char *Data) { char *Output=NULL, *Token=NULL, *ptr; int len=0; Output=CopyStr(RetStr,Output); ptr=Data; while (ptr && (*ptr != '\0')) { if (*ptr=='&') { ptr++; ptr=GetToken(ptr,";",&Token,0); if (*Token=='#') { Output=AddCharToBuffer(Output,len,strtol(Token+1,NULL,16)); len++; } else if (strcmp(Token,"amp")==0) { Output=AddCharToBuffer(Output,len,'&'); len++; } else if (strcmp(Token,"lt")==0) { Output=AddCharToBuffer(Output,len,'<'); len++; } else if (strcmp(Token,"gt")==0) { Output=AddCharToBuffer(Output,len,'>'); len++; } else if (strcmp(Token,"quot")==0) { Output=AddCharToBuffer(Output,len,'"'); len++; } else if (strcmp(Token,"apos")==0) { Output=AddCharToBuffer(Output,len,'\''); len++; } else if (strcmp(Token,"tilde")==0) { Output=AddCharToBuffer(Output,len,'~'); len++; } else if (strcmp(Token,"circ")==0) { Output=AddCharToBuffer(Output,len,'^'); len++; } } else { Output=AddCharToBuffer(Output,len,*ptr); len++; ptr++; } } DestroyString(Token); return(Output); }
char *DeQuoteStr(char *Buffer, const char *Line) { char *out, *in; size_t olen=0; char hex[3]; if (Line==NULL) return(NULL); out=CopyStr(Buffer,""); in=(char *) Line; while(in && (*in != '\0') ) { if (*in=='\\') { in++; switch (*in) { case 'e': out=AddCharToBuffer(out,olen,ESC); olen++; break; case 'n': out=AddCharToBuffer(out,olen,'\n'); olen++; break; case 'r': out=AddCharToBuffer(out,olen,'\r'); olen++; break; case 't': out=AddCharToBuffer(out,olen,'\t'); olen++; break; case 'x': in++; hex[0]=*in; in++; hex[1]=*in; hex[2]='\0'; out=AddCharToBuffer(out,olen,strtol(hex,NULL,16) & 0xFF); olen++; break; case '\\': default: out=AddCharToBuffer(out,olen,*in); olen++; break; } } else { out=AddCharToBuffer(out,olen,*in); olen++; } in++; } return(out); }