|
| 1 | +/**************************************************************************** |
| 2 | +Copyright (c) 2010 cocos2d-x.org |
| 3 | +
|
| 4 | +http://www.cocos2d-x.org |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +of this software and associated documentation files (the "Software"), to deal |
| 8 | +in the Software without restriction, including without limitation the rights |
| 9 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +copies of the Software, and to permit persons to whom the Software is |
| 11 | +furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in |
| 14 | +all copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | +THE SOFTWARE. |
| 23 | +****************************************************************************/ |
| 24 | +#include "CCNS.h" |
| 25 | +#include <string> |
| 26 | +#include <vector> |
| 27 | +#include <stdlib.h> |
| 28 | + |
| 29 | +typedef std::vector<std::string> strArray; |
| 30 | + |
| 31 | +// string toolkit |
| 32 | +void split(std::string src, const char* token, strArray& vect) |
| 33 | +{ |
| 34 | + int nend=0; |
| 35 | + int nbegin=0; |
| 36 | + while(nend != -1) |
| 37 | + { |
| 38 | + nend = src.find(token, nbegin); |
| 39 | + if(nend == -1) |
| 40 | + vect.push_back(src.substr(nbegin, src.length()-nbegin)); |
| 41 | + else |
| 42 | + vect.push_back(src.substr(nbegin, nend-nbegin)); |
| 43 | + nbegin = nend + strlen(token); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// first, judge whether the form of the string like this: {x,y} |
| 48 | +// if the form is right,the string will be splited into the parameter strs; |
| 49 | +// or the parameter strs will be empty. |
| 50 | +// if the form is right return true,else return false. |
| 51 | +bool splitWithForm(const char* pStr, strArray& strs) |
| 52 | +{ |
| 53 | + bool bRet = false; |
| 54 | + |
| 55 | + do |
| 56 | + { |
| 57 | + CC_BREAK_IF(!pStr); |
| 58 | + |
| 59 | + // string is empty |
| 60 | + std::string content = pStr; |
| 61 | + CC_BREAK_IF(content.length() == 0); |
| 62 | + |
| 63 | + int nPosLeft = content.find('{'); |
| 64 | + int nPosRight = content.find('}'); |
| 65 | + |
| 66 | + // don't have '{' and '}' |
| 67 | + CC_BREAK_IF(nPosLeft == std::string::npos || nPosRight == std::string::npos); |
| 68 | + // '}' is before '{' |
| 69 | + CC_BREAK_IF(nPosLeft > nPosRight); |
| 70 | + |
| 71 | + std::string pointStr = content.substr(nPosLeft + 1, nPosRight - nPosLeft - 1); |
| 72 | + // nothing between '{' and '}' |
| 73 | + CC_BREAK_IF(pointStr.length() == 0); |
| 74 | + |
| 75 | + int nPos1 = pointStr.find('{'); |
| 76 | + int nPos2 = pointStr.find('}'); |
| 77 | + // contain '{' or '}' |
| 78 | + CC_BREAK_IF(nPos1 != std::string::npos || nPos2 != std::string::npos); |
| 79 | + |
| 80 | + split(pointStr, ",", strs); |
| 81 | + if (strs.size() != 2 || strs[0].length() == 0 || strs[1].length() == 0) |
| 82 | + { |
| 83 | + strs.clear(); |
| 84 | + break; |
| 85 | + } |
| 86 | + |
| 87 | + bRet = true; |
| 88 | + } while (0); |
| 89 | + |
| 90 | + return bRet; |
| 91 | +} |
| 92 | + |
| 93 | +// implement the functions |
| 94 | +namespace cocos2d |
| 95 | +{ |
| 96 | + CCRect CCRectFromString(const char* pszContent) |
| 97 | + { |
| 98 | + CCRect result = CCRectZero; |
| 99 | + |
| 100 | + do |
| 101 | + { |
| 102 | + CC_BREAK_IF(!pszContent); |
| 103 | + std::string content = pszContent; |
| 104 | + |
| 105 | + // find the first '{' and the third '}' |
| 106 | + int nPosLeft = content.find('{'); |
| 107 | + int nPosRight = content.find('}'); |
| 108 | + for (int i = 1; i < 3; ++i) |
| 109 | + { |
| 110 | + if (nPosRight == std::string::npos) |
| 111 | + { |
| 112 | + break; |
| 113 | + } |
| 114 | + nPosRight = content.find('}', nPosRight + 1); |
| 115 | + } |
| 116 | + CC_BREAK_IF(nPosLeft == std::string::npos || nPosRight == std::string::npos); |
| 117 | + |
| 118 | + content = content.substr(nPosLeft + 1, nPosRight - nPosLeft - 1); |
| 119 | + int nPointEnd = content.find('}'); |
| 120 | + CC_BREAK_IF(nPointEnd == std::string::npos); |
| 121 | + nPointEnd = content.find(',', nPointEnd); |
| 122 | + CC_BREAK_IF(nPointEnd == std::string::npos); |
| 123 | + |
| 124 | + // get the point string and size string |
| 125 | + std::string pointStr = content.substr(0, nPointEnd); |
| 126 | + std::string sizeStr = content.substr(nPointEnd + 1, content.length() - nPointEnd); |
| 127 | + |
| 128 | + // split the string with ',' |
| 129 | + strArray pointInfo; |
| 130 | + CC_BREAK_IF(!splitWithForm(pointStr.c_str(), pointInfo)); |
| 131 | + strArray sizeInfo; |
| 132 | + CC_BREAK_IF(!splitWithForm(sizeStr.c_str(), sizeInfo)); |
| 133 | + |
| 134 | + float x = (float) atof(pointInfo[0].c_str()); |
| 135 | + float y = (float) atof(pointInfo[1].c_str()); |
| 136 | + float width = (float) atof(sizeInfo[0].c_str()); |
| 137 | + float height = (float) atof(sizeInfo[1].c_str()); |
| 138 | + |
| 139 | + result = CCRectMake(x, y, width, height); |
| 140 | + } while (0); |
| 141 | + |
| 142 | + return result; |
| 143 | + } |
| 144 | + |
| 145 | + CCPoint CCPointFromString(const char* pszContent) |
| 146 | + { |
| 147 | + CCPoint ret = CCPointZero; |
| 148 | + |
| 149 | + do |
| 150 | + { |
| 151 | + strArray strs; |
| 152 | + CC_BREAK_IF(!splitWithForm(pszContent, strs)); |
| 153 | + |
| 154 | + float x = (float) atof(strs[0].c_str()); |
| 155 | + float y = (float) atof(strs[1].c_str()); |
| 156 | + |
| 157 | + ret = CCPointMake(x, y); |
| 158 | + } while (0); |
| 159 | + |
| 160 | + return ret; |
| 161 | + } |
| 162 | + |
| 163 | + CCSize CCSizeFromString(const char* pszContent) |
| 164 | + { |
| 165 | + CCSize ret = CCSizeZero; |
| 166 | + |
| 167 | + do |
| 168 | + { |
| 169 | + strArray strs; |
| 170 | + CC_BREAK_IF(!splitWithForm(pszContent, strs)); |
| 171 | + |
| 172 | + float width = (float) atof(strs[0].c_str()); |
| 173 | + float height = (float) atof(strs[1].c_str()); |
| 174 | + |
| 175 | + ret = CCSizeMake(width, height); |
| 176 | + } while (0); |
| 177 | + |
| 178 | + return ret; |
| 179 | + } |
| 180 | +} |
0 commit comments