Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <string>
  4. #include <string.h>
  5. #include <windows.h>
  6. using namespace std;
  7.  
  8. char * a_to_u(char txt[]);
  9. char * u_to_a(char txt[]);
  10.  
  11.  
  12. int main(int argc, char *argv[]) {
  13.  
  14.     if (argc != 2) {
  15.         printf("Invalid arguments");
  16.         return 0;
  17.     }
  18.  
  19.     FILE *fp;
  20.     char path[100];
  21.     char text[5000];
  22.  
  23.     printf("Insert file path: ");
  24.     scanf("%[^\n]s", path);
  25.  
  26.     fp = fopen(path, "r");
  27.     if (fp) {
  28.         fgets(text, 1024, fp);
  29.         text[strlen(text)] = '\0';
  30.     } else {
  31.         printf("An error occurred while opening the file.\n");
  32.     }
  33.     fclose(fp);
  34.  
  35.     char *mod;
  36.     if (strcmp(argv[1], "-a") == 0)
  37.         mod = a_to_u(text);
  38.     else if (strcmp(argv[1], "-u") == 0)
  39.         mod = a_to_u(text);
  40.     else
  41.         printf("Invalid arguments");
  42.     return 0;
  43.  
  44.  
  45.     fp = fopen(path, "w");
  46.     if (fp) {
  47.         fprintf(fp, mod);
  48.     } else {
  49.         printf("An error occurred while opening the file.\n");
  50.     }
  51.     fclose(fp);
  52. }
  53.  
  54. char * a_to_u(char txt[]) {
  55.  
  56.     std::string str(txt);
  57.  
  58.     if (str.empty())
  59.         return char{};
  60.     int size_needed = MultiByteToWideChar(CP_ACP, 0, &str[0], (int)str.size(), NULL, 0);
  61.     std::wstring wstr(size_needed, 0);
  62.     MultiByteToWideChar(CP_ACP, 0, &str[0], (int)str.size(), &wstr[0], size_needed);
  63.  
  64.     size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
  65.     std::string strTo(size_needed, 0);
  66.     WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
  67.  
  68.     strcpy(txt, strTo.c_str());
  69.     return txt;
  70. }
  71.  
  72. char * u_to_a(char txt[]) {
  73.  
  74.     std::string str(txt);
  75.  
  76.     if (str.empty())
  77.         return char{};
  78.     int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
  79.     std::wstring wstr(size_needed, 0);
  80.     MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstr[0], size_needed);
  81.  
  82.     size_needed = WideCharToMultiByte(CP_ACP, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
  83.     std::string strTo(size_needed, 0);
  84.     WideCharToMultiByte(CP_ACP, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
  85.  
  86.     strcpy(txt, strTo.c_str());
  87.     return txt;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement