main.c 888 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "pocketpy.h"
  4. #include "pocketpy/pocketpy.h"
  5. char* read_file(const char* path) {
  6. FILE* file = fopen(path, "r");
  7. if(file == NULL) {
  8. printf("Error: file not found\n");
  9. return NULL;
  10. }
  11. fseek(file, 0, SEEK_END);
  12. long size = ftell(file);
  13. fseek(file, 0, SEEK_SET);
  14. char* buffer = malloc(size + 1);
  15. fread(buffer, 1, size, file);
  16. buffer[size] = 0;
  17. return buffer;
  18. }
  19. int main(int argc, char** argv) {
  20. #if _WIN32
  21. SetConsoleCP(CP_UTF8);
  22. SetConsoleOutputCP(CP_UTF8);
  23. #endif
  24. if(argc != 2) goto __HELP;
  25. char* source = read_file(argv[1]);
  26. py_initialize();
  27. if(py_exec_simple(source)){
  28. py_Error* err = py_getlasterror();
  29. py_Error__print(err);
  30. }
  31. py_finalize();
  32. free(source);
  33. __HELP:
  34. printf("Usage: pocketpy [filename]\n");
  35. return 0;
  36. }