-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.c
More file actions
82 lines (73 loc) · 1.94 KB
/
Copy pathutil.c
File metadata and controls
82 lines (73 loc) · 1.94 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
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "util.h"
/* Limpa o resto da linha quando se usa scanf. */
void limparEnter(void) {
int c;
while ((c = getchar()) != '\n' && c != EOF) {
}
}
/* Le uma linha de texto e remove o enter final. */
void lerLinha(char texto[], int tamanho) {
if (fgets(texto, tamanho, stdin) != NULL) {
texto[strcspn(texto, "\n")] = '\0';
}
}
/* Mostra uma mensagem e le texto. */
void lerTexto(const char msg[], char destino[], int tamanho) {
printf("%s", msg);
lerLinha(destino, tamanho);
}
/* Le um numero inteiro com validacao simples. */
int lerInt(const char msg[]) {
int valor;
printf("%s", msg);
while (scanf("%d", &valor) != 1) {
limparEnter();
printf("Valor invalido. Tente outra vez: ");
}
limparEnter();
return valor;
}
/* Le um numero decimal com validacao simples. */
float lerFloat(const char msg[]) {
float valor;
printf("%s", msg);
while (scanf("%f", &valor) != 1) {
limparEnter();
printf("Valor invalido. Tente outra vez: ");
}
limparEnter();
return valor;
}
/* Le uma data no formato: dia mes ano. */
Data lerData(const char msg[]) {
Data d;
printf("%s (dia mes ano): ", msg);
while (scanf("%d %d %d", &d.dia, &d.mes, &d.ano) != 3) {
limparEnter();
printf("Data invalida. Escreva dia mes ano: ");
}
limparEnter();
return d;
}
/* Mostra uma data no ecra. */
void escreverData(Data d) {
printf("%02d/%02d/%04d", d.dia, d.mes, d.ano);
}
/* Transforma a data num numero para facilitar comparacoes e ordenacao. */
int valorData(Data d) {
return d.ano * 10000 + d.mes * 100 + d.dia;
}
/* Guarda uma linha no ficheiro de auditoria. */
void registarLog(const char texto[]) {
FILE *f = fopen(F_LOG, "a");
time_t agora;
if (f == NULL) {
return;
}
agora = time(NULL);
fprintf(f, "%s - %s", texto, ctime(&agora));
fclose(f);
}