Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MAIN.C
- #include "lista.h"
- int main()
- {
- Testa t = create(10,OnDimensionChangedCallback, OnSwitchActivatedCallback, ActionCallback);
- do{
- menu(t);
- sleep(2);
- system("clear");
- }while(1);
- return 0;
- }
- // LISTA.C
- #include "lista.h"
- pthread_t tf_1;
- pthread_t tf_2;
- pthread_t tf_3;
- int sec;
- Testa create(int dim, onDimensionChangedCallback evt1, onSwitchActivatedCallback evt2, onActionCallback evt3){
- Testa t = (Testa)malloc(sizeof(testa));
- t->dim = t->eff_dim = dim;
- t->l = build(dim);
- t->on = 0;
- time_t rtime;
- time(&rtime);
- struct tm * timeinfo = localtime(&rtime);
- sec = timeinfo->tm_sec;
- pthread_create(&tf_1,NULL, evt1, (void *)t);
- pthread_create(&tf_2,NULL, evt2, (void *)t);
- pthread_create(&tf_3,NULL, evt3, (void *)t);
- return t;
- }
- Lista build(int dim){
- int i;
- Lista head = NULL;
- for(i = 0; i < dim; i++)
- head = addhead(head,i);
- return head;
- }
- Lista addhead(Lista head, int i){
- Lista tail = (Lista)malloc(sizeof(lista));
- tail->x = i;
- tail->next = head;
- return tail;
- }
- void *onDimensionChanged(void *arg){
- Testa t = (Testa)arg;
- if(t->dim != t->eff_dim){
- puts("cambio dimensione!");
- t->dim = t->eff_dim;
- }
- return arg;
- }
- void *onActionPerformed(void *arg){
- Testa t = (Testa)arg;
- char *n;
- time_t rtime;
- time(&rtime);
- struct tm * timeinfo = localtime(&rtime);
- if((timeinfo->tm_sec - sec) >= 5){
- puts("[ STANDBY - premi Y per continuare... ]");
- timeinfo = localtime(&rtime);
- sec = timeinfo->tm_sec;
- }
- return arg;
- }
- void print(Testa t){
- Lista l = t->l;
- int n = (t->dim > t->eff_dim ? t->eff_dim : t->dim);
- while(l){
- printf("%d - ", l->x);
- l = l->next;
- n--;
- }
- printf("NULL\n");
- }
- void *OnDimensionChangedCallback(void *arg){
- do{
- pthread_create(&tf_1,NULL,onDimensionChanged,arg);
- }while(!pthread_join(tf_1,NULL));
- return arg;
- }
- void *OnSwitchActivatedCallback(void *arg){
- do{
- pthread_create(&tf_2,NULL,onSwitchActivated,arg);
- }while(!pthread_join(tf_2,NULL));
- return arg;
- }
- void *ActionCallback(void *arg){
- do{
- pthread_create(&tf_2,NULL,onActionPerformed,arg);
- }while(!pthread_join(tf_2,NULL));
- return arg;
- }
- void *onSwitchActivated(void *arg){
- Testa t = (Testa)arg;
- if(t->on == 1){
- t->on = 0;
- t = redim(t);
- }
- return arg;
- }
- Testa redim(Testa t){
- int i;
- t->l = NULL;
- for(i = 0; i < t->dim; i++)
- t->l = addhead(t->l,i);
- return t;
- }
- void menu(Testa t){
- int c;
- printf("Scegli:\n1 - cambia dimensione\n2 - stampa\n3 - ripopola in base alle dimensioni nuove\n> ");
- scanf("%d", &c);
- switch(c){
- case 1:
- printf("Nuova dimensione fittizia: ");
- scanf("%d", &(t->eff_dim));
- break;
- case 2:
- print(t);
- break;
- case 3:
- t->on = 1;
- break;
- }
- }
- // LISTA.H
- #ifndef _lis
- #define _lis
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <time.h>
- #include <unistd.h>
- typedef void *(*onDimensionChangedCallback)(void *);
- typedef void *(*onSwitchActivatedCallback)(void *);
- typedef void *(*onActionCallback)(void *);
- typedef struct lista{
- int x;
- struct lista *next;
- } lista, *Lista;
- typedef struct testa{
- int dim;
- int eff_dim;
- int on;
- Lista l;
- }testa, *Testa;
- Testa create(int dim, onDimensionChangedCallback evt1, onSwitchActivatedCallback evt2, onActionCallback evt3);
- Lista build(int dim);
- Lista addhead(Lista head, int i);
- void *onDimensionChanged(void *arg);
- void *onSwitchActivated(void *arg);
- void *ActionCallback(void *arg);
- void print(Testa l);
- void *OnSwitchActivatedCallback(void *arg);
- void *OnDimensionChangedCallback(void *arg);
- Testa redim(Testa t);
- void menu(Testa t);
- #endif // _lis
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement