split.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env python
  2. END_PREFIX = '} // namespace '
  3. TEST_PREFIX = '#if FARMHASHSELFTEST'
  4. def main():
  5. with open('farmhash.cc', 'rb') as input_file:
  6. lines = input_file.readlines()
  7. namespace_out = None
  8. prev_name = None
  9. output = open('common.h', 'wb')
  10. output.write('#pragma once\n\n')
  11. def write_common():
  12. output.write('#include "common.h"\n\n')
  13. def write_include(include):
  14. if include:
  15. output.write('namespace {\n')
  16. output.write(' ' + '#include "{}"\n'.format(include))
  17. output.write('}\n\n')
  18. write_test = False
  19. for line in lines:
  20. if line.startswith(TEST_PREFIX):
  21. write_test = True
  22. output.close()
  23. output = open('test.cc', 'wb')
  24. write_common()
  25. write_include('farmhash_iface.h')
  26. if write_test:
  27. output.write(line)
  28. continue
  29. elif line.startswith('namespace '):
  30. namespace = line.split(' ')[1]
  31. if namespace.startswith('farmhash'):
  32. filename = namespace + '.cc'
  33. output.close()
  34. output = open(filename, 'wb')
  35. write_common()
  36. write_include(prev_name)
  37. output.write(line)
  38. prev_name = filename
  39. else:
  40. if prev_name is not None:
  41. output.close()
  42. output = open('farmhash_iface.cc', 'wb')
  43. write_common()
  44. write_include(prev_name)
  45. prev_name = None
  46. output.write(line)
  47. else:
  48. output.write(line)
  49. output.close()
  50. if __name__ == '__main__':
  51. main()