-1

I'd like to know how to split the css file on tokens using C11++ regex and insert into std::map as key/value?

CSS file:

#id1 {
    border: 1px;
    color: red;
}
#id2 {
    border: 2px;
    color: green;
};

C++ code:

#include <iostream>
#include <fstream>
#include <map>
#include <regex>
using namespace std;

int main() {
    ifstream file("file.css", ios::binary);

    if(!file.is_open())
        return 1;

    //... how to split file on tokens? 

    //and insert tokens probably like this (but only in a loop and dynamically):
    map<string, string> array;
    array.insert(make_pair("#id1", "border: 1px; color: red;"));
    array.insert(make_pair("#id2", "border: 1px; color: green;"));
    //...

    map<string, string>::iterator i = array.begin();

    while(i != array.end()) {
        cout << "key: " << i->first << " | value: " << i->second << endl;
        i++;
    }

    //out:
    /*
    key: #id1 | value: border: 1px; color: red;
    key: #id2 | value: border: 2px; color: green;
    */
    return 0;
}

What i need in JavaScript implementation:

var css = "\
    #id1 {\
        border: 1px;\
        color: red;\
    }\
    #id2 {\
        border: 2px;\
        color: green;\
    }";

//remove tabulation and split on tokens 
var array = css.replace(/[\t\n\r]*/g, '').replace(/\s{2}/g, ' ').split('}'),
    i = array.length-1,
    stack = [];

//close right brace and insert into stack[]
while(i--) {
    stack.unshift(array[i] + "}");
}

i =  stack.length;

var map = {};

//fill map (key/value)
while(i--) {
    map[stack[i].match(/(.*){.*}/)[1]] = stack[i].match(/.*{(.*)}/)[1];
}
//out
for(i in map) {
    document.write('key: ' + i + ' | value: ' + map[i] + '<br />')
}

Please see working example

In fact, I just need a simple example of a splitting css file using regex

Algorithm
  • 329
  • 6
  • 16
  • Have gcc implemented `std::regex` with all features the Standard requires? – Nawaz Dec 18 '11 at 13:22
  • 4
    Which part of this is unclear -- how to use `` in C++, or what the actual regular expressions should be? Each has a straight-forward answer, but this is too much of a "write my entire program for me" type question as it stands. – Kerrek SB Dec 18 '11 at 13:27
  • I've the last version GCC 4.7. In fact, I just need a simple example of a splitting css file using regex – Algorithm Dec 18 '11 at 13:31
  • @Algorithm: I don't think GCC 4.7's listdc++ has `` fully implemented yet. You'll need Clang's libc++ or MSVC 10 or 11. – rubenvb Dec 18 '11 at 13:59
  • @rubenvb has been implemented yet http://gcc.gnu.org/projects/cxx0x.html – Algorithm Dec 18 '11 at 14:39
  • @Algorithm: I don't see any reference to `` on that page, justly so (it's a library feature, not a compiler feature). Here is where it specifically says only bad partial support is complete: http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.200x – rubenvb Dec 18 '11 at 15:18
  • 1
    You might be better off using boost::regex or boost::xpressive - as stated gcc's regex implementation might not be complete. I couldn't get GCC4.6's regex implemntation to work correctly but switching to boost::xpressive solved the problem. – mark Dec 18 '11 at 19:16

2 Answers2

0

As with RegEx match open tags except XHTML self-contained tags, you shouldn't use regular expressions to parse a language like CSS. Instead, consider something like ANTLR, which is a proper parser generator and is at least capable of giving correct results.

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • I understand that the use of regular expressions is not a good practice to parse the CSS. But in my case is more suitable – Algorithm Dec 18 '11 at 16:45
  • "I understand that the use of firearms is not a good practice to clear the table after dinner. But in my case it is more suitable." It doesn't sound right, at least not without a detailed explanation (maybe you have no hands, but can operate firearms with your feet, and your family has done it this way for generations). – John Zwinck Dec 18 '11 at 16:47
  • Well, I did the same example of what I need with JavaSctipt, but I want to know how to do the same thing in C++ – Algorithm Dec 18 '11 at 16:49
0

As stated by John you should consider writing a proper parser.

Have a look at boost::spirit. The tutorial shows how to write a simple parser for reading XML into an abstract syntax tree.

http://www.boost.org/libs/spirit/

ildjarn
  • 62,044
  • 9
  • 127
  • 211
mark
  • 7,381
  • 5
  • 36
  • 61
  • Thanks, i know about boost::spirit (great library), but i want to parse it myself. I had almost finished the one, and soon show the result – Algorithm Dec 18 '11 at 20:28