| 32 | } |
| 33 | |
| 34 | MediaQuery::ptr MediaQuery::parse( const std::string& str ) { |
| 35 | DisplayManager* displayManager = Engine::instance()->getDisplayManager(); |
| 36 | int currentDisplayIndex = Engine::instance()->getCurrentWindow()->getCurrentDisplayIndex(); |
| 37 | Display* currentDisplay = displayManager->getDisplayIndex( currentDisplayIndex ); |
| 38 | Float dpi = currentDisplay->getDPI(); |
| 39 | MediaQuery::ptr query = std::make_shared<MediaQuery>(); |
| 40 | |
| 41 | std::vector<std::string> tokens = String::split( str, " \t\r\n", "", "(" ); |
| 42 | |
| 43 | for ( auto& tok : tokens ) { |
| 44 | if ( tok == "not" ) { |
| 45 | query->mNot = true; |
| 46 | } else if ( tok.at( 0 ) == '(' ) { |
| 47 | tok.erase( 0, 1 ); |
| 48 | |
| 49 | if ( tok.at( tok.length() - 1 ) == ')' ) { |
| 50 | tok.erase( tok.length() - 1, 1 ); |
| 51 | } |
| 52 | |
| 53 | MediaQueryExpression expr; |
| 54 | std::vector<std::string> exprTokens = String::split( tok, ':' ); |
| 55 | if ( !exprTokens.empty() ) { |
| 56 | String::trimInPlace( exprTokens[0] ); |
| 57 | |
| 58 | expr.feature = (MediaFeature)String::valueIndex( exprTokens[0], MediaFeatureStrings, |
| 59 | media_feature_none ); |
| 60 | |
| 61 | if ( expr.feature != media_feature_none ) { |
| 62 | if ( exprTokens.size() == 1 ) { |
| 63 | expr.checkAsBool = true; |
| 64 | } else { |
| 65 | String::trimInPlace( exprTokens[1] ); |
| 66 | expr.checkAsBool = false; |
| 67 | |
| 68 | if ( expr.feature == media_feature_orientation ) { |
| 69 | expr.val = String::valueIndex( exprTokens[1], MediaOrientationStrings, |
| 70 | media_orientation_landscape ); |
| 71 | } else { |
| 72 | std::string::size_type slash_pos = exprTokens[1].find( '/' ); |
| 73 | if ( slash_pos != std::string::npos ) { |
| 74 | std::string val1 = exprTokens[1].substr( 0, slash_pos ); |
| 75 | std::string val2 = exprTokens[1].substr( slash_pos + 1 ); |
| 76 | String::trimInPlace( val1 ); |
| 77 | String::trimInPlace( val2 ); |
| 78 | |
| 79 | int intVal1, intVal2; |
| 80 | float fVal1, fVal2; |
| 81 | |
| 82 | if ( String::fromString( intVal1, val1 ) && |
| 83 | String::fromString( intVal2, val2 ) ) { |
| 84 | expr.val = intVal1; |
| 85 | expr.val2 = intVal2; |
| 86 | } |
| 87 | |
| 88 | if ( String::fromString( fVal1, val1 ) && |
| 89 | String::fromString( fVal2, val2 ) ) { |
| 90 | expr.fval = fVal1; |
| 91 | expr.fval2 = fVal2; |
no test coverage detected