cssParser

 

//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;

 

转载于:https://www.cnblogs.com/hqu-ye/p/4029604.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值