Home | History | Annotate | Download | only in hdr
      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 #ifndef __DMTOKEN_H__
     18 #define __DMTOKEN_H__
     19 
     20 #ifndef __cplusplus
     21 #error "This is a C++ header file; it requires C++ to compile."
     22 #endif
     23 
     24 /*==================================================================================================
     25 
     26 Header Name: dmToken.h
     27 
     28 General Description: This file contains declaration of utility classes DMToken, DMURI, DMParser
     29 
     30 ==================================================================================================*/
     31 
     32 #include "xpl_Types.h"
     33 #include "dmStringUtil.h"
     34 #include "dm_uri_utils.h"
     35 
     36 /**
     37  * DMToken represents a parser of a string with segments separated by specified delimeter.
     38  */
     39 class DMToken
     40 {
     41  public:
     42   /**
     43   * Default constructor
     44   */
     45   DMToken();
     46 
     47   /**
     48   * Constructor that sets value of delimeter
     49   * \param delimeter [in] - delimeter
     50   */
     51   DMToken(char delimeter);
     52 
     53   /**
     54   * Constructor that sets parameters of an object
     55   * \param bIsAlloc [in] - specifies if internal buffer should be allocated and string copied into it
     56   * \param szStr [in] - string to parse
     57   * \param delimeter [in] - delimeter
     58   */
     59   DMToken(BOOLEAN bIsAlloc, CPCHAR szStr, char delimeter);
     60 
     61   /**
     62   * Destructor
     63   */
     64   ~DMToken();
     65 
     66    /**
     67   * Assigns string to parse
     68   * \param szStr [in] - string to parse
     69   * \return Return Type (CPCHAR)
     70   * - Pointer on internal buffer if operation is completed successfully.
     71   * - NULL otherwise.
     72   */
     73   CPCHAR assign(CPCHAR szStr);
     74 
     75    /**
     76   * Retrieves next segment of a string
     77   * \return Return Type (CPCHAR)
     78   * - Pointer on next segment if operation is completed successfully.
     79   * - NULL otherwise.
     80   */
     81   CPCHAR nextSegment();
     82 
     83   /**
     84   * Retrieves pointer on string
     85   */
     86   char * getBuffer() const { return m_pStr; }
     87 
     88    /**
     89   * Retrieves length of a string
     90   */
     91   INT32 length() { return m_pStr ? DmStrlen(m_pStr):0; }
     92 
     93   /**
     94   * Retrieves count of segments in a string
     95   */
     96   UINT32 getSegmentsCount();
     97 
     98   /**
     99   * Resets object
    100   */
    101   void reset();
    102 
    103 
    104  protected:
    105   /* Pointer on current segment position */
    106   char * m_pTokenPos;
    107   /* Pointer on current delimeter position */
    108   char * m_pDelimPos;
    109   /* Pointer on parsed string */
    110   char * m_pStr;
    111   /* Delimeter */
    112   char m_cDelim;
    113   /* Specifies if internal buffer should eb allocated */
    114   BOOLEAN m_bIsAlloc;
    115 
    116 
    117 };
    118 
    119 
    120 /**
    121 * DMToken represents a URI parser
    122 */
    123 class DMURI : public DMToken
    124 {
    125 
    126 public:
    127   /**
    128   * Default constructor
    129   */
    130   DMURI();
    131 
    132    /**
    133   * Constructor that sets parameters of an object
    134   * \param bIsAlloc [in] - specifies if internal buffer should be allocated and string copied into it
    135   */
    136   DMURI(BOOLEAN bIsAlloc);
    137 
    138    /**
    139   * Constructor that sets parameters of an object
    140   * \param bIsAlloc [in] - specifies if internal buffer should be allocated and string copied into it
    141   * \param szURI [in] - string to parse
    142   */
    143   DMURI(BOOLEAN bIsAlloc, CPCHAR szURI);
    144 
    145    /**
    146   * Retrieves last segment of URI
    147    * \return Return Type (CPCHAR)
    148   * - Pointer on last segment if operation is completed successfully.
    149   * - NULL otherwise.
    150   */
    151   CPCHAR getLastSegment();
    152 
    153   /**
    154   * Retrieves tail segments of URI
    155    * \return Return Type (CPCHAR)
    156   * - Pointer on last segment if operation is completed successfully.
    157   * - NULL otherwise.
    158   */
    159   CPCHAR getTailSegments() const;
    160 
    161    /**
    162   * Retrieves parent URI (without last segment)
    163    * \return Return Type (CPCHAR)
    164   * - Pointer on last segment if operation is completed successfully.
    165   * - NULL otherwise.
    166   */
    167   CPCHAR getParentURI();
    168 
    169 };
    170 
    171 
    172 /**
    173 * Represents segments of URI
    174 */
    175 struct DM_URI_SEGMENT_T
    176 {
    177 
    178 public:
    179    /**
    180   * Default constructor
    181   */
    182   DM_URI_SEGMENT_T()
    183   {
    184     m_pStr = 0;
    185     m_nLen = 0;
    186   }
    187 
    188   /* Pointer on segment */
    189   char * m_pStr;
    190   /* Segment length */
    191   INT32  m_nLen;
    192 };
    193 
    194 
    195 /**
    196 * DMToken represents a URI parser
    197 */
    198 class DMParser
    199 {
    200 
    201 public:
    202   /**
    203   * Constructor that sets parameters of an object
    204   * \param delimeter [in] - delimeter
    205   */
    206   DMParser(char delimeter = SYNCML_DM_FORWARD_SLASH);
    207 
    208   /**
    209   * Constructor that sets parameters of an object
    210   * \param szURI [in] - URI to parse
    211   * \param delimeter [in] - delimeter
    212   */
    213   DMParser(CPCHAR szURI, char delimeter = SYNCML_DM_FORWARD_SLASH);
    214 
    215   /**
    216   * Destructor
    217   */
    218   ~DMParser();
    219 
    220    /**
    221   * Retrieves count of segments in a string
    222   */
    223   inline UINT32 getSegmentsCount() const { return m_nSegmentsCount; }
    224 
    225    /**
    226   * Assigns string to parse
    227   * \param szStr [in] - string to parse
    228   * \return Return Type (CPCHAR)
    229   * - Pointer on internal buffer if operation is completed successfully.
    230   * - NULL otherwise.
    231   */
    232   CPCHAR assign(CPCHAR szStr);
    233 
    234    /**
    235   * Retrieves next segment of a string
    236   * \return Return Type (CPCHAR)
    237   * - Pointer on next segment if operation is completed successfully.
    238   * - NULL otherwise.
    239   */
    240   CPCHAR nextSegment();
    241 
    242   /**
    243   * Searches segment in the URI
    244   * \return TRUE if segment is found
    245   */
    246   BOOLEAN findSegment(CPCHAR szSegment);
    247 
    248   /**
    249   * Resets object
    250   */
    251   void reset();
    252 
    253 protected:
    254   /* Parsed segments */
    255   DM_URI_SEGMENT_T * m_aSegments;
    256   /* Current segment index */
    257   INT32 m_nCurrentSegment;
    258   /* Segments count */
    259   INT32 m_nSegmentsCount;
    260     /* Pointer on current segment position */
    261   char * m_pTokenPos;
    262   /* Pointer on current delimeter position */
    263   char * m_pDelimPos;
    264   /* Pointer on parsed string */
    265   char * m_pStr;
    266   /* Delimeter */
    267   char m_cDelim;
    268 
    269 };
    270 
    271 #endif
    272