This document describes the operation of the STRING class in BitPim.
BitPim uses two string types. The built in python strings for most of the code including the GUI and database, and a STRING class that is used in the construction of PACKETs for communication with the phone.
The STRING class is defined in prototypes.py
It translates text in communications with the phone. It is very specialised for this task and has some features not found in a regular string objects.
The STRING class converts the python strings into a format that the phones can understand.
There are several properties (keywords) of a string this class has to make conversion, how they are set depends on how the phone works.
constant terminator pad sizeinbytes default raiseonunterminatedread raiseontruncate value pascal encoding read_encoding write_encoding
constant
(Optional) A constant value. This string can only have one value, L{ValueException} will be raised if it is set to anything else.
terminator
(Default=0 (like c strings)) Set this to the string terminator or 'None' is there is no terminator. If set there will always be a terminator when writing. The terminator is not returned when getting the value. The terminator is counted in the length of the string and is sent to the phone.
pad
(Default=0) The padding byte used when writing fixed length strings, stripped off when reading from the phone.
sizeinbytes
(Optional) Set if the phone uses fixed length strings. If not set, then the terminator will be used to find the end of strings on reading. If not set and the terminator is None, then reads will be entire rest of buffer.
default
(Optional) The default value to assign to the string, can be overwritten.
raiseonunterminatedread
(Default True) L{NotTerminatedException} will be raised if there is no terminator on the value being read in. terminator keyword must also be set or this keyword is ignored.
raiseontruncate
(Default True) L{ValueLengthException} will be raised if the supplied value is too large to fit within sizeinbytes.
value
(Optional) Value. Value to assign to the string.
pascal
(Default False) Pascal sytle string, the string is preceded with one byte giving the length of the string (including terminator if there is one)
encoding
(Default 'ascii') The charset to use when reading from/writing to a buffer.
read_encoding
(Default None) The charset to use to convert the string to unicode when reading from a buffer. If None then encoding is used to as the charset.
write_encoding
(Default None) The charset the string is converted to when writing to a buffer. If None then encoding is used to as the charset.
UNICODE
Internally BitPim uses UNICODE for all text string, most phones do not.
The STRING class acts as a conversion object to go from unicode to whatever the phone supports. It does this without the phone developer having to do anything. It generates it's own exceptions when it cannot convert the python unicde string to the phone's string format L{PhoneStringEncodeException} and when it cannot convert the phone's format to a python unicode string L{PhoneStringDecodeException}. L{PhoneStringEncodeException} causes the user to see an error message (it is handled gracefully). L{PhoneStringDecodeException} is not, if you get this then the encoding of the string is incorrect and needs to be fixed.
The B>sizeinbytes keyword does not mean string length. Some string encoding types use more than one byte for a character, and some use different numbers of bytes for different characters (e.g. UTF-8).
STRINGs can be assigned values using '=' like other data types.
Example STRING uses in phone PACKETs
60 STRING {'raiseonunterminatedread': False, 'raiseontruncate': False } filename "includes full pathname" 23 STRING {'encoding': PHONE_ENCODING, 'raiseonunterminatedread': False, 'raiseontruncate': False } name * STRING {'terminator': 0xA, 'default':'ams:'} +location_maybe
The keyword values are specified inbetween the curly braces.
The number at the start is used as the sizeinbytes keyword. Using a '*' causes it to be left as default.
The advantage of using the sizeinbytes keyword is that you can use non-hardcoded values.
The + before the name of the string means that the value will be set to the default value without having to be explicitly assigned, used when packets are written to the phone.