128, 128, 0
};
-/**
- * \brief copy a string into a buffer of a known size. abort if we exceed the limits
- * \param dest the targetbuffer
- * \param src the source string
- * \param n the size od dest
- * \returns the number of characters copied if dest is big enough, -n if not.
+/*
+ * copy a string into a buffer of a known size. abort if we exceed the limits
+ *
+ * dest the targetbuffer
+ * src the source string
+ * n the size od dest
+ *
+ * returns the number of characters copied if dest is big enough, -n if not.
*/
int safestrncpy(char *dest, const char *src, size_t n)
{
return strdup(line);
}
- result = (char*) malloc(strlen(UTF8_HEADER) + 4 + length * 2);
+ result = (char*) malloc(sizeof(UTF8_HEADER) + 4 + length * 2);
strncpy (result, UTF8_HEADER, strlen (UTF8_HEADER));
CtdlEncodeBase64(result + strlen(UTF8_HEADER), line, length, 0);
end = strlen (result);
}
}
+/**
+ * \brief detect whether this char starts an utf-8 encoded char
+ * \param Char character to inspect
+ * \returns yes or no
+ */
+inline int Ctdl_IsUtf8SequenceStart(char Char)
+{
+/** 11??.???? indicates an UTF8 Sequence. */
+ return ((Char & 0xC0) != 0);
+}
+
+/**
+ * \brief evaluate the length of an utf8 special character sequence
+ * \param Char the character to examine
+ * \returns width of utf8 chars in bytes
+ */
+inline int Ctdl_GetUtf8SequenceLength(char Char)
+{
+ int n = 1;
+ char test = (1<<7);
+
+ while ((n < 8) && ((test & Char) != 0)) {
+ test = test << 1;
+ n ++;
+ }
+ if (n > 6)
+ n = 1;
+ return n;
+}
+
+/**
+ * \brief measure the number of glyphs in an UTF8 string...
+ * \param str string to measure
+ * \returns the length of str
+ */
+int Ctdl_Utf8StrLen(char *str)
+{
+ int n = 0;
+ int m = 0;
+ char *aptr;
+
+ if (str == NULL)
+ return n;
+ aptr = str;
+ while (*aptr != '\0') {
+ if (Ctdl_IsUtf8SequenceStart(*aptr)){
+ m = Ctdl_GetUtf8SequenceLength(*aptr);
+ while ((m-- > 0) && (*aptr++ != '\0'))
+ n ++;
+ }
+ else {
+ n++;
+ aptr++;
+ }
+
+ }
+ return n;
+}
+
+/**
+ * \brief cuts a string after maxlen glyphs
+ * \param str string to cut to maxlen glyphs
+ * \param maxlen how long may the string become?
+ * \returns pointer to maxlen or the end of the string
+ */
+char *Ctdl_Utf8StrCut(char *str, int maxlen)
+{
+ int n = 0, m = 0;
+ char *aptr;
+
+ if (str == NULL)
+ return NULL;
+ aptr = str;
+ while (*aptr != '\0') {
+ if (Ctdl_IsUtf8SequenceStart(*aptr)){
+ m = Ctdl_GetUtf8SequenceLength(*aptr);
+ while ((m-- > 0) && (*aptr++ != '\0'))
+ n ++;
+ }
+ else {
+ n++;
+ aptr++;
+ }
+ if (n > maxlen) {
+ *aptr = '\0';
+ return aptr;
+ }
+ }
+ return aptr;
+}
+
/*
* Convert all whitespace characters in a supplied string to underscores
}
}
}
+
+
+/*
+ * Parse a URL into host, port number, and resource identifier.
+ * (This is used by various functions which might need to fetch web pages.)
+ */
+int parse_url(char *url, char *hostname, int *port, char *identifier)
+{
+ char protocol[1024];
+ char scratch[1024];
+ char *ptr = NULL;
+ char *nptr = NULL;
+
+ strcpy(scratch, url);
+ ptr = (char *)strchr(scratch, ':');
+ if (!ptr) {
+ return(1); /* no protocol specified */
+ }
+
+ strcpy(ptr, "");
+ strcpy(protocol, scratch);
+ if (strcmp(protocol, "http")) {
+ return(2); /* not HTTP */
+ }
+
+ strcpy(scratch, url);
+ ptr = (char *) strstr(scratch, "//");
+ if (!ptr) {
+ return(3); /* no server specified */
+ }
+ ptr += 2;
+
+ strcpy(hostname, ptr);
+ nptr = (char *)strchr(ptr, ':');
+ if (!nptr) {
+ *port = 80; /* default */
+ nptr = (char *)strchr(hostname, '/');
+ }
+ else {
+ sscanf(nptr, ":%d", port);
+ nptr = (char *)strchr(hostname, ':');
+ }
+
+ if (nptr) {
+ *nptr = '\0';
+ }
+
+ nptr = (char *)strchr(ptr, '/');
+
+ if (!nptr) {
+ return(4); /* no url specified */
+ }
+
+ strcpy(identifier, nptr);
+ return(0);
+}
+
+