ungureanuvladvictor

ncurses

Feb 10th, 2014
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.63 KB | None | 0 0
  1. /*
  2. 320232
  3. OSLab 2 2_1.c
  4. Vlad Ungureanu [email protected]
  5. */
  6.  
  7. #include <ncurses.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <time.h>
  11. #include <unistd.h>
  12.  
  13. static char *rand_string (char *str, size_t size) {
  14.     const char charset[] = "abcdefghijklmnoprstuvwxyzABDCEFGHIJKLMNOPRSTUVWXYZ";
  15.  
  16.     if (size) {
  17.         --size;
  18.         size_t n;
  19.         for (n = 0; n < size; n++) {
  20.             int key = rand() % (int) (sizeof charset -1);
  21.             str[n] = charset[key];
  22.         }
  23.         str[size] = '\0';
  24.     }
  25.     return str;
  26. }
  27.  
  28. static char *empty_string (char *str, size_t size) {
  29.     size_t n;
  30.     for (n = 0; n < size; n++)
  31.         str[n] = ' ';
  32.     str[size] = '\0';
  33.     return str;
  34. }
  35.  
  36. char* rand_string_alloc (size_t size) {
  37.     char *s = malloc (size + 1);
  38.     if (s) {
  39.         srand (time(NULL));
  40.         int tmp = rand() % 10;
  41.         if (tmp < 7)
  42.             rand_string(s, size);
  43.         else empty_string(s, size);
  44.     }
  45.     return s;
  46. }
  47.  
  48. void makeStrigs(char*** strings, int col) {
  49.     (*strings) = (char**)malloc(sizeof(char*) * col);
  50.     int x = 0;
  51.     int y = 0;
  52.    
  53.     if((*strings) == NULL) {
  54.         printf("Not good!");
  55.         exit(1);
  56.     }
  57.  
  58.     for(x = 0; x < col; x++) {
  59.         (*strings)[x] = (char*)malloc(sizeof(char) * 1000);
  60.        
  61.         if((*strings)[x] == NULL) {
  62.             printf("Not good!");
  63.             exit(1);
  64.         }
  65.        
  66.         for(y = 0; y < 1000; y++) {
  67.             if(x % 3 != 0)
  68.                 (*strings)[x][y] = rand() % 90 + 32;
  69.             else
  70.                 (*strings)[x][y] = ' ';
  71.         }
  72.     }
  73. }
  74.  
  75. void dealloc(char** strings, int col) {
  76.     int i;
  77.    
  78.     for(i = 0; i < col; i++)
  79.         free(strings[i]);
  80.    
  81.     free(strings);
  82. }
  83.  
  84. void print_matrix(char** strings, int col) {
  85.     int x, y, ct = 0;
  86.  
  87.     while(ct != col) {
  88.         for(x = 0; x < col; x++)
  89.             for(y = 0; y < ct; y++)
  90.                 mvprintw(y, x, "%c", strings[x][ct-y]);
  91.     ct++;
  92.     refresh();
  93.     usleep(50000);
  94.     }
  95. }
  96.  
  97. int main(int argc, char **argv) {
  98.     int row, col;
  99.     char** strings;
  100.     srand(time(NULL));
  101.     initscr();             /* start the curses mode */
  102.     start_color();                         /* needed to use colors */
  103.     init_pair(5, COLOR_GREEN, COLOR_BLACK); /* set foreground and background pair indexed as 5 */
  104.     getmaxyx(stdscr, row, col);      /* get the number of rows and columns */
  105.     attron(COLOR_PAIR(5));                 /* set color style to 5: magenta-black pair (see below) */
  106.     makeStrigs(&strings, col);
  107.     print_matrix(strings, col);
  108.     endwin();
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment