Skip to content

Commit 8f1ee22

Browse files
committed
Material para la segunda clase
Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
1 parent e49cd95 commit 8f1ee22

27 files changed

+1484
-0
lines changed

ejemplos/1/main.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
int main(void) {
5+
int *valor;
6+
7+
valor = (int *)malloc(sizeof(int));
8+
printf("Valor del puntero después del malloc %p\n", &valor);
9+
10+
printf("Valor de inicio del puntero %d\n", *valor);
11+
*valor = 5000;
12+
printf("Valor modificado del puntero %d\n", *valor);
13+
14+
free(valor);
15+
return 0;
16+
}

ejemplos/2/coche.c

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#include "coche.h"
2+
#include <string.h>
3+
4+
struct coche {
5+
uint32_t id;
6+
const char *matricula;
7+
const char *marca;
8+
9+
uint32_t flags;
10+
};
11+
12+
struct coche *curso_coche_alloc(void)
13+
{
14+
return (struct coche *)malloc(sizeof(struct coche));
15+
}
16+
17+
void curso_coche_free(struct coche *c)
18+
{
19+
if (c->flags & (1 << CURSO_COCHE_ATTR_MARCA))
20+
xfree(c->marca);
21+
22+
if (c->flags & (1 << CURSO_COCHE_ATTR_MATRICULA))
23+
xfree(c->matricula);
24+
25+
xfree(c);
26+
}
27+
28+
bool curso_coche_attr_is_set(const struct coche *c, uint16_t attr)
29+
{
30+
return c->flags & (1 << attr);
31+
}
32+
33+
void curso_coche_attr_unset(struct coche *c, uint16_t attr)
34+
{
35+
if (!(c->flags & (1 << attr)))
36+
return;
37+
38+
switch (attr) {
39+
case CURSO_COCHE_ATTR_MARCA:
40+
if (c->marca) {
41+
xfree(c->marca);
42+
c->marca = NULL;
43+
}
44+
break;
45+
case CURSO_COCHE_ATTR_MATRICULA:
46+
if (c->matricula) {
47+
xfree(c->matricula);
48+
c->matricula = NULL;
49+
}
50+
break;
51+
case CURSO_COCHE_ATTR_ID:
52+
break;
53+
}
54+
55+
c->flags &= ~(1 << attr);
56+
}
57+
58+
void curso_coche_set_data(struct coche *c, uint16_t attr, const void *data,
59+
uint32_t data_len)
60+
{
61+
if (attr > CURSO_COCHE_ATTR_MAX)
62+
return;
63+
64+
switch (attr) {
65+
case CURSO_COCHE_ATTR_MARCA:
66+
if (c->marca)
67+
xfree(c->marca);
68+
69+
c->marca = strdup(data);
70+
break;
71+
case CURSO_COCHE_ATTR_MATRICULA:
72+
if (c->matricula)
73+
xfree(c->matricula);
74+
75+
c->matricula = strdup(data);
76+
break;
77+
case CURSO_COCHE_ATTR_ID:
78+
c->id = *((uint32_t *)data);
79+
break;
80+
}
81+
82+
c->flags |= (1 << attr);
83+
}
84+
85+
void curso_coche_attr_set_u32(struct coche *c, uint16_t attr, uint32_t data)
86+
{
87+
curso_coche_set_data(c, attr, &data, sizeof(uint32_t));
88+
}
89+
90+
void curso_coche_attr_set_str(struct coche *c, uint16_t attr, const char *data)
91+
{
92+
curso_coche_set_data(c, attr, data, 0);
93+
}
94+
95+
const void *curso_coche_attr_get_data(struct coche *c, uint16_t attr)
96+
{
97+
if (!(c->flags & (1 << attr)))
98+
return NULL;
99+
100+
switch(attr) {
101+
case CURSO_COCHE_ATTR_MARCA:
102+
return c->marca;
103+
case CURSO_COCHE_ATTR_MATRICULA:
104+
return c->matricula;
105+
case CURSO_COCHE_ATTR_ID:
106+
return &c->id;
107+
}
108+
return NULL;
109+
}
110+
111+
uint32_t curso_coche_attr_get_u32(struct coche *c, uint16_t attr)
112+
{
113+
const void *ret = curso_coche_attr_get_data(c, attr);
114+
return ret == NULL ? 0 : *((uint32_t *)ret);
115+
}
116+
117+
const char *curso_coche_attr_get_str(struct coche *c, uint16_t attr)
118+
{
119+
return curso_coche_attr_get_data(c, attr);
120+
}
121+
122+
int curso_coche_snprintf(char *buf, size_t size, struct coche *c)
123+
{
124+
return snprintf(buf, size, "matricula %s marca %s id %d",
125+
c->matricula, c->marca, c->id);
126+
}

