logo separator

[mkgmap-dev] methods to write signed / unsigned integers

From Ticker Berkin rwb-mkgmap at jagit.co.uk on Thu Feb 15 11:30:47 GMT 2018

Hi Gerd

I started to think about this a while ago when I was fixing cities >
65535, and introduced some alternative names for get/getChar and
put/putChar and also added some of the assertions. But then I noticed
that some of the names I used were used for BIT I/O elsewhere, and also
the Display tree shared the same code.

I didn't like having code like:
        int n = reader.get() & 0xff;
        writer.putChar((char) val);
inline; I think this is much clearer:
        int n = reader.getU1()
        writer.putU2(val)
and allows for range checking, but you have to study the code to know
if you should use putSx() or putUx()

Maybe something like:
   getU1(),    getU2(),    getU3(),    getUN(nBytes)
   getS1(),    getS2(),    getS3(),    getSN(nBytes)
   putU1(int), putU2(int), putU3(int), putUN(nBytes, int)
   putS1(int), putS2(int), putS3(int), putSN(nBytes, int)

For full integer/4 byte	I/O, it	is meaningless to
differentiate between Signed & Unsigned, but might be clearer to have
xxxU4() and xxxS4() routines as well and use instead of
getInt()/putInt()

There are names a bit like some of these already in
 imgfmt/app/ImgFile{Reader,Writer}.java                                
                                    
and their implementations

Regards
Ticker

On Thu, 2018-02-15 at 10:06 +0000, Gerd Petermann wrote:
> Hi,
> 
> we have various methods to write an integer with 1, 2, 3, or 4 bytes
> to an img file.
> I always feel unsure what method to use because none of them makes
> clear what happens with negative values.
> 
> Besides that some of the existing routines seem to throw misleading
> exceptions, 
> e.g. FileBackedImgFileWriter seems to assume that it is only used for
> the mdr tmp file and creates texts like this:
> throw new MapFailedException("could not write3 to mdr tmp file");
> throw new MapFailedException("could not write put3 to mdr tmp file");
> 
> Only the javadoc for put1() and put2() tell me the range (0..255 or
> 0..65535) .
> 
> If I got that right put3() allows negative values, so I think it is
> NOT 0..16777215 but -8388608 .. 8388607 ?
> 
> I'd like to improve the readability of the code, but I don't want to
> mess up anything.
> Would it be possible to add comments to all the methods?
> 
> Gerd
> 
> 
> 
> _______________________________________________
> mkgmap-dev mailing list
> mkgmap-dev at lists.mkgmap.org.uk
> http://www.mkgmap.org.uk/mailman/listinfo/mkgmap-dev


More information about the mkgmap-dev mailing list