Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

messages.C

Go to the documentation of this file.
00001 #include <string.h>
00002 #include "bcipc.h"
00003 #include "messages.h"
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <unistd.h>
00007 
00008 #include <libintl.h>
00009 #define _(String) gettext(String)
00010 #define gettext_noop(String) String
00011 #define N_(String) gettext_noop (String)
00012 
00013 Messages::Messages(int input_flag, int output_flag, int id)
00014 {
00015         if(id == -1)
00016         {
00017                 msgid = msgget(IPC_PRIVATE, IPC_CREAT | 0777);
00018                 client = 0;
00019         }
00020         else
00021         {
00022                 this->msgid = id;
00023                 client = 1;
00024         }
00025         
00026         this->input_flag = input_flag;
00027         this->output_flag = output_flag;
00028         bc_enter_msg_id(msgid);
00029 }
00030 
00031 Messages::~Messages()
00032 {
00033         if(!client)
00034         {
00035                 msgctl(msgid, IPC_RMID, NULL);
00036                 bc_remove_msg_id(msgid);
00037         }
00038 }
00039 
00040 char* Messages::get_message_buffer()
00041 {
00042         return buffer.text;
00043 }
00044 
00045 int Messages::read_message(char *text)
00046 {
00047         buffer.mtype = input_flag;
00048 
00049         if((msgrcv(msgid, (struct msgbuf*)&buffer, MESSAGESIZE, input_flag, 0)) < 0)
00050         {
00051                 printf(_("recieve message failed\n"));
00052                 sleep(1);     // don't flood the screen during the loop
00053                 return -1;
00054         }
00055 
00056 //printf("%d %d\n", buffer.text[0], buffer.mtype);
00057         strcpy(text, buffer.text);
00058         return 0;
00059 }
00060 
00061 long Messages::read_message()
00062 {
00063         buffer.mtype = input_flag;
00064         
00065         if((msgrcv(msgid, (struct msgbuf*)&buffer, MESSAGESIZE, input_flag, 0)) < 0)
00066         {
00067                 printf(_("recieve message failed\n"));
00068                 sleep(1);
00069                 return -1;
00070         }
00071         return atol(buffer.text);
00072 }
00073 
00074 float Messages::read_message_f()
00075 {
00076         float value;
00077         char *data = read_message_raw();
00078         sscanf(data, "%f", &value);
00079         return value;
00080 }
00081 
00082 char* Messages::read_message_raw()
00083 {
00084         buffer.mtype = input_flag;
00085         
00086         if((msgrcv(msgid, (struct msgbuf*)&buffer, MESSAGESIZE, input_flag, 0)) < 0)
00087         {
00088                 printf(_("recieve message failed\n"));
00089                 sleep(1);
00090                 return "RECIEVE MESSAGE FAILED";
00091         }
00092         else
00093                 return buffer.text;
00094 }
00095 
00096 int Messages::read_message(long *value1, long *value2)
00097 {
00098         char *data = read_message_raw();
00099         sscanf(data, "%ld %ld", value1, value2);
00100         return 0;
00101 }
00102 
00103 int Messages::read_message_f(float *value1, float *value2)
00104 {
00105         char *data = read_message_raw();
00106         sscanf(data, "%f %f", value1, value2);
00107         return 0;
00108 }
00109 
00110 int Messages::read_message(long *command, long *value1, long *value2)
00111 {
00112         char *data = read_message_raw();
00113         sscanf(data, "%ld %ld %ld", command, value1, value2);
00114         return 0;
00115 }
00116 
00117 int Messages::read_message(long *command, long *value1, long *value2, long *value3)
00118 {
00119         char *data = read_message_raw();
00120         sscanf(data, "%ld %ld %ld %ld", command, value1, value2, value3);
00121         return 0;
00122 }
00123 
00124 int Messages::read_message_f(float *value1, float *value2, float *value3)
00125 {
00126         char *data = read_message_raw();
00127         sscanf(data, "%f %f %f", value1, value2, value3);
00128         return 0;
00129 }
00130 
00131 int Messages::read_message_f(float *value1, float *value2, float *value3, float *value4)
00132 {
00133         char *data = read_message_raw();
00134         sscanf(data, "%f %f %f %f", value1, value2, value3, value4);
00135         return 0;
00136 }
00137 
00138 int Messages::read_message(int *command, char *text)
00139 {
00140         int i, j;
00141         
00142         char *data = read_message_raw();
00143         sscanf(data, "%d", command);
00144 // find the start of the text
00145         for(i = 0; i < MESSAGESIZE && data[i] != ' '; i++) { ; }
00146 // get the space
00147         i++;
00148 // copy the text part
00149         for(j = 0; (text[j] = data[i]) != 0; i++, j++) { ; }
00150         return 0;
00151 }
00152 
00153 
00154 int Messages::write_message(char *text)
00155 {
00156         buffer.mtype = output_flag;
00157         strcpy(buffer.text, text);
00158 
00159         if((msgsnd(msgid, (struct msgbuf*)&buffer, strlen(text) + 1, 0)) < 0) printf(_("send message failed\n"));
00160         return 0;
00161 }
00162 
00163 int Messages::write_message_raw()
00164 {
00165         buffer.mtype = output_flag;
00166 
00167         if((msgsnd(msgid, (struct msgbuf*)&buffer, strlen(buffer.text) + 1, 0)) < 0) printf(_("send message failed\n"));
00168         return 0;
00169 }
00170 
00171 int Messages::write_message_flagged(int output_flag, int command)
00172 {
00173         buffer.mtype = output_flag;
00174         sprintf(buffer.text, "%d", command);
00175         
00176         if((msgsnd(msgid, (struct msgbuf*)&buffer, strlen(buffer.text) + 1, 0)) < 0) printf(_("send message failed\n"));
00177         return 0;
00178 }
00179 
00180 int Messages::write_message(int number)
00181 {
00182         sprintf(buffer.text, "%d", number);
00183         buffer.mtype = output_flag;
00184         if((msgsnd(msgid, (struct msgbuf*)&buffer, strlen(buffer.text) + 1, 0)) < 0) perror(_("Messages::write_message"));
00185         return 0;
00186 }
00187 
00188 int Messages::write_message_f(float number)
00189 {
00190         sprintf(buffer.text, "%f", number);
00191         buffer.mtype = output_flag;
00192         if((msgsnd(msgid, (struct msgbuf*)&buffer, strlen(buffer.text) + 1, 0)) < 0) perror(_("Messages::write_message"));
00193         return 0;
00194 }
00195 
00196 int Messages::write_message(int command, char *text)
00197 {
00198         sprintf(buffer.text, "%d %s", command, text);
00199         return write_message_raw();
00200 }
00201 
00202 int Messages::write_message(long command, long value)
00203 {
00204         sprintf(buffer.text, "%ld %ld", command, value);
00205         return write_message_raw();
00206 }
00207 
00208 int Messages::write_message_f(float value1, float value2)
00209 {
00210         sprintf(buffer.text, "%f %f", value1, value2);
00211         return write_message_raw();
00212 }
00213 
00214 int Messages::write_message(long command, long value1, long value2)
00215 {
00216         sprintf(buffer.text, "%ld %ld %ld", command, value1, value2);
00217         return write_message_raw();
00218 }
00219 
00220 int Messages::write_message(long command, long value1, long value2, long value3)
00221 {
00222         sprintf(buffer.text, "%ld %ld %ld %ld", command, value1, value2, value3);
00223         return write_message_raw();
00224 }
00225 
00226 int Messages::write_message_f(float value1, float value2, float value3, float value4)
00227 {
00228         sprintf(buffer.text, "%f %f %f %f", value1, value2, value3, value4);
00229         return write_message_raw();
00230 }
00231 
00232 int Messages::write_message_f(float value1, float value2, float value3)
00233 {
00234         sprintf(buffer.text, "%f %f %f", value1, value2, value3);
00235         return write_message_raw();
00236 }
00237 
00238 int Messages::get_id()
00239 {
00240         return msgid;
00241 }

Generated on Sun Jan 8 13:38:57 2006 for Cinelerra-svn by  doxygen 1.4.4