hoome

Some utils to C

Aug 3rd, 2013
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.57 KB | None | 0 0
  1. // myutils.h
  2. // Utils to handle C, make everything easly.
  3. // made by Artur 'hoOmE' Paiva <[email protected]>
  4. // Under GPLv3 - http://www.gnu.org/licenses/gpl.html
  5.  
  6. // Define your os, can be "linux" or "windows".
  7. char OS[] = "linux";
  8.  
  9. // Include basic input/output library and system utils.
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13.  
  14. // Declare some vars
  15. int null = NULL;
  16. int none = NULL;
  17. int false = 0;
  18. int true = 1;
  19.  
  20. // Function to clear the screen
  21. int clear() {
  22.     int returned = 1;
  23.     if (!strcmp(OS,"linux")) {
  24.         returned = system("clear");
  25.     } else if (!strcmp(OS,"windows")) {
  26.         returned = system("cls");
  27.     }
  28.     return(returned);
  29. }
  30.  
  31. // Structure to a simple string
  32. typedef struct String {
  33.     char *string;
  34.     int size;
  35. } string;
  36.  
  37. // Function to get a input
  38. char * input(char *text,int size) {
  39.     printf("%s",text);
  40.     size+=2;
  41.     char input[size];
  42.     fflush(stdin);
  43.     fgets(input,size,stdin);
  44.     char *input2 = malloc(size);
  45.     strcpy(input2,input);
  46.     strtok(input2, "\n");
  47.     return(input2);
  48. }
  49.  
  50. // Function to convert to int
  51. int toint(char *value) {
  52.     return(atoi(value));
  53. }
  54.  
  55. // Function to convert to char array
  56. char *tostring(int value) {
  57.     char *str = malloc(1);
  58.     sprintf(str, "%d", value);
  59.     return(str);
  60. }
  61.  
  62. // Function to print text
  63. int print(char *value) {
  64.     printf("%s\n",value);
  65. }
  66.  
  67. // Function to make a delay
  68. void delay(int time) {
  69.     if(!strcmp(OS,"linux")) {
  70.         sleep(time);
  71.     } else if (!strcmp(OS,"windows")) {
  72.         sleep(time*1000);
  73.     }
  74. }
  75.  
  76. // Function to pause
  77. int pause() {
  78.     int returned = 1;
  79.     if(!strcmp(OS,"linux")) {
  80.         printf("\nPressione qualquer tecla para continuar...\n");
  81.         returned = system("read null");
  82.     } else if (!strcmp(OS,"windows")) {
  83.         printf("\n");
  84.         returned = system("pause");
  85.     }
  86.     return(returned);
  87. }
  88.  
  89. // Function to create a menu
  90. int menu(char *titulo,char *items[],int opcoes) {
  91.     int counter = 0;
  92.     if (opcoes > 9) {
  93.         opcoes = 9;
  94.     }
  95.     if (opcoes < 1) {
  96.         opcoes = 1;
  97.     }
  98.     int resultado = 0;
  99.     clear();
  100.     if (!strcmp(OS,"linux")) {
  101.         printf("\033[1;37m\t%s\t\n\n",titulo);
  102.     } else {
  103.         printf("\t%s\t\n\n",titulo);
  104.     }
  105.     while (counter < opcoes) {
  106.         if (!strcmp(OS,"linux")) {
  107.             printf("\033[1;33m%d =>\033[1;37m %s\n",counter+1,items[counter]);
  108.         } else if (!strcmp(OS,"windows")) {
  109.             printf("%d => %s",counter+1,items[counter]);
  110.         }
  111.         counter++;
  112.     }
  113.     resultado = toint(input("\nType an option > \033[0m",1));
  114.     if (resultado > opcoes || resultado < 1) {
  115.         print("You typed a unvalid option!");
  116.         pause();
  117.         menu(titulo,items,opcoes);
  118.     } else {
  119.         return(resultado);
  120.     }
  121. }
  122.  
  123. // Function to ajust the screen
  124. void screenfix() {
  125.     if (!strcmp(OS,"linux")) {
  126.         printf("\033[1;37m");
  127.     } else if (!strcmp(OS,"windows")) {
  128.         system("color 1F");
  129.     }
  130. }
  131.  
  132. // Print a vector
  133. void print_r(int *vetor,int size,char separator) {
  134.     int counter = 0;
  135.     while (counter < size) {
  136.         printf("%d%c",vetor[counter],separator);
  137.         counter++;
  138.     }
  139. }
  140.  
  141. // Dump the values from a vector
  142. void vardump(int *vetor, int size) {
  143.     int counter = 0;
  144.     printf("Array %p {\n",&vetor);
  145.     while (counter < size) {
  146.         printf("\t%d => %d\n",counter,vetor[counter]);
  147.         counter++;
  148.     }
  149.     printf("}\n");
  150. }
  151.  
  152. // Function that just "pass", useless in most cases, but here is
  153. void pass() {
  154.     printf("");
  155. }
Advertisement
Add Comment
Please, Sign In to add comment