ST / NDefLib

Dependents:   NFC M2M_2016_STM32 MyongjiElec_capstone1 IDW01M1_Cloud_IBM ... more

Fork of NDefLib by ST Expansion SW Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RecordText.h Source File

RecordText.h

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    RecordText.h
00004  * @author  ST / Central Labs
00005  * @version V2.0.0
00006  * @date    28 Apr 2017
00007  * @brief   {@link Record} containing a simple text
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00012  *
00013  * Redistribution and use in source and binary forms, with or without modification,
00014  * are permitted provided that the following conditions are met:
00015  *   1. Redistributions of source code must retain the above copyright notice,
00016  *      this list of conditions and the following disclaimer.
00017  *   2. Redistributions in binary form must reproduce the above copyright notice,
00018  *      this list of conditions and the following disclaimer in the documentation
00019  *      and/or other materials provided with the distribution.
00020  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00021  *      may be used to endorse or promote products derived from this software
00022  *      without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  ******************************************************************************
00036  */
00037 
00038 
00039 #ifndef NDEFLIB_RECORDTYPE_RECORDTEXT_H_
00040 #define NDEFLIB_RECORDTYPE_RECORDTEXT_H_
00041 #include <string>
00042 #include <NDefLib/Record.h>
00043 
00044 namespace NDefLib {
00045 
00046 /**
00047  * {@link Record} containing a simple text.
00048  * @par The text is copied inside the class.
00049  */
00050 class RecordText: public Record {
00051 public:
00052 
00053     /**
00054      * Read a recordText from a buffer.
00055      * @param header Record header.
00056      * @param buffer Buffer to read the record content from.
00057      * @return a record of type Text or NULL if it was not possible build this type of record
00058      * @par User is in charge of freeing the pointer returned by this function.
00059      */
00060     static RecordText* parse(const RecordHeader &header,
00061             const uint8_t * const buffer);
00062 
00063     /**
00064      * Set the type of encoding used to store the text data
00065      */
00066     typedef enum {
00067         UTF8, //!< UTF8
00068         UTF16,//!< UTF16
00069     } TextEncoding;
00070 
00071     /**
00072      * Build a utf8, English text record.
00073      * @param text Text to store in the record.
00074      */
00075     explicit RecordText(const std::string &text="");
00076 
00077     /**
00078      * Build a text record.
00079      * @param encoding Rype used to store the message.
00080      * @param language Language used in the text.
00081      * @param Text record text.
00082      */
00083     RecordText(const TextEncoding encoding, const std::string &language,
00084             const std::string &text);
00085 
00086     /**
00087      * Get the record type.
00088      * @return TYPE_TEXT
00089      */
00090     virtual RecordType_t get_type() const {
00091         return TYPE_TEXT;
00092     } //getType
00093 
00094     /**
00095      * Get the text inside this record.
00096      * @return the text content
00097      */
00098     const std::string& get_text() const {
00099         return mText;
00100     }
00101 
00102     /**
00103      * Change the text content.
00104      * @param text New text to store.
00105      */
00106     void set_text (const std::string &text){
00107         mText = text;
00108         update_playload_length();
00109     }
00110 
00111     /**
00112      * Get the language used in the text.
00113      * @return Language used in the text.
00114      */
00115     const std::string& get_language()const{
00116         return mLanguage;
00117     }
00118     
00119     /**
00120      * Get the encoding used to store the text.
00121      * @return get the encoding used to store the text
00122      */
00123     TextEncoding get_encoding()const{
00124         return mEncode;
00125     }
00126 
00127     /**
00128      * Compare 2 objects.
00129      * @return true if the objects have the same encoding,language and text
00130      */
00131     bool operator==(const RecordText &other)const{
00132         return  mTextStatus == other.mTextStatus &&
00133                 mLanguage == other.mLanguage &&
00134                 mText == other.mText;
00135 
00136     }
00137 
00138     virtual uint16_t write(uint8_t *buffer);
00139     virtual ~RecordText() { };
00140 
00141 private:
00142 
00143     void set_record_header();
00144 
00145     void update_playload_length(){
00146         mRecordHeader.set_payload_length(1 + mLanguage.size() + mText.size());
00147     }
00148 
00149     /**
00150      * encode the text encoding type and language size in a single byte
00151      * @param enc encoding used by this record
00152      * @param language language used by this record
00153      * @return text encoding and language size in a single byte
00154      */
00155     static uint8_t get_text_status(TextEncoding enc,
00156             const std::string &language) {
00157         uint8_t status = language.size();
00158         if (enc == UTF16) {
00159             status &= 0x80; // set to 1 the bit 7
00160         } //if
00161         return status;
00162     } //getTextStatus
00163 
00164     /**
00165      * extract the encoding information from a status byte
00166      * @param textStatus status byte
00167      * @return encoding used by this record
00168      */
00169     static TextEncoding get_encoding(const uint8_t textStatus) {
00170         if ((textStatus & 0x80) == 0)
00171             return UTF8;
00172         else
00173             return UTF16;
00174     }
00175 
00176     /**
00177      * extract language string length from a status byte
00178      * @param textStatus status byte
00179      * @return number of bytes needed to store the language name used by this record
00180      */
00181     static uint8_t get_language_length(const uint8_t textStatus) {
00182         //take the fist 5 bits
00183         return textStatus & 0x1F;
00184     }
00185 
00186     const TextEncoding mEncode;
00187     const std::string mLanguage;
00188     std::string mText;
00189 
00190     const uint8_t mTextStatus;
00191 
00192     static const uint8_t NDEFTextIdCode;
00193 
00194 };
00195 
00196 } /* namespace NDefLib */
00197 
00198 #endif /* NDEFLIB_RECORDTYPE_RECORDTEXT_H_ */