Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.68 KB | None | 0 0
  1. /*
  2. ** bsq_part_one.c
  3. ** undefined
  4. ** File description:
  5. ** bsq_part_one.c
  6. */
  7.  
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <unistd.h>
  14. #include "bsq.h"
  15.  
  16. char **malloc_double_table(char *buff, int size);
  17.  
  18. int fs_open_file(char const *file_path)
  19. {
  20.     int open_file = 0;
  21.  
  22.     open_file = open(file_path, O_RDONLY);
  23.     if (open_file < 0)
  24.         my_putstr("ECHEC");
  25.     else {
  26.         my_putstr("SUCCESS");
  27.         my_putchar('\n');
  28.         read_file(file_path, open_file);
  29.     }
  30.     return (0);
  31. }
  32.  
  33. int read_file(char const *file_path, int open_file)
  34. {
  35.     struct stat file_stat;
  36.     long int size = 0;
  37.     char *buff;
  38.  
  39.     stat(file_path, &file_stat);
  40.     size = file_stat.st_size;
  41.     buff = malloc(sizeof(char) * (size + 1));
  42.     read(open_file, buff, size);
  43.     buff[size] = '\0';
  44.     //my_putstr(buff);
  45.     malloc_double_table(buff, size);
  46.     return (0);
  47. }
  48.  
  49. int cordoner_y(char *tab)
  50. {
  51.     int y = 0;
  52.  
  53.     for (int i = 0; tab[i]; i++) {
  54.         if (tab[i] == '\n')
  55.             y++;
  56.     }
  57.     return (y);
  58. }
  59.  
  60. int cordoner_x(char *tab)
  61. {
  62.     int i = 0;
  63.     // INTERPOLATION
  64.     for (i; tab[i] != '\n'; i++)
  65.         i++;
  66.     return (i);
  67. }
  68.  
  69. char *my_strcpy(char *dest, char const *src)
  70. {
  71.     int y = 0;
  72.     int x = 0;
  73.  
  74.     for (int i = 0; src[i] != '\0'; i++) {
  75.         if (src[i] == '\n' && y == 0) {
  76.         y++;
  77.         }
  78.         else if (y >= 1) {
  79.             dest[x] = src[i];
  80.             x++;
  81.         }
  82.     }
  83.     dest[x] = '\0';
  84.     return (dest);
  85. }
  86.  
  87. char **complet_double_tab(char **double_table, char *table)
  88. {
  89.     int y = 0;
  90.     int x = 0;
  91.  
  92.     for (int i = 0; table[i] != '\0'; i++) {
  93.         if (table[i] == '\n') {
  94.         x = 0;
  95.         y++;
  96.        }
  97.        else {
  98.         double_table[y][x] = table[i];
  99.         x++;
  100.        }
  101.     }
  102.     return (double_table);
  103. }
  104.  
  105.  
  106. char **malloc_double_table(char *buff, int size)
  107. {
  108.     char *new_tab = malloc(sizeof(char) * (size + 1));
  109.     new_tab = my_strcpy(new_tab,buff);
  110.     int x = cordoner_x(new_tab); // nombre de ligne = 33 caracte  x = 34 + 1
  111.     int y = cordoner_y(new_tab); // nombre de cologne y = 9 + 2x
  112.     char **double_table = malloc(sizeof(char*) * (y + 2));
  113.  
  114.     for (int i = 0; i <= y + 1; i++)
  115.         double_table[i] = malloc(sizeof(char) * (x + 1));
  116.     double_table[y + 1] = NULL;
  117.     double_table = complet_double_tab(double_table,new_tab); // completer le premier tableau. en char
  118.     copy_double_tab_bi(double_table, x, y); // paas besoin ppour créeer le double tab en int avec les valeurs inisilation
  119. }
  120.  
  121. int main(int argc, char const *argv[])
  122. {
  123.     fs_open_file(argv[1]);
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement