生活随笔
收集整理的這篇文章主要介紹了
使用katana-parser解析css文件
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
katana-parser的安裝參考github官網(wǎng)。
下面是一個解析css的demo:
#include <stdio.h>
#include <stdlib.h>
#include <sys/timeb.h>
#include "katana.h"/*
g++ css-parser.cpp `pkg-config --cflags --libs katana`
./a.out test.csseg : test.css:hr {color: sienna;}p {margin-left: 20px;color:red;}body {background-image: url("images/back40.gif");}div {color:black;}
*/#define StopWatchBegin(begin) \struct timeb t1; \ftime(&t1);#define StopWatchEnd(begin) \struct timeb t2; \ftime(&t2); \printf("<" #begin "> costs %dms.\n", (t2.millitm - t1.millitm));void apply_rule(KatanaArray *Properties)
{for (long i = 0; i < Properties->length; i++){auto prop = (KatanaDeclaration *)Properties->data[i];printf("Set property \"%s\" with %d values\n", prop->property, prop->values->length);for (long v = 0; v < prop->values->length; v++){auto value = (KatanaValue *)prop->values->data[v];// printf("unit is %d\n", value->unit);switch (value->unit){case KATANA_VALUE_URI:{printf("properties value is : %s\n", value->string);break;}case KATANA_VALUE_IDENT:{printf("properties value is : %s\n", value->string);break;}}}}
}
void parse_rule(KatanaRule *Rule)
{if (Rule == NULL){exit;}switch (Rule->type){case KatanaRuleStyle:{auto sr = (KatanaStyleRule *)Rule;for (long i = 0; i < sr->selectors->length; ++i){auto sel = (KatanaSelector *)sr->selectors->data[i];switch (sel->match){case KatanaSelectorMatchTag:printf("Processing selector is: %s\n", (sel->tag) ? sel->tag->local : "UNNAMED");apply_rule(sr->declarations);break;case KatanaSelectorMatchId:break;case KatanaSelectorMatchClass:printf("Processing class selector: %s\n", (sel->data) ? sel->data->value : "UNNAMED");break;case KatanaSelectorMatchPseudoClass: // E.g. a:linkbreak;case KatanaSelectorMatchPseudoElement:break;case KatanaSelectorMatchPagePseudoClass:break;case KatanaSelectorMatchAttributeExact:break;case KatanaSelectorMatchAttributeSet:break;case KatanaSelectorMatchAttributeList:break;case KatanaSelectorMatchAttributeHyphen:break;case KatanaSelectorMatchAttributeContain:break;case KatanaSelectorMatchAttributeBegin:break;case KatanaSelectorMatchAttributeEnd:break;case KatanaSelectorMatchUnknown:break;}}}}
}void dump_stylesheet(const char *filename)
{FILE *fp = fopen(filename, "r");if (!fp){printf("File %s not found!\n", filename);exit(0);}StopWatchBegin(KatanaParseFile);KatanaOutput *output = katana_parse_in(fp);StopWatchEnd(KatanaParseFile);KatanaStylesheet *sheet = output->stylesheet;printf("sheet->imports:%d\n", sheet->imports.length);printf("sheet->rules:%d\n", sheet->rules.length);printf("======================\n");for (long i = 0; i < sheet->rules.length; ++i){if (sheet->rules.data[i]){KatanaRule *item = (KatanaRule *)sheet->rules.data[i];printf("name : %s\n", item->name);parse_rule(item);printf("======================\n");}}// katana_dump_output(output);katana_destroy_output(output);
}int main(int argc, const char *argv[])
{if (argc != 2){printf("Usage: css_dump <CSS filename>.\n");exit(0);}dump_stylesheet(argv[1]);return 0;
}
總結
以上是生活随笔為你收集整理的使用katana-parser解析css文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。