mappy.py 767 B

123456789101112131415161718192021222324252627282930313233343536
  1. #! /usr/bin/env python2.7
  2. #
  3. # get map from Python codec
  4. # return values:
  5. # r, e tables:
  6. # r: [ eord, uord ] table
  7. # e: list of warnings
  8. def getMap (enc, tip):
  9. if tip == 'SBCS':
  10. N = 256
  11. elif tip == 'DBCS':
  12. N = 65536
  13. else:
  14. assert False
  15. e = []
  16. r = []
  17. i = 0
  18. while i < N:
  19. try:
  20. if i < 256:
  21. x = chr(i).decode(enc).encode('utf-16le')
  22. else:
  23. x = (chr(i / 256) + chr(i % 256)).decode(enc).encode('utf-16le')
  24. except UnicodeDecodeError:
  25. # e.append( '\t\t\tcan not decode CHR(%d)=0%02XX from %s encoding' % (i, i, enc) )
  26. pass
  27. else:
  28. if len(x) == 2:
  29. x = ord(x[0]) + 256 * ord(x[1])
  30. r.append( (i, x) )
  31. else:
  32. e.append( '\t\t\tcan not encode CHR(%d)=0%02XX from %s encoding to UCS-2' % (i, i, enc) )
  33. i = i + 1
  34. return r, e