Advertisement
mrAnderson33

перевод чисел в разные СС, отображение файлов в память( unix

Mar 18th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.60 KB | None | 0 0
  1. #include <sys/mman.h>
  2. #include <sys/stat.h>
  3. #include <sys/types.h>
  4. #include <fcntl.h>
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include<ctype.h>
  10.  
  11. int gorner(char*, unsigned);
  12. char * invertgorner(unsigned,unsigned );
  13. char * cc(char * , unsigned ,unsigned);
  14.  
  15. int main(int argc, char* argv[] )
  16. {
  17. if (argc < 3){
  18.                     fprintf(stderr,"please write the name files!\n");
  19.                     exit(1);
  20.                   }
  21.    
  22. int fd_in = open(argv[1],O_RDONLY);
  23. if (fd_in == -1)
  24. {
  25.     fprintf(stderr,"error! file with this name does not exist!\n ");
  26.         exit(1);
  27. }
  28. int fd_out = open(argv[2],O_CREAT|O_RDWR,0666);
  29.    
  30. char * p = (char *) mmap(0,255,PROT_READ,MAP_SHARED,fd_in , 0);
  31. if (p == MAP_FAILED) {
  32.     perror ("mmap");
  33.     exit(1);   
  34. }
  35. close(fd_in);
  36. ftruncate(fd_out,sizeof(char) *255 * 5 );
  37. char * file_out =(char *) mmap(0,sizeof(char) *255,PROT_WRITE,MAP_SHARED,fd_out,0);
  38. char * f = file_out;
  39.  
  40. char *num = (char *) malloc(sizeof(char) * 255);
  41. char  *n = num;
  42. char * buf = (char *) malloc(sizeof(char) * 255);
  43. unsigned base_in = 0;
  44. unsigned base_out=2;
  45. int len = 0;
  46. unsigned flag =0;
  47.  
  48. while(*p!=')')
  49. {
  50.         if (*p == '(') {flag = 1; p++;}
  51.         if (flag == 0) *n++=*p++;
  52.         else base_in = base_in*10 + *p++ -'0';
  53. }
  54. for(base_out ;base_out <= 16;base_out*=2)
  55.     {
  56.       sprintf(file_out,"%s(%u)=%s(%u); ",num,base_in,cc(num,base_in,base_out),base_out);
  57.     sprintf(buf,"%s(%u)=%s(%u); ",num,base_in,cc(num,base_in,base_out),base_out);
  58.     file_out+=strlen(buf);
  59.     len+=strlen(buf)+1;
  60.     *file_out++='\n';
  61.     if(base_out == 8)
  62.         {
  63.             base_out = 10;
  64.             sprintf(file_out,"%s(%u)=%s(%u);",num,base_in,cc(num,base_in,base_out),base_out);
  65.             sprintf(buf,"%s(%u)=%s(%u);",num,base_in,cc(num,base_in,base_out),base_out);
  66.         file_out+=strlen(buf);
  67.         *file_out++='\n';
  68.     len+=strlen(buf)+1;
  69.  
  70.             base_out = 8;
  71.  
  72.         }
  73. }
  74.  
  75. ftruncate(fd_out,len);
  76. close(fd_out); 
  77. munmap(file_out,0x100);
  78. munmap(p,0x100);
  79. return 0;
  80. }
  81.  
  82. int gorner(char * num , unsigned base)
  83. {
  84. int res = 0;
  85. while(*num) res =isdigit(*num) ? (res * base + (*num++) - '0') : (res * base + toupper(*num++))-'A'+10;
  86. return res;
  87. }
  88.  
  89. char * invertgorner(unsigned num, unsigned base)
  90. {
  91.     unsigned n = num;
  92.     char * r =  malloc (sizeof(char)* 256);
  93.     while (n)
  94.     {
  95.         r++;
  96.         n /= base;
  97.     }
  98.     while (num)
  99.     {
  100.         *--r = (((num % base) < 10) ? ((num % base) + '0') : ((num % base) - 10 + 'A'));
  101.         num /= base;
  102.     }
  103.     return r;
  104. }
  105.  
  106.  
  107. char * cc(char * num, unsigned base_in, unsigned base_out)
  108. {
  109.     if (base_in == 10)return invertgorner(gorner(num, base_in), base_out);
  110.     return invertgorner(gorner(num, base_in), base_out);
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement