nyctergatis.com

Contact

Projects
Sysquake Remote Live
NME
PDF
Hike
Sudoku
GifBuilder
jpeglib for Palm OS
MySQL Client
Cross-GCC for Mac OS
NMEAutolink.c
Go to the documentation of this file.
00001 
00008 /* License: new BSD license (see NME.h) */
00009 
00010 #include "NMEAutolink.h"
00011 
00013 #define isBlank(c) ((c) == ' ' || (c) == '\t' || (c) == '\r' || (c) == '\n')
00014 
00016 #define isAlpha(c) ((c) >= 'a' && (c) <= 'z' || (c) >= 'A' && (c) <= 'Z')
00017 
00018 NMEBoolean NMEAutoconvertCamelCase(NMEConstText src, NMEInt srcLen,
00019         NMEInt *i,
00020         NMEContext *context,
00021         void *userData)
00022 {
00023     NMEInt i1, j;
00024     
00025     // no match if first character is not a blank
00026     if (*i == 0)
00027         i1 = isBlank(src[*i]) ? *i + 1 : *i;
00028     else if (!isBlank(src[*i]))
00029         return FALSE;
00030     else
00031         i1 = *i + 1;
00032     
00033     if (isAlpha(src[i1]))
00034         for (j = 1; i1 + j < srcLen && isAlpha(src[i1 + j]); j++)
00035             if (src[i1 + j] <= 'Z' && src[i1 + j - 1] >= 'a')
00036             {
00037                 // lowercase followed by uppercase -> camelCase
00038                 // find end of sequence of letters
00039                 for (; i1 + j < srcLen && isAlpha(src[i1 + j]); j++)
00040                     ;
00041                 // copy link, including initial blank if any, if there is enough space
00042                 NMEAddString(&src[*i], i1 - *i, '\0', context); // blank
00043                 NMEAddString("[[", -1, '\0', context);
00044                 NMEAddString(&src[i1], j, '\0', context);
00045                 NMEAddString("]]", -1, '\0', context);
00046                 *i = i1 + j;
00047                 return TRUE;
00048             }
00049     
00050     return FALSE;
00051 }
00052 
00053 NMEBoolean NMEAutoconvertURL(NMEConstText src, NMEInt srcLen,
00054         NMEInt *i,
00055         NMEContext *context,
00056         void *userData)
00057 {
00058     NMEInt i1, j, k, p;
00059     static char const * const prefix[] =
00060     {
00061         "http://", "https://", "ftp://", "mailto:", NULL
00062     };
00063     static char const punctuation[] = ",.?!:;'";
00064     
00065     // no match if first character is not a blank
00066     if (*i == 0)
00067         i1 = isBlank(src[*i]) ? *i + 1 : *i;
00068     else if (!isBlank(src[*i]))
00069         return FALSE;
00070     else
00071         i1 = *i + 1;
00072     
00073     // find prefix
00074     for (j = 0; prefix[j]; j++)
00075     {
00076         for (k = 0; prefix[j][k] && i1 + k < srcLen && src[i1 + k] == prefix[j][k]; k++)
00077             ;
00078         if (prefix[j][k])
00079             continue;   // end of prefix not reached
00080         
00081         // continue until next blank/control or double-quote
00082         for (p = k; i1 + p < srcLen && src[i1 + p] != '"'
00083                 && !(src[i1 + p] >= '\0' && src[i1 + p] <= ' '); p++)
00084             ;
00085         
00086         // give up if nothing more than prefix
00087         if (p == k)
00088             continue;
00089         
00090         // remove trailing punctuation character
00091         for (j = 0; punctuation[j]; j++)
00092             if (src[i1 + p - 1] == punctuation[j])
00093             {
00094                 p--;
00095                 break;
00096             }
00097         
00098         // copy link, including blank src[*i] if any
00099         NMEAddString(&src[*i], i1 - *i, '\0', context); // blank
00100         NMEAddString("[[", -1, '\0', context);
00101         NMEAddString(&src[i1], p, '\0', context);
00102         NMEAddString("]]", -1, '\0', context);
00103         *i = i1 + p;
00104         return TRUE;
00105     }
00106     
00107     return FALSE;
00108 }
Generated by Doxygen.
Copyright 2007-2013, Yves Piguet.
All rights reserved.