symbian-qemu-0.9.1-12/python-2.6.1/Mac/Demo/example0/checktext.py
changeset 1 2fb8b9db1c86
equal deleted inserted replaced
0:ffa851df0825 1:2fb8b9db1c86
       
     1 """checktext - Check that a text file has macintosh-style newlines"""
       
     2 
       
     3 import sys
       
     4 import EasyDialogs
       
     5 import string
       
     6 
       
     7 def main():
       
     8     pathname = EasyDialogs.AskFileForOpen(message='File to check end-of-lines in:')
       
     9     if not pathname:
       
    10         sys.exit(0)
       
    11     fp = open(pathname, 'rb')
       
    12     try:
       
    13         data = fp.read()
       
    14     except MemoryError:
       
    15         EasyDialogs.Message('Sorry, file is too big.')
       
    16         sys.exit(0)
       
    17     if len(data) == 0:
       
    18         EasyDialogs.Message('File is empty.')
       
    19         sys.exit(0)
       
    20     number_cr = string.count(data, '\r')
       
    21     number_lf = string.count(data, '\n')
       
    22     if number_cr == number_lf == 0:
       
    23         EasyDialogs.Message('File contains no lines.')
       
    24     if number_cr == 0:
       
    25         EasyDialogs.Message('File has unix-style line endings')
       
    26     elif number_lf == 0:
       
    27         EasyDialogs.Message('File has mac-style line endings')
       
    28     elif number_cr == number_lf:
       
    29         EasyDialogs.Message('File probably has MSDOS-style line endings')
       
    30     else:
       
    31         EasyDialogs.Message('File has no recognizable line endings (binary file?)')
       
    32     sys.exit(0)
       
    33 
       
    34 if __name__ == '__main__':
       
    35     main()