1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /*================================================================================================== 18 19 Source Name: dm_uri_utils.cc 20 21 General Description: File contains implementation for helper functions for uri processing 22 23 ==================================================================================================*/ 24 25 #include "dm_uri_utils.h" 26 #include "xpl_dm_Manager.h" 27 #include "dmdefs.h" 28 #include "dmtoken.h" 29 #include "dm_tree_class.H" 30 31 CEnv::CEnv() 32 { 33 m_szWFS = m_szMainRFS = ""; 34 } 35 36 CPCHAR CEnv::GetWFSFullPath(CPCHAR name, DMString & path) 37 { 38 return GetFullPath(m_szWFS,name,path); 39 } 40 41 CPCHAR CEnv::GetMainRFSFullPath(CPCHAR name, DMString & path) 42 { 43 return GetFullPath(m_szMainRFS,name,path); 44 } 45 46 CPCHAR CEnv::GetFullPath(CPCHAR fs_path, CPCHAR name, DMString & path) 47 { 48 path = fs_path; 49 path += "/"; 50 path += name; 51 return path.GetBuffer(); 52 } 53 54 CPCHAR CEnv::GetRFSFullPath(int index, CPCHAR name, DMString & path) 55 { 56 return GetFullPath(GetRFS(index),name,path); 57 } 58 59 60 BOOLEAN CEnv::Init() 61 { 62 const char* szEnv = XPL_DM_GetEnv(SYNCML_DM_SETTINGS_PATH); 63 64 if ( !szEnv ) 65 return FALSE; 66 67 DMString strItem, strReminder(szEnv); 68 69 while( DmStringParserGetItem( strItem, strReminder, ':' ) ) 70 m_aFSes.push_back( strItem ); 71 72 if ( m_aFSes.size() == 0 ) 73 return FALSE; 74 75 m_szWFS = m_aFSes[0].c_str(); 76 77 if ( m_aFSes.size() > 1 ) 78 m_szMainRFS = m_aFSes[1].c_str(); 79 else 80 m_szMainRFS = m_szWFS; 81 82 return TRUE; 83 } 84 85 void CEnv::Done() 86 { 87 m_szWFS = m_szMainRFS = ""; 88 m_aFSes.clear(); 89 } 90 91 92 93 bool DmStringParserGetItem( DMString& strItem, DMString& strReminder, char cDelim ) 94 { 95 if ( strReminder[0] == 0 ) 96 return false; 97 98 const char* s = DmStrchr( strReminder, cDelim ); 99 int nPos = s ? s - strReminder : -1; //strReminder.find( cDelim ); 100 101 if ( nPos < 0 ){ 102 strItem = strReminder; 103 strReminder = ""; 104 } 105 else { 106 strItem.assign( strReminder, nPos ); 107 strReminder = DMString(s+1); 108 } 109 return true; 110 } 111 112 113 void DmParseURI(CPCHAR szPath, DMString& strURI, DMString& strKey ) 114 { 115 const char* szSlashPos = DmStrrchr( szPath, SYNCML_DM_FORWARD_SLASH ); 116 117 if ( !szSlashPos ) 118 return; 119 120 strURI.assign( szPath, szSlashPos-szPath); 121 122 szSlashPos++; // skip slash 123 const char* szQuestionPos = DmStrchr( szSlashPos, '?' ); 124 125 if ( szQuestionPos ) { 126 strKey.assign( szSlashPos, szQuestionPos-szSlashPos ); 127 } 128 else 129 strKey = szSlashPos; 130 } 131 132 int DmStrToStringVector(const char * pStr, int nLen, DMStringVector& oVector, char cDelim ) 133 { 134 // load/unload plugins 135 DMString strChild, strList; // = (const char*)pStr; 136 if (nLen >0) 137 { 138 strChild= DMString((const char*)pStr, nLen); 139 strList= DMString((const char*)pStr, nLen); 140 } else 141 { 142 strChild= (const char*)pStr; 143 strList = (const char*)pStr; 144 } 145 146 int i=0; 147 while ( DmStringParserGetItem( strChild, strList, cDelim ) ) 148 { 149 oVector.push_back(strChild); 150 i++; 151 } 152 return i; 153 } 154 155 char* GetURISegment(char **ppbURI) 156 { 157 return GetStringSegment(ppbURI, SYNCML_DM_FORWARD_SLASH); 158 } 159 160 char* GetStringSegment(char **ppbURI, char cDelim) 161 { 162 char *pbURI = *ppbURI; 163 char *pURIStartLocation = pbURI; 164 165 if(pbURI != NULL) 166 { 167 pbURI = DmStrchr(pbURI, cDelim); 168 if(pbURI != NULL) 169 { 170 *pbURI++ = SYNCML_DM_NULL; 171 } 172 } 173 *ppbURI = pbURI; 174 return (pURIStartLocation); 175 } 176 177 BOOLEAN DmIsParentURI( CPCHAR szParentURI, CPCHAR szChildURI ) 178 { 179 DMParser sURIParserP(szParentURI); 180 DMParser sURIParserC(szChildURI); 181 CPCHAR sSegmentP = sURIParserP.nextSegment(); 182 CPCHAR sSegmentC = sURIParserC.nextSegment(); 183 184 UINT16 countP = sURIParserP.getSegmentsCount(); 185 UINT16 countC = sURIParserC.getSegmentsCount(); 186 187 if ( countP == 0 ) 188 return TRUE; 189 190 if ( countP > countC ) 191 return FALSE; 192 193 while ( sSegmentP && sSegmentC ) 194 { 195 if ( *sSegmentP != '*' && *sSegmentC != '*' && DmStrcmp(sSegmentC, sSegmentP) != 0 ) 196 { 197 return FALSE; 198 } 199 200 sSegmentP = sURIParserP.nextSegment(); 201 sSegmentC = sURIParserC.nextSegment(); 202 } 203 return TRUE; 204 } 205 206 207 SYNCML_DM_RET_STATUS_T 208 DmIsDMAccNodePath(CPCHAR szPath) 209 { 210 //CPCHAR dm_ver = XPL_DM_GetEnv(SYNCML_DM_VERSION); 211 SYNCML_DM_RET_STATUS_T result = SYNCML_DM_COMMAND_INVALID_URI; 212 213 const char *found = NULL; 214 215 if( dmTreeObj.IsVersion_12() ) 216 { 217 found = DmStrstr(szPath, DM_DMACC_1_1_URI ); 218 } 219 else 220 { 221 found = DmStrstr(szPath, DM_DMACC_1_2_URI ); 222 } 223 224 result = found && ( found == szPath ) 225 ? SYNCML_DM_FEATURE_NOT_SUPPORTED 226 : SYNCML_DM_SUCCESS; 227 228 return result; 229 } 230 231 BOOLEAN 232 DmIsEnabledURI(CPCHAR szPath) 233 { 234 235 SYNCML_DM_RET_STATUS_T dm_stat; 236 237 dm_stat = DmIsDMAccNodePath(szPath); 238 if ( dm_stat == SYNCML_DM_FEATURE_NOT_SUPPORTED ) 239 return FALSE; 240 241 return XPL_DM_IsFeatureEnabled(szPath); 242 } 243