1 /*
2  * lingot, a musical instrument tuner.
3  *
4  * Copyright (C) 2004-2018  Iban Cereijo.
5  * Copyright (C) 2004-2008  Jairo Chapela.
6 
7  *
8  * This file is part of lingot.
9  *
10  * lingot is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * lingot is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with lingot; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24 
25 #include <stdio.h>
26 #include <string.h>
27 #include <pthread.h>
28 
29 #include "lingot-msg.h"
30 
31 #define MAX_MESSAGES 	5
32 
33 char message[MAX_MESSAGES][1000];
34 message_type_t message_type[MAX_MESSAGES];
35 int error_codes[MAX_MESSAGES];
36 
37 int front = 0, rear = 0;
38 
39 pthread_mutex_t message_queue_mutex = PTHREAD_MUTEX_INITIALIZER;
40 
lingot_msg_add_error(const char * msg)41 void lingot_msg_add_error(const char* msg) {
42 	lingot_msg_add(msg, ERROR, 0);
43 }
44 
lingot_msg_add_error_with_code(const char * msg,int error_code)45 void lingot_msg_add_error_with_code(const char* msg, int error_code) {
46 	lingot_msg_add(msg, ERROR, error_code);
47 }
48 
lingot_msg_add_warning(const char * msg)49 void lingot_msg_add_warning(const char* msg) {
50 	lingot_msg_add(msg, WARNING, 0);
51 }
52 
lingot_msg_add_info(const char * msg)53 void lingot_msg_add_info(const char* msg) {
54 	lingot_msg_add(msg, INFO, 0);
55 }
56 
lingot_msg_add(const char * msg,message_type_t type,int error_code)57 void lingot_msg_add(const char* msg, message_type_t type, int error_code) {
58 
59 	pthread_mutex_lock(&message_queue_mutex);
60 	if (front == ((rear + 1) % MAX_MESSAGES)) {
61 		fprintf(stderr, "warning: the messages queue is full!\n");
62 	} else {
63 		// check if the message is already in the queue
64 		int duplicated = 0;
65 		int i = front;
66 		while (i != rear) {
67 			i = (i + 1) % MAX_MESSAGES;
68 			if (!strcmp(message[i], msg)) {
69 				duplicated = 1;
70 				fprintf(stderr, "warning: duplicated message: %s\n", msg);
71 				break;
72 			}
73 		}
74 
75 		if (!duplicated) {
76 			rear = ((rear + 1) % MAX_MESSAGES);
77 			strcpy(message[rear], msg);
78 			message_type[rear] = type;
79 			error_codes[rear] = error_code;
80 
81 			if (type != INFO) {
82 				fprintf(stderr, "%s: %s\n",
83 						(message_type == ERROR) ? "error" : "warning", msg);
84 					}
85 				}
86 			}
87 	pthread_mutex_unlock(&message_queue_mutex);
88 }
89 
lingot_msg_get(char ** msg,message_type_t * type,int * error_code)90 int lingot_msg_get(char** msg, message_type_t* type, int* error_code) {
91 	int result = 0;
92 	*msg = NULL;
93 
94 	pthread_mutex_lock(&message_queue_mutex);
95 	if (front != rear) {
96 		front = (front + 1) % MAX_MESSAGES;
97 		*msg = strdup(message[front]);
98 		*type = message_type[front];
99 		*error_code = error_codes[front];
100 		result = 1;
101 	}
102 	pthread_mutex_unlock(&message_queue_mutex);
103 
104 	return result;
105 }
106