File: lingot-msg.c

package info (click to toggle)
lingot 0.9.1-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 2,308 kB
  • ctags: 566
  • sloc: sh: 10,765; ansic: 3,878; makefile: 100
file content (91 lines) | stat: -rw-r--r-- 2,347 bytes parent folder | download
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
90
91
/*
 * lingot, a musical instrument tuner.
 *
 * Copyright (C) 2004-2011  Ibán Cereijo Graña, Jairo Chapela Martínez.
 *
 * This file is part of lingot.
 *
 * lingot is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * lingot is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with lingot; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>
#include <string.h>
#include <pthread.h>

#include "lingot-msg.h"

#define MAX_MESSAGES 5

char message[MAX_MESSAGES][1000];
message_type_t message_type[MAX_MESSAGES];

int front = 0, rear = 0;

pthread_mutex_t message_queue_mutex = PTHREAD_MUTEX_INITIALIZER;

void lingot_msg_add_error(const char* msg) {
	lingot_msg_add(msg, ERROR);
}

void lingot_msg_add_warning(const char* msg) {
	lingot_msg_add(msg, WARNING);
}

void lingot_msg_add_info(const char* msg) {
	lingot_msg_add(msg, INFO);
}

void lingot_msg_add(const char* msg, message_type_t type) {

	pthread_mutex_lock(&message_queue_mutex);
	if (front == ((rear + 1) % MAX_MESSAGES)) {
		perror("The messages array is full!\n");
	} else {
		// check if the message is already in the queue
		int duplicated = 0;
		int i = front;
		while (i != rear) {
			i = (i + 1) % MAX_MESSAGES;
			if (!strcmp(message[i], msg)) {
				duplicated = 1;
				printf("duplicated message: %s\n", msg);
				break;
			}
		}

		if (!duplicated) {
			rear = ((rear + 1) % MAX_MESSAGES);
			strcpy(message[rear], msg);
			message_type[rear] = type;
		}
	}
	pthread_mutex_unlock(&message_queue_mutex);
}

int lingot_msg_get(char** msg, message_type_t* type) {
	int result = 0;
	*msg = NULL;

	pthread_mutex_lock(&message_queue_mutex);
	if (front != rear) {
		front = (front + 1) % MAX_MESSAGES;
		*msg = strdup(message[front]);
		*type = message_type[front];
		result = 1;
	}
	pthread_mutex_unlock(&message_queue_mutex);

	return result;
}