📄 html.c
字号:
quotedValue = FALSE; } /* mark the beginning of the value */ if(*plainPtr != NUL) { value = plainPtr; } /* find the end of the value */ while(*plainPtr != NUL) { if(isspace(*plainPtr)) { if(quotedValue == FALSE) { break; } } if((*plainPtr == '\"') && (quotedValue)) { break; } plainPtr++; } /* mark the end of the value */ if(*plainPtr != NUL) { *plainPtr = NUL; plainPtr++; } } } } /* add the new attribute to the markup structure */ if(AddAttributeToMarkup(htmlMarkup, token, value, quotedValue) == FALSE) { DEBUG_PRINT(("unable to add attribute to markup")); DestroyMarkupStruct(htmlMarkup); FreeMemory(plainBuffer); return FALSE; } } } FreeMemory(plainBuffer); return TRUE;} BOOL AddAttributeToMarkup(HTML_MARKUP *htmlMarkup, const char *name, const char *value, BOOL quotedValue){ assert(htmlMarkup != NULL); assert(name != NULL); if(htmlMarkup->attribCount < MAX_ATTRIBUTE_COUNT) { if(MakeAttribute(&htmlMarkup->attrib[htmlMarkup->attribCount], name, value, quotedValue) == TRUE) { htmlMarkup->attribCount++; return TRUE; } else { DEBUG_PRINT(("unable to make attribute name=\"%s\" value=\"%s\"", name, value)); } } else { DEBUG_PRINT(("markup structure full! tag=\"%s\" name=\"%s\" value=\"%s\"", htmlMarkup->tag, name, value)); } return FALSE;} void DestroyMarkupStruct(HTML_MARKUP *htmlMarkup){ uint ctr; assert(htmlMarkup != NULL); /* destroy the tag */ /* do not assert against this, as this function might be used to */ /* destroy a partially-built structure */ if(htmlMarkup->tag != NULL) { FreeMemory(htmlMarkup->tag); htmlMarkup->tag = NULL; } /* destroy all markup attributes */ for(ctr = 0; ctr < htmlMarkup->attribCount; ctr++) { DestroyAttribute(&htmlMarkup->attrib[ctr]); }} BOOL IsMarkupTag(HTML_MARKUP *htmlMarkup, const char *tag){ assert(htmlMarkup != NULL); assert(tag != NULL); return (stricmp(htmlMarkup->tag, tag) == 0) ? TRUE : FALSE;} static uint MarkupAttributeIndex(HTML_MARKUP *htmlMarkup, const char *name){ uint ctr; assert(htmlMarkup != NULL); assert(name != NULL); for(ctr = 0; ctr < htmlMarkup->attribCount; ctr++) { assert(htmlMarkup->attrib[ctr].name != NULL); if(stricmp(htmlMarkup->attrib[ctr].name, name) == 0) { return ctr; } } return ERROR;} BOOL MarkupToPlaintext(HTML_MARKUP *htmlMarkup, char **plaintext){ uint ctr; HTML_ATTRIBUTE *htmlAttribute; uint size; uint attrSize; uint maxAttrSize; char *buffer; char *attrBuffer; assert(htmlMarkup != NULL); assert(htmlMarkup->tag != NULL); assert(plaintext != NULL); /* estimate the required size of the plaintext buffer */ maxAttrSize = 0; attrBuffer = NULL; /* additional byte is to account for NUL */ size = strlen(htmlMarkup->tag) + 1; for(ctr = 0; ctr < htmlMarkup->attribCount; ctr++) { htmlAttribute = &htmlMarkup->attrib[ctr]; assert(htmlAttribute != NULL); assert(htmlAttribute->name != NULL); /* an additional byte is added to size for preceding space char */ attrSize = strlen(htmlAttribute->name) + 1; if(htmlAttribute->value != NULL) { /* additional byte added to account for equal sign */ attrSize += strlen(htmlAttribute->value) + 1; if(htmlAttribute->quoted) { /* account for the quote characters */ attrSize += 2; } } /* additional byte added for NULL character */ attrSize++; size += attrSize; if(maxAttrSize < attrSize) { maxAttrSize = attrSize; } } if((buffer = AllocMemory(size)) == NULL) { DEBUG_PRINT(("unable to allocate plaintext buffer (%u bytes)", size)); return FALSE; } if(maxAttrSize != 0) { if((attrBuffer = AllocMemory(maxAttrSize)) == NULL) { DEBUG_PRINT(("unable to allocate attribute plaintext buffer (%u bytes)", maxAttrSize)); FreeMemory(buffer); return FALSE; } } else { attrBuffer = NULL; } /* start copying in the markup as plaintext */ strcpy(buffer, htmlMarkup->tag); for(ctr = 0; ctr < htmlMarkup->attribCount; ctr++) { htmlAttribute = &htmlMarkup->attrib[ctr]; /* checked previously, but check again */ assert(htmlAttribute != NULL); assert(htmlAttribute->name != NULL); /* this had best be true */ assert(attrBuffer != NULL); /* its a little ugly, but is much quicker than prior tactic */ /* (an equally ugly set of strcat() functions used conditionally) */ if(htmlAttribute->value == NULL) { sprintf(attrBuffer, " %s", htmlAttribute->name); } else { if(htmlAttribute->quoted == FALSE) { sprintf(attrBuffer, " %s=%s", htmlAttribute->name, htmlAttribute->value); } else { sprintf(attrBuffer, " %s=\"%s\"", htmlAttribute->name, htmlAttribute->value); } } /* copy in the attribute plaintext into the markup plaintext */ strcat(buffer, attrBuffer); } /* free the attribute buffer */ FreeMemory(attrBuffer); /* give the buffer to caller */ *plaintext = buffer; return TRUE;} BOOL IsAttributeInMarkup(HTML_MARKUP *htmlMarkup, const char *name){ assert(htmlMarkup != NULL); return (MarkupAttributeIndex(htmlMarkup, name) != ERROR) ? TRUE : FALSE;} const char *MarkupAttributeValue(HTML_MARKUP *htmlMarkup, const char *name){ uint index; assert(htmlMarkup != NULL); if((index = MarkupAttributeIndex(htmlMarkup, name)) == ERROR) { return NULL; } /* check validity of attribute */ assert(htmlMarkup->attrib[index].name != NULL); return htmlMarkup->attrib[index].value;} BOOL ChangeMarkupTag(HTML_MARKUP *htmlMarkup, const char *tag){ assert(htmlMarkup != NULL); assert(tag != NULL); if(htmlMarkup->tag != NULL) { FreeMemory(htmlMarkup->tag); } htmlMarkup->tag = DuplicateString(tag); return (htmlMarkup->tag != NULL) ? TRUE : FALSE;}const HTML_ATTRIBUTE *MarkupAttribute(HTML_MARKUP *htmlMarkup, uint index){ assert(htmlMarkup != NULL); if(index >= htmlMarkup->attribCount) { return NULL; } assert(htmlMarkup->attribCount < MAX_ATTRIBUTE_COUNT); return &htmlMarkup->attrib[index];}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -