-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
89 lines (75 loc) · 2.47 KB
/
utils.c
File metadata and controls
89 lines (75 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <windows.h>
#include <time.h>
#include "structs.h"
#include "defines.h"
#include "utils.h"
//Toma un string y cambia todos los caracteres "reemplazado" por los "reemplazante"
void string_changechar(char str[], int largo, char reemplazado, char reemplazante){
for (int i = 0; i < largo; i++){
if (str[i] == reemplazado){
str[i] = reemplazante;
}
}
}
//Toma un string y lo pone en mayusculas
void string_toupper(char str[], int largo){
for (int i = 0; i < largo; i++){
if (str[i] == '\0') break;
if (str[i] == 'ñ') str[i] = 'Ñ';
if (str[i] == 164) str[i] = 165;
str[i] = toupper(str[i]); //Caracteres ingles
}
}
//Toma un string y lo pone en minusculas
void string_tolower(char str[], int largo){
for (int i = 0; i < largo; i++){
if (str[i] == '\0') break;
if (str[i] == 'Ñ') str[i] = 'ñ';
if (str[i] == 165) str[i] = 164;
str[i] = tolower(str[i]);
}
}
//agarra un string y devuelve la fecha
void get_date_string(char str[], char separator){
time_t t = time(NULL);
struct tm tiempo = *localtime(&t);
sprintf(str, "%02d%c%02d%c%04d", tiempo.tm_mday, separator, tiempo.tm_mon, separator, 1900+tiempo.tm_year); //String a devolver
}
//Funcion que solo deja ingresar un numero dentro de un rango.
float scan_num_range(char prompt[], float minimo, float maximo){
float num;
char input[32];
while (1){
printf("%s", prompt);
scanf("%31s", input);
fflush(stdin);
//Cambia las comas por puntos
string_changechar(input, strlen(input), ',', '.');
int valid = 1;
//Chequea si no ingreso letras o cosas asi
for (int i = 0; i < strlen(input); i++){
if ((!isdigit(input[i])) && (input[i] != '.')) valid = 0;
}
if (!valid){
set_text_color(12);
printf("[!] Valor invalido. Ingrese un numero entero / con coma. [E#2]\n");
set_text_color(7);
continue;
}
sscanf(input, "%f", &num);
if (num >= minimo && num <= maximo) break;
set_text_color(12);
printf("[!] Valor fuera de rango. Ingrese un valor valido. [E#5]\n");
set_text_color(7);
}
return num;
}
//Funcion que cambia el texto de color
void set_text_color(int color){
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, color);
}