📄 linksocket.c
字号:
int ReadChar(LinkSocket *socket, char *c){ return ReadChars(socket, c, 1);}int WriteChars(LinkSocket *socket, char buffer[], int nchartowrite){ int nbyteswritten; if(Debug(LINKSOCKET)) { printf("LinkSocket.c WriteChars-- %s %d\n", buffer, nchartowrite); } if (nchartowrite == 0){ socket->error = LINK_FALSE; return OK; } if((LINK_TRUE == socket->isBuffered) && (OK == WriteToBuffer(socket, nchartowrite)) ) { nbyteswritten = BufferedWriteChars(&(socket->writebuffer[socket->currentbuffer]), buffer, nchartowrite); socket->currentbuffer += nbyteswritten; } else nbyteswritten = socket_send(socket->sd, buffer, nchartowrite); if(nchartowrite == nbyteswritten) { socket->error = LINK_FALSE; return OK; } /*system error */ socket->error = LINK_TRUE; return NOK;}int WriteChar(LinkSocket *socket, char *c){ return WriteChars(socket, c, 1);}int BufferedWriteChars(char *toBuffer, char *fromBuffer, int nchartowrite){ memcpy(toBuffer,fromBuffer,nchartowrite); /* int i = 0; while(i < nchartowrite) { toBuffer[i] = fromBuffer[i]; i++; } */ return nchartowrite;}/* * bmackiew: This function originally translated the Unicode characters * to ASCII for use by the calling function. Since we deal with stored * strings in Unicode only, there's no need to do the translation. */int ReadUnicodeChars(LinkSocket *socket, char *buffer, int nchartoread){ int ncharread = 0; int i; char *inbuffer; /* * ### bmackiew: Make sure we read in the proper number of characters! */ /* bmackiew: The numbers of characters to read was originally multiplied * by two since strings were stored in ASCII. We've stored them as * Unicode already, so we know how many characters were needed for * storage and readback. */#if 0 nchartoread = nchartoread*2;#endif inbuffer = (char *)malloc(sizeof(char) * nchartoread); if (inbuffer == NULL) return -1; /* Memory allocation error */ ncharread = socket_receive(socket->sd, inbuffer, nchartoread); if(nchartoread == ncharread) { /* * This was originally done by a strcpy(), but that fails with * English Unicode characters. */ for (i = 0; i < nchartoread; i++) { buffer[i] = inbuffer[i]; } buffer[ncharread] = '\0'; free(inbuffer); socket->error = LINK_FALSE; return OK; } /*error condition */ free(inbuffer); socket->error = LINK_TRUE; return NOK;}/* * bmackiew: Modified to not use ASCIIToUnicode(), as by the time * characters are sent here, they are already in Unicode. */int WriteUnicodeChars(LinkSocket *socket, char buffer[], int nchartowrite){ int ncharwritten = 0; /*Internal buffer for holding unicode characters*/ char *inbuffer; if (nchartowrite == 0){ socket->error = LINK_FALSE; return OK; } inbuffer = (char *)malloc(nchartowrite); if (inbuffer == NULL) return -1; /* Memory allocation error */ if((LINK_TRUE == socket->isBuffered) && (OK == WriteToBuffer(socket, nchartowrite)) ) { ncharwritten = BufferedWriteUnicodeChars(&(socket->writebuffer[socket->currentbuffer]), buffer, nchartowrite); socket->currentbuffer += ncharwritten; nchartowrite = N_CHARS_PER_UNICODE_CHAR * nchartowrite; } else { ncharwritten = socket_send(socket->sd, buffer, nchartowrite); } if(nchartowrite == ncharwritten){ /*error condition */ socket->error = LINK_FALSE; free(inbuffer); return OK; } socket->error = LINK_TRUE; free(inbuffer); return NOK;}int BufferedWriteUnicodeChars(char *toBuffer, char *fromBuffer, int nchartowrite){ return ASCIIToUnicode(fromBuffer,toBuffer, nchartowrite);}/* * Converts Unicode characters to _English_ ASCII characters by * stripping them of their first byte. Note that this is not a * comprehensive solution for other language stored in ASCII. * */char *EnglishUnicodeToASCII(char unicodechar[], int len) { int i, length; char *cchar; cchar = (char *)malloc(length/2); for( i = 0; i< length/2;i++){ cchar[i] = unicodechar[i*2 + 1]; } return cchar;}/* * Converts ASCII characters to Unicode Chars by prefixing them with 0x00; */char *ASCIIToEnglishUnicode(char cchar[]) { int i, length; char *unicodechar; length = strlen(cchar); unicodechar = (char *)malloc(length * 2); if(Debug(LINKSOCKET)) printf("LinkSocket.c::ASCIITOUnicode \n"); for(i=0;i<length;i++) { unicodechar[i*2]=0x0; unicodechar[i*2 + 1] = cchar[i]; if(Debug(LINKSOCKET)) { printf("Unicode char %d = %c %0x\n",i*2,unicodechar[i*2], unicodechar[i*2]); printf("Unicode char %d = %c %0x\n",(i*2 + 1),unicodechar[i*2+1], unicodechar[i*2+1]); } } return unicodechar;}/* * unicodecmp() * * This was written to supplement the strcmp() standard C function. * Instead of comparing two strings for to see if there is a match, * it is designed to take two strings which may be Unicode strings, * and compares character by character. * * This function was written to support OpenMap's C-side link server * code. The protocol requires that Unicode be sent across the link * so the Java-side client can easily parse the data being sent. * However, our server implementation takes in ASCII strings and * then converts them to Unicode. For the time being, we make the * assumption that we are passed English ASCII only, which may be * converted to Unicode by prepending the 0x0 byte to each ASCII * character. * * Unfortunately, C's standard strcmp() function will fail when * comparing two of our Unicode'd strings, as the first character * read in will be the \0 character, and strcmp() will terminate * immediately. This is a quick fix to get around that problem. * */int unicodecmp(const char *s1, const char *s2, int length) { int i; for (i = 0; i < length; i++) { if (s1[i] != s2[i]) { return 1; } } return 0;}/* * The functions listed below are deprecated, and are included only until * the redundant buffering code has been eliminated from the link server. * *//* Converts Unicode(ASCII in 2 bytes) Characters to ASCII Characters by stripping them of their 1st byte*/int UnicodeToASCII(char unicodechar[], char cchar[], int length){ int i; for( i = 0; i< length/2;i++){ cchar[i] = unicodechar[i*2 + 1]; } return length/2;}/* Converts ASCII Charecters to Unicode Chars by prefixing them with 0x00;*/int ASCIIToUnicode(const char cchar[], char unicodechar[], int length ){ int i; if(Debug(LINKSOCKET)) printf("LinkSocket.c::ASCIITOUnicode \n"); for(i=0;i<length;i++) { unicodechar[i*2]=0x0; unicodechar[i*2 + 1] = cchar[i]; if(Debug(LINKSOCKET)) { printf("Unicode char %d = %c %0x\n",i*2,unicodechar[i*2],unicodechar[i*2]); printf("Unicode char %d = %c %0x\n",(i*2 + 1),unicodechar[i*2+1],unicodechar[i*2+1]); } } return length*2;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -