static void prspace(ftnint n) { register ftnint m; while(nch>0 && n>0) { --n; prch(0); } m = SZSHORT * (n/SZSHORT); if(m > 0) prskip(stdout, m); for(n -= m ; n>0 ; --n) prch(0); }
int iso_utf8_2gsm(char* source, int size, char* destination, int max) { int source_count=0; int dest_count=0; int found=0; char newch; char logtmp[51]; char tmpch; destination[dest_count]=0; if (source==0 || size <= 0) return 0; #ifdef DEBUGMSG log_charconv = 1; #endif if (log_charconv) { *logch_buffer = 0; logch("!! iso_utf8_2gsm(source=%.*s, size=%i)", size, source, size); logch(NULL); } // Convert each character until end of string while (source_count<size && dest_count<max) { found = char2gsm(source[source_count], &newch); if (found == 2) { if (dest_count >= max -2) break; destination[dest_count++] = 0x1B; } if (found >= 1) { destination[dest_count++] = newch; if (log_charconv) { sprintf(logtmp, "%02X[%c]", (unsigned char)source[source_count], prch(source[source_count])); if (found > 1 || source[source_count] != newch) { sprintf(strchr(logtmp, 0), "->%s%02X", (found == 2)? "Esc-" : "", (unsigned char)newch); if (gsm2char(newch, &tmpch, found)) sprintf(strchr(logtmp, 0), "[%c]", tmpch); } logch("%s ", logtmp); } } if (found == 0 && outgoing_utf8) { // ASCII and UTF-8 table: http://members.dslextreme.com/users/kkj/webtools/ascii_utf8_table.html // Good converter: http://www.macchiato.com/unicode/convert.html unsigned int c; int iterations = 0; // 3.1beta7: If UTF-8 decoded character is not found from tables, decoding is ignored: int saved_source_count = source_count; char sourcechars[51]; c = source[source_count]; if (log_charconv) sprintf(sourcechars, "%02X", (unsigned char)source[source_count]); // 3.1beta7: Check if there is enough characters left. // Following bytes in UTF-8 should begin with 10xx xxxx // which means 0x80 ... 0xBF if (((c & 0xFF) >= 0xC2 && (c & 0xFF) <= 0xC7) || ((c & 0xFF) >= 0xD0 && (c & 0xFF) <= 0xD7)) { if (source_count < size -1 && (source[source_count +1] & 0xC0) == 0x80) { // 110xxxxx c &= 0x1F; iterations = 1; } } else if ((c & 0xFF) >= 0xE0 && (c & 0xFF) <= 0xE7) { if (source_count < size -2 && (source[source_count +1] & 0xC0) == 0x80 && (source[source_count +2] & 0xC0) == 0x80) { // 1110xxxx c &= 0x0F; iterations = 2; } } else if ((c & 0xFF) >= 0xF0 && (c & 0xFF) <= 0xF4) { if (source_count < size -3 && (source[source_count +1] & 0xC0) == 0x80 && (source[source_count +2] & 0xC0) == 0x80 && (source[source_count +3] & 0xC0) == 0x80) { // 11110xxx c &= 0x07; iterations = 3; } } if (iterations > 0) { int i; for (i = 0; i < iterations; i++) { c = (c << 6) | (source[++source_count] -0x80); if (log_charconv) sprintf(strchr(sourcechars, 0), "%02X", (unsigned char)source[source_count]); } // Euro character is 20AC in UTF-8, but A4 in ISO-8859-15: if ((c & 0xFF) == 0xAC) c = 0xA4; found = char2gsm((char)c, &newch); if (found == 2) { if (dest_count >= max -2) break; destination[dest_count++] = 0x1B; } if (found >= 1) { destination[dest_count++] = newch; if (log_charconv) { sprintf(logtmp, "%s(%02X[%c])->%s%02X", sourcechars, (unsigned char)c, prch(c), (found == 2)? "Esc-" : "", (unsigned char)newch); if (gsm2char(newch, &tmpch, found)) sprintf(strchr(logtmp, 0), "[%c]", tmpch); logch("%s ", logtmp); } } else { found = special_char2gsm((char)c, &newch); if (found) { destination[dest_count++] = newch; if (log_charconv) { sprintf(logtmp, "%s(%02X[%c])~>%02X", sourcechars, (unsigned char)c, prch(c), (unsigned char)newch); if (gsm2char(newch, &tmpch, 1)) sprintf(strchr(logtmp, 0), "[%c]", tmpch); logch("%s ", logtmp); } } else source_count = saved_source_count; } } } // 3.1beta7: Try additional table: if (found == 0) { found = special_char2gsm(source[source_count], &newch); if (found) { destination[dest_count++] = newch; if (log_charconv) { sprintf(logtmp, "%02X[%c]~>%02X", (unsigned char)source[source_count], prch(source[source_count]), (unsigned char)newch); if (gsm2char(newch, &tmpch, 1)) sprintf(strchr(logtmp, 0), "[%c]", tmpch); logch("%s ", logtmp); } } } if (found==0) { writelogfile0(LOG_NOTICE, 0, tb_sprintf("Cannot convert %i. character %c 0x%2X to GSM, you might need to update the translation tables.", source_count +1, source[source_count], source[source_count])); #ifdef DEBUGMSG printf("%s\n", tb); #endif } source_count++; } if (log_charconv) logch(NULL); // Terminate destination string with 0, however 0x00 are also allowed within the string. destination[dest_count]=0; return dest_count; }