ejemplos/2/coche.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <stdio.h>
2+
#include <stdint.h>
3+
#include <stdlib.h>
4+
#include <stdbool.h>
5+
6+
enum {
7+
CURSO_COCHE_ATTR_MARCA,
8+
CURSO_COCHE_ATTR_MATRICULA,
9+
CURSO_COCHE_ATTR_ID,
10+
__CURSO_COCHE_ATTR_MAX
11+
};
12+
13+
#define CURSO_COCHE_ATTR_MAX (__CURSO_COCHE_ATTR_MAX - 1)
14+
15+
#define xfree(ptr) free((void *)ptr);
16+
17+
struct coche;
18+
19+
struct coche *curso_coche_alloc(void);
20+
void curso_coche_free(struct coche *);
21+
22+
bool curso_coche_attr_is_set(const struct coche *c, uint16_t attr);
23+
24+
void curso_coche_attr_unset(struct coche *c, uint16_t attr);
25+
26+
void curso_coche_attr_set_data(struct coche *c, uint16_t attr, const void *data,
27+
uint32_t data_len);
28+
void curso_coche_attr_set_u32(struct coche *c, uint16_t attr, uint32_t data);
29+
void curso_coche_attr_set_str(struct coche *c, uint16_t attr, const char *data);
30+
31+
const void *curso_coche_attr_get_data(struct coche *c, uint16_t attr);
32+
uint32_t curso_coche_attr_get_u32(struct coche *c, uint16_t attr);
33+
const char *curso_coche_attr_get_str(struct coche *c, uint16_t attr);
34+
35+
int curso_coche_snprintf(char *buf, size_t size, struct coche *c);

ejemplos/2/main.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "coche.h"
2+
3+
int main(void)
4+
{
5+
struct coche *c1;
6+
char info[250];
7+
8+
c1 = curso_coche_alloc();
9+
10+
curso_coche_attr_set_str(c1, CURSO_COCHE_ATTR_MATRICULA, "DF 67838 SE");
11+
curso_coche_attr_set_str(c1, CURSO_COCHE_ATTR_MARCA, "mini");
12+
curso_coche_attr_set_u32(c1, CURSO_COCHE_ATTR_ID, 0);
13+
14+
curso_coche_snprintf(info, sizeof(info), c1);
15+
printf("los valores del coche son %s\n", info);
16+
17+
curso_coche_free(c1);
18+
return 0;
19+
}

