//cssParser.h

#include<iostream>
using namespace std;
struct MyAttribute
{
 MyAttribute*  next;
 string m_strName;
 string m_strValue;
 MyAttribute()
 {
  ReSet();
 }
 void ReSet()
 {
  m_strName = "";
  m_strValue = "";
  next = NULL;
 }

MyAttribute& operator= (MyAttribute& rhs)
 {
  if (this != &rhs)
  {
   m_strName = rhs.m_strName;
   m_strValue = rhs.m_strValue;
   next = rhs.next;   
  }
  return *this;   
 }
};
class  MyHash
{
public :

private:

};

//cssParser.cpp

#include<iostream>
#include<unistd.h>
#include<fstream>
#include<string>
#include "CssParser.h"
using namespace std;

#define STR_WRITE(x) x, sizeof(x) - 1
MyAttribute* pColorListHead = new MyAttribute();
MyAttribute* pImageListHead = new MyAttribute();

void DeleteMyAttribute(MyAttribute* del)
{
 if (!del)
 {
  return ;
 } 
 DeleteMyAttribute(del->next);
 delete del;
}

//0xEF, 0xBB, 0xBF,  UTF-8 标记BOM
bool InitializeColorCheckTable(MyAttribute* pHead, string path)
{
 ifstream inFile;
 string strEqualLeft = "";
 string strKey = "";
 string strValue = "";
 int index = -1;
 MyAttribute* pTail = pHead; 
 inFile.open(path.c_str(), ios::in); 
 if (NULL == inFile )
 {   
  printf("open file failed");
  inFile.close();
  return false;
 }
 while (getline(inFile, strEqualLeft))
 { 
  if (strEqualLeft.length() >2
   && strEqualLeft[0] == 0xEF
   && strEqualLeft[1] == 0xBB
   && strEqualLeft[2] == 0xBF)
  {   
   strEqualLeft = strEqualLeft.substr(3, strEqualLeft.length());
  }
  if (strEqualLeft.length() >0
   && strEqualLeft[0] == '#')
  {
   continue;
  }
  index = (int)strEqualLeft.find_last_of('=') ;
  if (-1 == index)
  {
   continue;
  }  
  strKey = strEqualLeft.substr(0, index);   
  strValue = strEqualLeft.substr(index+1, strEqualLeft.length());

MyAttribute* pItem = new MyAttribute();
  if (!pItem)
  {
   return false;
  }
  pItem->m_strName = strKey;
  pItem->m_strValue = strValue;
  pTail->next = pItem;
  pTail = pTail->next;
  //cout<<"Key:"<<pItem->m_strName<<" Value:"<<pItem->m_strValue<<endl;
 }

inFile.close();
 return true;
}

string GetAttribute(string key)
{
 MyAttribute *pItem = pColorListHead;
 for ( ;
  pItem !=NULL;
  pItem = pItem->next)
 {
  if (pItem->m_strName == key)
  {   
   return pItem->m_strValue;
  }
 }
 return "";
}

