I'll think about this, it sounds like a good idea indeed.
(1+((text_len-1)/4))*4
char Text[240]; // 0 to 240 characters of text
import socket
import struct
sd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sd.connect(('127.0.0.1', 29999))
# Init InSim.
isi = struct.pack('4B2HBcH15sx15sx',
44, # Size
1, # Type
0, # ReqI
0, # Zero
0, # UDPPort
0, # Flags
0, # Sp0
'\x00', # Prefix
0, # Interval
'', # Admin
'Test',) # IName
sd.send(isi)
# Send button with eight chars of text (displayed)
btn = struct.pack('12B8s',
20, # Size
45, # Type
1, # ReqI
255, # UCID
1, # ClickID
0, # Inst
0, # BStyle
0, # TypeIn
20, # L
20, # T
40, # W
10, # H
'12345678',) # Eight chars long
sd.send(btn)
# Create a str 240 chars long.
text = ''.join(['a' for i in xrange(0, 240)]) # Create str.
# Send button with exactly 240 chars (not displayed)
btn = struct.pack('12B240s',
252, # Size
45, # Type
1, # ReqI
255, # UCID
2, # ClickID
0, # Inst
0, # BStyle
0, # TypeIn
20, # L
40, # T
40, # W
10, # H
text,) # 240 chars long
sd.send(btn)
# Create str 240 chars with trailing zero
text = ''.join(['a' for i in xrange(0, 239)]) + '\x00'
# Send button 240 chars with trailing zero (displayed)
btn = struct.pack('12B240s',
252, # Size
45, # Type
1, # ReqI
255, # UCID
3, # ClickID
0, # Inst
0, # BStyle
0, # TypeIn
20, # L
60, # T
40, # W
10, # H
text,) # 240 chars with null terminator
sd.send(btn)
while True:
pass
struct IS_BTN btn_pack;
memset(&btn_pack, 1, sizeof(IS_BTN)); //Memset to 1 to not have a zero terminator at the end "automatically"
btn_pack.Type = ISP_BTN;
btn_pack.ReqI = 1;
btn_pack.UCID = 255;
btn_pack.ClickID = 1;
btn_pack.L = 1;
btn_pack.T = 1;
btn_pack.W = 240;
btn_pack.H = 10;
char* text = /****/
memcpy(btn_pack.Text, text, strlen(text)); //Copy just the text, NOT the terminator
btn_pack.Text[strlen(text)] = '\0';
insim.send_button(&btn_pack, strlen(text) + 1); //Modified func form CInsim, second parameter is the length of the text to send
int CInsim::send_packet(void* s_packet)
{
pthread_mutex_lock (&ismutex);
//Detect packet type
switch(*((unsigned char*)s_packet+1))
{
case ISP_BTN:
{
struct IS_BTN* pack = (struct IS_BTN*)s_packet;
unsigned char text_len = strlen(pack->Text);
/*TODO: Should we truncate the string if it's too long
* or should we just discard the packet?
* If we discard the packet, perhaps another
* return code should be used. */
if(text_len > IS_BTN_MAXTLEN)
return -1;
unsigned char text2send;
unsigned char remdr = text_len % 4;
if(remdr == 0)
text2send = text_len;
else
text2send = text_len + 4 - remdr;
pack->Size = IS_BTN_HDRSIZE + text2send;
}
break;
case ISP_MTC:
{
struct IS_MTC* pack = (struct IS_MTC*)s_packet;
unsigned char text_len = strlen(pack->Text);
//Same as above
if(text_len > IS_MTC_MAXTLEN)
return -1;
unsigned char text2send = text_len + 4 - text_len % 4;
pack->Size = IS_MTC_HDRSIZE + text2send;
}
break;
default:
break;
}
if (send(sock, (const char *)s_packet, *((unsigned char*)s_packet), 0) < 0)
{
pthread_mutex_unlock (&ismutex);
return -1;
}
pthread_mutex_unlock (&ismutex);
return 0;
}
char c[3];
memcpy(c, "XXXXXXXX", 8);
std::string c;
c = "XXXXXXXX";
char c[10];
std::cin >> c;
std::cout << c << std::endl;