ejemplos/3/coche.c

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#include "coche.h"
2+
#include <string.h>
3+
4+
struct coche {
5+
uint32_t id;
6+
const char *matricula;
7+
const char *marca;
8+
9+
uint32_t flags;
10+
};
11+
12+
struct coche *curso_coche_alloc(void)
13+
{
14+
return (struct coche *)malloc(sizeof(struct coche));
15+
}
16+
17+
void curso_coche_free(struct coche *c)
18+
{
19+
if (c->flags & (1 << CURSO_COCHE_ATTR_MARCA))
20+
xfree(c->marca);
21+
22+
if (c->flags & (1 << CURSO_COCHE_ATTR_MATRICULA))
23+
xfree(c->matricula);
24+
25+
xfree(c);
26+
}
27+
28+
bool curso_coche_attr_is_set(const struct coche *c, uint16_t attr)
29+
{
30+
return c->flags & (1 << attr);
31+
}
32+
33+
void curso_coche_attr_unset(struct coche *c, uint16_t attr)
34+
{
35+
if (!(c->flags & (1 << attr)))
36+
return;
37+
38+
switch (attr) {
39+
case CURSO_COCHE_ATTR_MARCA:
40+
if (c->marca) {
41+
xfree(c->marca);
42+
c->marca = NULL;
43+
}
44+
break;
45+
case CURSO_COCHE_ATTR_MATRICULA:
46+
if (c->matricula) {
47+
xfree(c->matricula);
48+
c->matricula = NULL;
49+
}
50+
break;
51+
case CURSO_COCHE_ATTR_ID:
52+
break;
53+
}
54+
55+
c->flags &= ~(1 << attr);
56+
}
57+
58+
void curso_coche_set_data(struct coche *c, uint16_t attr, const void *data,
59+
uint32_t data_len)
60+
{
61+
if (attr > CURSO_COCHE_ATTR_MAX)
62+
return;
63+
64+
switch (attr) {
65+
case CURSO_COCHE_ATTR_MARCA:
66+
if (c->marca)
67+
xfree(c->marca);
68+
69+
c->marca = strdup(data);
70+
break;
71+
case CURSO_COCHE_ATTR_MATRICULA:
72+
if (c->matricula)
73+
xfree(c->matricula);
74+
75+
c->matricula = strdup(data);
76+
break;
77+
case CURSO_COCHE_ATTR_ID:
78+
c->id = *((uint32_t *)data);
79+
break;
80+
}
81+
82+
c->flags |= (1 << attr);
83+
}
84+
85+
void curso_coche_attr_set_u32(struct coche *c, uint16_t attr, uint32_t data)
86+
{
87+
curso_coche_set_data(c, attr, &data, sizeof(uint32_t));
88+
}
89+
90+
void curso_coche_attr_set_str(struct coche *c, uint16_t attr, const char *data)
91+
{
92+
curso_coche_set_data(c, attr, data, 0);
93+
}
94+
95+
const void *curso_coche_attr_get_data(struct coche *c, uint16_t attr)
96+
{
97+
if (!(c->flags & (1 << attr)))
98+
return NULL;
99+
100+
switch(attr) {
101+
case CURSO_COCHE_ATTR_MARCA:
102+
return c->marca;
103+
case CURSO_COCHE_ATTR_MATRICULA:
104+
return c->matricula;
105+
case CURSO_COCHE_ATTR_ID:
106+
return &c->id;
107+
}
108+
return NULL;
109+
}
110+
111+
uint32_t curso_coche_attr_get_u32(struct coche *c, uint16_t attr)
112+
{
113+
const void *ret = curso_coche_attr_get_data(c, attr);
114+
return ret == NULL ? 0 : *((uint32_t *)ret);
115+
}
116+
117+
const char *curso_coche_attr_get_str(struct coche *c, uint16_t attr)
118+
{
119+
return curso_coche_attr_get_data(c, attr);
120+
}
121+
122+
int curso_coche_snprintf(char *buf, size_t size, struct coche *c)
123+
{
124+
return snprintf(buf, size, "matricula %s marca %s id %d",
125+
c->matricula, c->marca, c->id);
126+
}

ejemplos/3/coche.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <stdio.h>
2+
#include <stdint.h>
3+
#include <stdlib.h>
4+
#include <stdbool.h>
5+
6+
enum {
7+
CURSO_COCHE_ATTR_MARCA,
8+
CURSO_COCHE_ATTR_MATRICULA,
9+
CURSO_COCHE_ATTR_ID,
10+
__CURSO_COCHE_ATTR_MAX
11+
};
12+
13+
#define CURSO_COCHE_ATTR_MAX (__CURSO_COCHE_ATTR_MAX - 1)
14+
15+
#define xfree(ptr) free((void *)ptr);
16+
17+
struct coche;
18+
19+
struct coche *curso_coche_alloc(void);
20+
void curso_coche_free(struct coche *);
21+
22+
bool curso_coche_attr_is_set(const struct coche *c, uint16_t attr);
23+
24+
void curso_coche_attr_unset(struct coche *c, uint16_t attr);
25+
26+
void curso_coche_attr_set_data(struct coche *c, uint16_t attr, const void *data,
27+
uint32_t data_len);
28+
void curso_coche_attr_set_u32(struct coche *c, uint16_t attr, uint32_t data);
29+
void curso_coche_attr_set_str(struct coche *c, uint16_t attr, const char *data);
30+
31+
const void *curso_coche_attr_get_data(struct coche *c, uint16_t attr);
32+
uint32_t curso_coche_attr_get_u32(struct coche *c, uint16_t attr);
33+
const char *curso_coche_attr_get_str(struct coche *c, uint16_t attr);
34+
35+
int curso_coche_snprintf(char *buf, size_t size, struct coche *c);

0 commit comments

Comments
 (0)