bool CssColorParser(string strDirectory, string outPath)
{
 ifstream inFile;
 ofstream  outFile;
 int index = 0; 
 int nend = 0; 
 string StrOneline = "";
 string ItemCss = "";  
 string strEqualLeft = "";
 string strIDName="";
 string strStyleName="";
 string strColorValue="";
 
 inFile.open(strDirectory.c_str(), ios::in); 
 outFile.open(outPath.c_str(), ios::out);//"./custom.css"
 if (NULL == inFile || NULL == outFile)
 {
  printf("open file[%s] failed outPath[%s]-----\n", strDirectory.c_str(), outPath.c_str());
  inFile.close();
  outFile.close();
  return false;
 }

InitializeColorCheckTable(pColorListHead, "./ColorCheckTable.cfg");

outFile.write(STR_WRITE("@charset \"utf-8\";\r\n"));

while (getline(inFile, StrOneline))
 {  
  strEqualLeft = "";
  strIDName = "";
  strStyleName = "";
  strColorValue = "";

if (StrOneline.length() >2
   && StrOneline[0] == 0xEF
   && StrOneline[1] == 0xBB
   && StrOneline[2] == 0xBF)
  {   
   StrOneline = StrOneline.substr(3, StrOneline.length());
  }

if (StrOneline.length() >0
   && StrOneline[0] == '#')
  {
   continue;
  }
  nend = (int)StrOneline.find_last_of('=') ;
  if(nend == -1)
  {   
   continue;
  }
  //解析=左边的值
  strEqualLeft = StrOneline.substr(0, nend);
  index = (int)strEqualLeft.find_last_of('.') ;
  strIDName = strEqualLeft.substr(0, index);  
  strIDName = GetAttribute(strIDName);
  if (strIDName == "")
  {   
   continue;
  }
  strStyleName = strEqualLeft.substr(index+1, strEqualLeft.length());
  //解析=右边的值
  strColorValue = StrOneline.substr(nend+1, StrOneline.length());
  int cutIndex = 0;
  cutIndex = strColorValue.find_last_of('\r');
  if (cutIndex != -1)
  {
   strColorValue = strColorValue.substr(0, cutIndex);
  }
  //构造Css样式语句
  ItemCss = strIDName+"{\r\n"+strStyleName+":"+strColorValue+";\r\n}\r\n";
  //cout<<ItemCss<<endl;
  outFile.write(ItemCss.c_str(),ItemCss.length());

}
 inFile.close();
 outFile.close();
 return true;
}

bool CssImageParser(string strDirectory, string urlPath, string randNum, string outPath)
{
 ofstream  outFile;
 string strIDName="";
 string strStyleName="background-image";
 string strUrlValue="";
 string ItemCss = "";
 string filePath = "";

outFile.open(outPath.c_str(), ios::app);
 if (NULL == outFile)
 {
  printf("open file[%s] failed  +++++\n", outPath.c_str());  
  outFile.close();
  return false;
 } 
 InitializeColorCheckTable(pImageListHead, "./ImageCheckTable.cfg");
 MyAttribute *p = pImageListHead->next;
 for( ;
  NULL != p ;
  p = p->next)
 {
  filePath = strDirectory + p->m_strName;
  //cout<<"filePath:"<<filePath<<endl;
  if(access(filePath.c_str(), F_OK) == 0)
  {
   strIDName = p->m_strValue;
   strUrlValue = "url("+urlPath+p->m_strName+"?"+randNum+")";
   ItemCss = strIDName+"{\r\n"+strStyleName+":"+strUrlValue+";\r\n}\r\n";
   outFile.write(ItemCss.c_str(),ItemCss.length());
  }
 }
 return true;
}

int main(int argc, char *argv[])
{  
 if (argc == 0)
 {

}
 else if(argc == 3)
 {
  CssColorParser(argv[1], argv[2]);
  DeleteMyAttribute(pColorListHead);
  DeleteMyAttribute(pImageListHead);
 }
 else if (argc == 6)
 {
  CssColorParser(argv[1],argv[5]);
  CssImageParser(argv[2],argv[3],argv[4],argv[5]);
  DeleteMyAttribute(pColorListHead);
  DeleteMyAttribute(pImageListHead);
 }
 
 return 0;
}

