file.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #Implementing files class
  2. class files:
  3. '''initializing filename, pages, ptr, mainPage and pgOffset
  4. filename is initialized to an empty string
  5. pages is an empty dictionary
  6. mainPage consists of 1024 unintialized pages
  7. pgOffset is a list of 1024 uninitialized number of entries
  8. '''
  9. filename = ""
  10. pages = {}
  11. ptr = 0
  12. mainPage = [False] * 1024
  13. pgoffset = [0] * 1024
  14. namer = ""
  15. def __init__(self, name):
  16. self.namer = name
  17. def renameFile(self, newName):
  18. self.filename = newName
  19. def getName(self):
  20. return self.filename
  21. # Method to write to the main text file funner.dat
  22. def writeToFile(self, txt, pgno, offset):
  23. n = 1024
  24. # Customize file open location for your own self
  25. fopen = open("F:/labOS/funner.dat", "r+")
  26. # moves the pointer of file towards the page number specified, as each page consists of 1024 bytes to it is multiplied by 1024
  27. # the product is then added with offset to start writing to the file from the offset specified
  28. fopen.seek(pgno*1024 + offset)
  29. # if file exists then open
  30. if fopen:
  31. fopen.seek(pgno*1024+offset)
  32. # checks whether sum of offset and len(txt) is greater than 1024 means that it actually checks whether the data inputed
  33. # by the user is greater than 1024 bytes.
  34. # for example: the word "Hassan" contains 6 letters hence it contains 6 Bytes so it will false the following condition
  35. if offset + len(txt) > 1024:
  36. buffer = txt
  37. # writes to file to the buffer array from 0 index to the difference of 1024 and offset
  38. fopen.write(buffer[0:(1024 - offset)])
  39. self.mainPage[pgno] = True
  40. # loops till 1024 - size of Byte
  41. for i in range(n):
  42. # initialized tmppg no to the respective mainPage
  43. tmppgno = self.mainPage[i]
  44. # moves the pointer as of Byte size
  45. fopen.seek(tmppgno*1024)
  46. # Writes to the file by slicing such that it starts writing from difference of 1024-offset+1 and ends at the
  47. # position returned by the difference of len(txt) and 1024-offset
  48. fopen.write(buffer[1024-offset+1: len(txt) - (1024-offset)])
  49. self.pgoffset[i] = len(txt) - (1024-offset)
  50. self.mainPage[i] = True
  51. # else if the length of text entered by the user is less than 1024
  52. else:
  53. fopen.seek(pgno*1024+offset)
  54. buffer = txt
  55. fopen.write(buffer[0:len(txt)])
  56. self.pgoffset[pgno] = len(txt) +offset
  57. fopen.close()
  58. # method to truncate the file
  59. def truncae(self, pgno, maximumSize):
  60. buffer = [0]*maximumSize
  61. limit = 1024
  62. count = 0
  63. # opens the file for read and append
  64. f = open("F:/labOS/funner.dat", "r+")
  65. if f:
  66. # moves the pointer
  67. f.seek(pgno*1024+maximumSize)
  68. # instead of trimming the file it actually overwrites the file with empty spaces
  69. for i in range(limit):
  70. buffer[count] = " "
  71. f.write(str(buffer[count]))
  72. f.close()
  73. root = files("Main")
  74. #root.read(2,0,10)