ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

string.c
Go to the documentation of this file.
00001 /*
00002  * RichEdit - string operations
00003  *
00004  * Copyright 2004 by Krzysztof Foltman
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
00019  */
00020 
00021 #include "editor.h"
00022 
00023 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
00024 
00025 static int ME_GetOptimalBuffer(int nLen)
00026 {
00027   /* FIXME: This seems wasteful for tabs and end of lines strings,
00028    *        since they have a small fixed length. */
00029   return ((sizeof(WCHAR) * nLen) + 128) & ~63;
00030 }
00031 
00032 /* Create a buffer (uninitialized string) of size nMaxChars */
00033 static ME_String *ME_MakeStringB(int nMaxChars)
00034 {
00035   ME_String *s = ALLOC_OBJ(ME_String);
00036 
00037   s->nLen = nMaxChars;
00038   s->nBuffer = ME_GetOptimalBuffer(s->nLen + 1);
00039   s->szData = ALLOC_N_OBJ(WCHAR, s->nBuffer);
00040   s->szData[s->nLen] = 0;
00041   return s;
00042 }
00043 
00044 ME_String *ME_MakeStringN(LPCWSTR szText, int nMaxChars)
00045 {
00046   ME_String *s = ME_MakeStringB(nMaxChars);
00047   /* Native allows NULL chars */
00048   memcpy(s->szData, szText, s->nLen * sizeof(WCHAR));
00049   return s;
00050 }
00051 
00052 /* Make a string by repeating a char nMaxChars times */
00053 ME_String *ME_MakeStringR(WCHAR cRepeat, int nMaxChars)
00054 {
00055   int i;
00056   ME_String *s = ME_MakeStringB(nMaxChars);
00057   for (i = 0; i < nMaxChars; i++)
00058     s->szData[i] = cRepeat;
00059   return s;
00060 }
00061 
00062 ME_String *ME_StrDup(const ME_String *s)
00063 {
00064   return ME_MakeStringN(s->szData, s->nLen);
00065 }
00066 
00067 void ME_DestroyString(ME_String *s)
00068 {
00069   if (!s) return;
00070   FREE_OBJ(s->szData);
00071   FREE_OBJ(s);
00072 }
00073 
00074 void ME_AppendString(ME_String *s1, const ME_String *s2)
00075 {
00076   if (s1->nLen+s2->nLen+1 <= s1->nBuffer)
00077   {
00078     memcpy(s1->szData + s1->nLen, s2->szData, s2->nLen * sizeof(WCHAR));
00079     s1->nLen += s2->nLen;
00080     s1->szData[s1->nLen] = 0;
00081   } else {
00082     WCHAR *buf;
00083     s1->nBuffer = ME_GetOptimalBuffer(s1->nLen+s2->nLen+1);
00084 
00085     buf = ALLOC_N_OBJ(WCHAR, s1->nBuffer);
00086     memcpy(buf, s1->szData, s1->nLen * sizeof(WCHAR));
00087     memcpy(buf + s1->nLen, s2->szData, s2->nLen * sizeof(WCHAR));
00088     FREE_OBJ(s1->szData);
00089     s1->szData = buf;
00090     s1->nLen += s2->nLen;
00091     s1->szData[s1->nLen] = 0;
00092   }
00093 }
00094 
00095 ME_String *ME_VSplitString(ME_String *orig, int charidx)
00096 {
00097   ME_String *s;
00098 
00099   /*if (charidx<0) charidx = 0;
00100   if (charidx>orig->nLen) charidx = orig->nLen;
00101   */
00102   assert(charidx>=0);
00103   assert(charidx<=orig->nLen);
00104 
00105   s = ME_MakeStringN(orig->szData+charidx, orig->nLen-charidx);
00106   orig->nLen = charidx;
00107   orig->szData[charidx] = '\0';
00108   return s;
00109 }
00110 
00111 int ME_IsWhitespaces(const ME_String *s)
00112 {
00113   /* FIXME multibyte */
00114   WCHAR *pos = s->szData;
00115   while(ME_IsWSpace(*pos++))
00116     ;
00117   pos--;
00118   if (*pos)
00119     return 0;
00120   else
00121     return 1;
00122 }
00123 
00124 int ME_IsSplitable(const ME_String *s)
00125 {
00126   WCHAR *pos = s->szData;
00127   WCHAR ch;
00128   while(ME_IsWSpace(*pos++))
00129     ;
00130   pos--;
00131   while((ch = *pos++) != 0)
00132   {
00133     if (ME_IsWSpace(ch))
00134       return 1;
00135   }
00136   return 0;
00137 }
00138 
00139 void ME_StrDeleteV(ME_String *s, int nVChar, int nChars)
00140 {
00141   int end_ofs = nVChar + nChars;
00142 
00143   assert(nChars >= 0);
00144   assert(nVChar >= 0);
00145   assert(end_ofs <= s->nLen);
00146 
00147   memmove(s->szData + nVChar, s->szData + end_ofs,
00148           (s->nLen - end_ofs + 1) * sizeof(WCHAR));
00149   s->nLen -= nChars;
00150 }
00151 
00152 int ME_FindNonWhitespaceV(const ME_String *s, int nVChar) {
00153   int i;
00154   for (i = nVChar; i<s->nLen && ME_IsWSpace(s->szData[i]); i++)
00155     ;
00156     
00157   return i;
00158 }
00159 
00160 /* note: returns offset of the first trailing whitespace */
00161 int ME_ReverseFindNonWhitespaceV(const ME_String *s, int nVChar) {
00162   int i;
00163   for (i = nVChar; i>0 && ME_IsWSpace(s->szData[i-1]); i--)
00164     ;
00165     
00166   return i;
00167 }
00168 
00169 /* note: returns offset of the first trailing nonwhitespace */
00170 int ME_ReverseFindWhitespaceV(const ME_String *s, int nVChar) {
00171   int i;
00172   for (i = nVChar; i>0 && !ME_IsWSpace(s->szData[i-1]); i--)
00173     ;
00174     
00175   return i;
00176 }
00177 
00178 
00179 static int
00180 ME_WordBreakProc(LPWSTR s, INT start, INT len, INT code)
00181 {
00182   /* FIXME: Native also knows about punctuation */
00183   TRACE("s==%s, start==%d, len==%d, code==%d\n",
00184         debugstr_wn(s, len), start, len, code);
00185   /* convert number of bytes to number of characters. */
00186   len /= sizeof(WCHAR);
00187   switch (code)
00188   {
00189     case WB_ISDELIMITER:
00190       return ME_IsWSpace(s[start]);
00191     case WB_LEFT:
00192     case WB_MOVEWORDLEFT:
00193       while (start && ME_IsWSpace(s[start - 1]))
00194         start--;
00195       while (start && !ME_IsWSpace(s[start - 1]))
00196         start--;
00197       return start;
00198     case WB_RIGHT:
00199     case WB_MOVEWORDRIGHT:
00200       while (start < len && !ME_IsWSpace(s[start]))
00201         start++;
00202       while (start < len && ME_IsWSpace(s[start]))
00203         start++;
00204       return start;
00205   }
00206   return 0;
00207 }
00208 
00209 
00210 int
00211 ME_CallWordBreakProc(ME_TextEditor *editor, ME_String *str, INT start, INT code)
00212 {
00213   if (!editor->pfnWordBreak) {
00214     return ME_WordBreakProc(str->szData, start, str->nLen*sizeof(WCHAR), code);
00215   } else if (!editor->bEmulateVersion10) {
00216     /* MSDN lied about the third parameter for EditWordBreakProc being the number
00217      * of characters, it is actually the number of bytes of the string. */
00218     return editor->pfnWordBreak(str->szData, start, str->nLen*sizeof(WCHAR), code);
00219   } else {
00220     int result;
00221     int buffer_size = WideCharToMultiByte(CP_ACP, 0, str->szData, str->nLen,
00222                                           NULL, 0, NULL, NULL);
00223     char *buffer = heap_alloc(buffer_size);
00224     WideCharToMultiByte(CP_ACP, 0, str->szData, str->nLen,
00225                         buffer, buffer_size, NULL, NULL);
00226     result = editor->pfnWordBreak(str->szData, start, str->nLen, code);
00227     heap_free(buffer);
00228     return result;
00229   }
00230 }
00231 
00232 LPWSTR ME_ToUnicode(BOOL unicode, LPVOID psz)
00233 {
00234   assert(psz != NULL);
00235 
00236   if (unicode)
00237     return psz;
00238   else {
00239     WCHAR *tmp;
00240     int nChars = MultiByteToWideChar(CP_ACP, 0, psz, -1, NULL, 0);
00241     if((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
00242       MultiByteToWideChar(CP_ACP, 0, psz, -1, tmp, nChars);
00243     return tmp;
00244   }
00245 }
00246 
00247 void ME_EndToUnicode(BOOL unicode, LPVOID psz)
00248 {
00249   if (!unicode)
00250     FREE_OBJ(psz);
00251 }

Generated on Sat May 26 2012 04:16:31 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.