cssParser的更多相关文章

  1. 浏览器-05 HTML和CSS解析1

    一个浏览器内核几个主要部分,HTML/CSS解析器,网络处理,JavaScript引擎,2D/3D图形引擎,多媒体支持等; HTML 解析和 DOM 网页基本结构 一个网页(Page),每个Page都 ...

  2. JMeter学习-027-JMeter参数文件(脚本分发)路径问题:jmeter.threads.JMeterThread: Test failed! java.lang.IllegalArgumentException: File distributed.csv must exist and be readable解决方法

    前些天,在进行分布式参数化测试的时候,出现了如题所示的错误报错信息.此文,针对此做一个简略的重现及分析说明. JMX脚本线程组参数配置如下所示: 参数文件路径配置如下所示: 执行JMX脚本后,服务器对 ...

  3. 使用Maven构建RichFaces 4.x项目

    使用Maven构建RichFaces 4.x项目 目录 开始之前 第一步 - 创建Maven项目 第二布 - 添加依赖文件 第三步 - 配置RichFaces 第四步 - 创建显示页面 开始之前 本文 ...

  4. javafx之CSS初探

    文档:http://www.haogongju.net/art/1807238 javafx中的css元素必须有-fx-前缀. 一.介绍 java8中新增了javafx.css开放了css相关api. ...

  5. Android常用组件

    UI相关 图片 Android-Universal-Image-Loader:com.nostra13.universalimageloader:异步加载.缓存.显示图片 ImageLoader:co ...

  6. Top Android App使用的组件 3

    8684公交 AdChina:com.adchina:易传媒广告平台 AdsMogo:com.adsmogo:芒果移动广告平台 大姨吗 AChartEngine:org.achartengine:An ...

  7. Android常用组件【转】

    UI相关 图片 Android-Universal-Image-Loader:com.nostra13.universalimageloader:异步加载.缓存.显示图片 ImageLoader:co ...

  8. Android经常使用开源组件汇总

    http://www.cnblogs.com/scige/p/3456790.html UI相关 图片 Android-Universal-Image-Loader:com.nostra13.univ ...

  9. CSS3基础(4)——CSS3 渲染属性

    一. CSS3 计数器详解    CSS3计数器(CSS Counters)可以允许我们使用CSS对页面中的任意元素进行计数,实现类似于有序列表的功能. 与有序列表相比,它的突出特性在于可以对任意元素 ...

随机推荐

  1. nginx反向代理带路径访问问题

    nginx的配置为192.168.0.219:80分别映射到upstream组192.168.0.55:8080和192.168.0.206:8080,那如何配置做到访问192.168.0.219:8 ...

  2. 常用PhpStorm 快捷键

    函数列表 打开某一个源码文件后,保证鼠标焦点在源文件内,按键盘组合键: alt + 7 返回原文件导航:双击最上面的工程名即可 PhpStorm折叠文件内所有函数 按下快捷`Ctrl`+`Shift` ...

  3. Java应用一般架构

    转载一下文章: 自己连看三便方的其要点精髓. 当我们架设一个系统的时候通常需要考虑到如何与其他系统交互,所以我们首先需要知道各种系统之间是如何交互的,使用何种技术实现. 1. 不同系统不同语言之间的交 ...

  4. python 利用pymssql连接MSSQL数据库,简单示例

    #-*- coding:GBK -*- import pymssql print 'Connect to the Datebase....' conn = pymssql.connect(host=' ...

  5. python selenium2示例 - email发送

    前言 在进行日常的自动化测试实践中,我们总是需要将测试过程中的记录.结果等等等相关信息通过自动的手段发送给相关人员.python的smtplib.email模块为我们提供了很好的email发送等功能的 ...

  6. ios - 使用@try、catch捕获异常:

    @try { // 可能会出现崩溃的代码 } @catch (NSException *exception) { // 捕获到的异常exception } @finally { // 结果处理 }

  7. 九度OJ 1261:寻找峰值点 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:500 解决:37 题目描述: 给定一个整数序列,该整数序列存在着这几种可能:先递增后递减.先递减后递增.全递减.全递增. 请找出那个最大值的 ...

  8. Android系统移植与调试之------->如何修改Android自带的apk出现一圈圈类似鸡蛋的花纹

    最近被一个问题烦恼到了,就是android4.1系统自带的Email.文件管理器.信息等apk都出现同一个问题,就是现实在平板上的时候会出现一圈圈类似鸡蛋的花纹. 我想了两种方法来解决,第一种方法没有 ...

  9. 去掉标题栏/ActionBar后点击menu键时应用崩溃

    MainActivity 继承了 ActionBarActivity后,想要去掉标题栏(ActionBar),在程序中加上requestWindowFeature(Window.FEATURE_NO_ ...

  10. matlab + c/c++ opencv 混合编程

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 辛苦原创所得,转载请注明出处 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ...