]> err.no Git - sope/commitdiff
rewrote WOHTMLParser to use Unicode
authorhelge <helge@e4a50df8-12e2-0310-a44c-efbce7f8a7e3>
Sun, 24 Apr 2005 14:51:41 +0000 (14:51 +0000)
committerhelge <helge@e4a50df8-12e2-0310-a44c-efbce7f8a7e3>
Sun, 24 Apr 2005 14:51:41 +0000 (14:51 +0000)
fixed some gcc 4.0 warnings

git-svn-id: http://svn.opengroupware.org/SOPE/trunk@766 e4a50df8-12e2-0310-a44c-efbce7f8a7e3

sope-appserver/NGObjWeb/ChangeLog
sope-appserver/NGObjWeb/DynamicElements/_WOStaticHTMLElement.h
sope-appserver/NGObjWeb/DynamicElements/_WOStaticHTMLElement.m
sope-appserver/NGObjWeb/Templates/WOHTMLParser.m
sope-appserver/NGObjWeb/Templates/WOWrapperTemplateBuilder.m
sope-appserver/NGObjWeb/Templates/WOxElemBuilder.m
sope-appserver/NGObjWeb/Version

index 3b361bc58f43d3112d15b5127f5d113af97501d4..d0eef4993da397f3da633acb8774bd21a5a45efc 100644 (file)
@@ -1,5 +1,11 @@
 2005-04-24  Helge Hess  <helge.hess@opengroupware.org>
 
+       * v4.5.153
+
+       * Templates/WOHTMLParser.m: rewrote parser to use unichar
+
+       * Templates: fixed gcc 4.0 warnings
+
        * v4.5.152
 
        * Templates/WODParser.m: rewrote parser to use unichar
index ac871e5e8def807eab1de9002feb346043e1904d..79ffe18538a35c168c30591fd0a6390540100bc5 100644 (file)
@@ -22,6 +22,7 @@
 #ifndef __DynamicElements__WOStaticHTMLElement_H__
 #define __DynamicElements__WOStaticHTMLElement_H__
 
+#import <Foundation/NSString.h>
 #include <NGObjWeb/WOElement.h>
 
 /*
@@ -37,8 +38,8 @@
   NSString *text;
 }
 
-// TODO: use Unicode?
 - (id)initWithBuffer:(const char *)_buffer length:(unsigned)_len;
+- (id)initWithCharacters:(const unichar *)_buffer length:(unsigned)_len;
 
 @end
 
index f98ad6a2042e261d46f2e027c6c48c877b3a19a5..e4a6e2ddc39837004055de6bee7ae1c094ac0c01 100644 (file)
@@ -37,6 +37,12 @@ static Class StrClass = Nil;
     : nil;
   return self;
 }
+- (id)initWithCharacters:(const unichar *)_buffer length:(unsigned)_len {
+  self->text = (_len > 0)
+    ? [[StrClass alloc] initWithCharacters:_buffer length:_len]
+    : nil;
+  return self;
+}
 
 - (void)dealloc {
   [self->text release];
index 8ba83d522cec57e45a7bfb19f34f7494839f3c9d..356c407080046dc377c1bc71a1301dc6872825e8 100644 (file)
@@ -50,7 +50,7 @@
 @implementation WOHTMLParser
 
 static WOElement *_parseElement(NSZone *_zone,
-                                const char *_buffer, unsigned *_idx,
+                                const unichar *_buffer, unsigned *_idx,
                                 unsigned _len, NSException **_exception,
                                 WOHTMLParser *self);
 
@@ -100,32 +100,11 @@ static BOOL  useUTF8 = NO;
                          contentElements:_subElements];
 }
 
-- (id)_makeConstantStringElementWithBuffer:(const unsigned char *)_buf
+- (id)_makeConstantStringElementWithBuffer:(const unichar *)_buf
   length:(unsigned)_len
 {
-  return [[WOStringClass allocWithZone:NULL] initWithBuffer:_buf length:_len];
-}
-
-- (NSString *)_makeStringForBuffer:(const unsigned char *)_buf 
-  length:(unsigned)_len
-{
-  NSString *r;
-  NSData   *data;
-  
-  if (_len == 0)
-    return @"";
-  
-  if (!useUTF8)
-    return [[StrClass alloc] initWithCString:_buf length:_len];
-  
-  // Note: we cast the pointer because we are not going to modify _buf for the
-  //       duration and we are never going to write the data - should work
-  //       with any Foundation, but isn't strictly API compatible
-  data = [[NSData alloc] initWithBytesNoCopy:(void *)_buf length:_len 
-                         freeWhenDone:NO];
-  r = [[StrClass alloc] initWithData:data encoding:NSUTF8StringEncoding];
-  [data release];
-  return r;
+  return [[WOStringClass allocWithZone:NULL] 
+          initWithCharacters:_buf length:_len];
 }
 
 /* accessors */
@@ -136,12 +115,20 @@ static BOOL  useUTF8 = NO;
 
 /* parsing API */
 
+- (NSStringEncoding)stringEncodingForData:(NSData *)_data  {
+  // TODO: we could check for UTF-16 marker in front of data
+  return useUTF8 ? NSUTF8StringEncoding : [NSString defaultCStringEncoding];
+}
+
 - (NSArray *)parseHTMLData:(NSData *)_html {
   NSMutableArray *topLevel;
-  const char     *html;
+  const unichar  *html;
   unsigned       idx, len;
   NSException    *exception = nil;
-
+  unichar        *buf;
+  unsigned int   bufLen;
+  NSString       *s;
+  
   if (![self->callback parser:self willParseHTMLData:_html])
     return nil;
   
@@ -150,18 +137,37 @@ static BOOL  useUTF8 = NO;
   if (_html == nil)
     return nil;
   
+  /* recode buffer using NSString */
+  
+  s = [[NSString alloc] initWithData:_html
+                       encoding:[self stringEncodingForData:_html]];
+  bufLen = [s length];
+  buf = calloc(bufLen + 2, sizeof(unichar));
+  [s getCharacters:buf];
+  [s release]; s = nil;
+  buf[bufLen] = 0; /* null-terminate buffer, parser might need that */
+  
+  /* start parsing */
+  
   topLevel = [NSMutableArray arrayWithCapacity:64];
   idx  = 0;
-  len  = [_html length];
-  html = [_html bytes];
+  len  = bufLen;
+  html = buf;
   
   while ((idx < len) && (exception == nil)) {
     WOElement *element;
     
-    if ((element = _parseElement(NULL, html, &idx, len, &exception, self))) {
-      [topLevel addObject:element];
-      [element release]; element = nil;
-    }
+    element = _parseElement(NULL, html, &idx, len, &exception, self);
+    if (element == nil)
+      continue;
+    
+    [topLevel addObject:element];
+    [element release]; element = nil;
+  }
+
+  if (buf != NULL) {
+    free(buf); buf = NULL;
+    html = NULL;
   }
   
   ASSIGN(self->parsingException, exception);
@@ -180,7 +186,7 @@ static BOOL  useUTF8 = NO;
 
 /* internal parsing */
 
-static int _numberOfLines(const char *_buffer, unsigned _lastIdx) {
+static int _numberOfLines(const unichar *_buffer, unsigned _lastIdx) {
   register int pos, lineCount = 1;
   
   for (pos = 0; (pos < (int)_lastIdx) && (_buffer[pos] != '\0'); pos++) {
@@ -190,7 +196,7 @@ static int _numberOfLines(const char *_buffer, unsigned _lastIdx) {
   return lineCount;
 }
 
-static inline BOOL _isHTMLSpace(char c) {
+static inline BOOL _isHTMLSpace(const unichar c) {
   switch (c) {
     case ' ': case '\t': case '\r': case '\n':
       return YES;
@@ -201,7 +207,7 @@ static inline BOOL _isHTMLSpace(char c) {
 }
 
 static NSException *_makeHtmlException(NSException *_exception,
-                                       const char *_buffer, unsigned _idx,
+                                       const unichar *_buffer, unsigned _idx,
                                        unsigned _len, NSString *_text,
                                        WOHTMLParser *self)
 {
@@ -240,7 +246,7 @@ static NSException *_makeHtmlException(NSException *_exception,
     
     if (!atEof && (_idx > 0)) {
       register unsigned pos;
-      const unsigned char *startPos, *endPos;
+      const unichar *startPos, *endPos;
 
       for (pos = _idx; (pos >= 0) && (_buffer[pos] != '\n'); pos--)
         ;
@@ -253,7 +259,8 @@ static NSException *_makeHtmlException(NSException *_exception,
       if (startPos < endPos) {
         NSString *ll;
         
-        ll = [self _makeStringForBuffer:startPos length:(endPos - startPos)];
+       ll = [[StrClass alloc] initWithCharacters:startPos 
+                              length:(endPos - startPos)];
         [ui setObject:ll forKey:@"lastLine"];
         [ll release];
       }
@@ -278,7 +285,7 @@ static NSException *_makeHtmlException(NSException *_exception,
 }
 
 static inline BOOL
-_isComment(const char *_buffer, unsigned _idx, unsigned _len)
+_isComment(const unichar *_buffer, unsigned _idx, unsigned _len)
 {
   // <!----> - 7 chars
   if ((_idx + 7) >= _len)  // check whether it is long enough
@@ -293,13 +300,14 @@ _isComment(const char *_buffer, unsigned _idx, unsigned _len)
   return YES;
 }
 
-static inline BOOL _isHashTag(const char *_buf, unsigned _idx, unsigned _len) {
+static inline BOOL _isHashTag(const unichar *_buf, unsigned _idx, 
+                             unsigned _len) {
   /* check for "<#.>" (len 4) */
   if ((_idx + 3) >= _len)  // check whether it is long enough
     return NO;
   return (_buf[_idx] == '<' && _buf[_idx + 1] == '#') ? YES : NO;
 }
-static inline BOOL _isHashCloseTag(const char *_buf, 
+static inline BOOL _isHashCloseTag(const unichar *_buf, 
                                   unsigned _idx, unsigned _len) 
 {
   /* check for "</#.>" (len 5) */
@@ -309,7 +317,27 @@ static inline BOOL _isHashCloseTag(const char *_buf,
     ? YES : NO;
 }
 
-static inline BOOL _isWOTag(const char *_buf, unsigned _idx, unsigned _len) {
+static BOOL _ucIsCaseEqual(const unichar *s, char *tok, unsigned len) {
+  register unsigned int i;
+  
+  for (i = 0; i < len; i++) {
+    register unsigned char c;
+    
+    if (s[i] == tok[i])
+      continue;
+    
+    if (s[i] == 0)
+      return NO;
+    
+    c = isupper(tok[i]) ? tolower(tok[i]) : toupper(tok[i]);
+    if (s[i] != c)
+      return NO;
+  }
+  return YES;
+}
+
+static inline BOOL _isWOTag(const unichar *_buf, unsigned _idx, 
+                           unsigned _len) {
   /* check for "<WEBOBJECT .......>" (len 19) (lowercase is allowed) */
   if ((_idx + 18) >= _len)  // check whether it is long enough
     return NO;
@@ -317,11 +345,11 @@ static inline BOOL _isWOTag(const char *_buf, unsigned _idx, unsigned _len) {
     return NO;
   
   // now check for '<WEBOBJECT'
-  return (strncasecmp(&(_buf[_idx]), "<WEBOBJECT", 10) == 0) ? YES : NO;
+  return _ucIsCaseEqual(&(_buf[_idx]), "<WEBOBJECT", 10);
 }
 
 static inline BOOL
-_isWOCloseTag(const char *_buf, unsigned _idx, unsigned _len)
+_isWOCloseTag(const unichar *_buf, unsigned _idx, unsigned _len)
 {
   /* check for </WEBOBJECT> (len=12) */
   if ((_idx + 12) > _len)  // check whether it is long enough
@@ -329,10 +357,10 @@ _isWOCloseTag(const char *_buf, unsigned _idx, unsigned _len)
   if (_buf[_idx] != '<') // check whether it is a tag
     return NO;
   
-  return (strncasecmp(&(_buf[_idx]), "</WEBOBJECT>", 12) == 0) ? YES : NO;
+  return _ucIsCaseEqual(&(_buf[_idx]), "</WEBOBJECT>", 12);
 }
 
-static inline void _skipSpaces(register const char *_buffer, unsigned *_idx,
+static inline void _skipSpaces(register const unichar *_buffer, unsigned *_idx,
                                unsigned _len)
 {
   register unsigned pos = *_idx;
@@ -346,7 +374,7 @@ static inline void _skipSpaces(register const char *_buffer, unsigned *_idx,
 }
 
 static NSString *_parseStringValue(NSZone *_zone,
-                                   register const char *_buffer,
+                                   register const unichar *_buffer,
                                    unsigned *_idx, unsigned _len,
                                    NSException **_exception,
                                    WOHTMLParser *self)
@@ -387,9 +415,12 @@ static NSString *_parseStringValue(NSZone *_zone,
     if (len == 0) // empty string
       return @"";
     
-    return [self _makeStringForBuffer:&(_buffer[startPos]) length:len];
+    return [[StrClass alloc] initWithCharacters:&(_buffer[startPos]) 
+                            length:len];
   }
-  else {
+  
+  /* string without quotes */
+  {
     unsigned startPos = pos;
 
     //NSLog(@"parsing id at '%c'[%i] ..", _buffer[pos], pos);
@@ -407,12 +438,12 @@ static NSString *_parseStringValue(NSZone *_zone,
     if ((pos - startPos) == 0) // wasn't a string ..
       return nil;
 
-    return [self _makeStringForBuffer:&(_buffer[startPos]) 
-                 length:(pos - startPos)];
+    return [[StrClass alloc] initWithCharacters:&(_buffer[startPos]) 
+                            length:(pos - startPos)];
   }
 }
 
-static WOElement *_parseHashElement(NSZone *_zone, const char *_buffer,
+static WOElement *_parseHashElement(NSZone *_zone, const unichar *_buffer,
                                    unsigned *_idx, unsigned _len,
                                    NSException **_exc,
                                    WOHTMLParser *self)
@@ -555,7 +586,7 @@ static WOElement *_parseHashElement(NSZone *_zone, const char *_buffer,
 }
 
 static NSMutableDictionary *
-_parseTagAttributes(NSZone *_zone, const char *_buffer,
+_parseTagAttributes(NSZone *_zone, const unichar *_buffer,
                     unsigned *_idx, unsigned _len,
                     NSException **_exception, WOHTMLParser *self)
 {
@@ -629,7 +660,7 @@ _parseTagAttributes(NSZone *_zone, const char *_buffer,
 
   return dict;
 }
-static WOElement *_parseWOElement(NSZone *_zone, const char *_buffer,
+static WOElement *_parseWOElement(NSZone *_zone, const unichar *_buffer,
                                   unsigned *_idx, unsigned _len,
                                   NSException **_exception,
                                   WOHTMLParser *self)
@@ -644,8 +675,8 @@ static WOElement *_parseWOElement(NSZone *_zone, const char *_buffer,
   if (!_isWOTag(_buffer, *_idx, _len))
     return nil; // not a WO tag ..
 
-  NSCAssert(strncasecmp("<WEBOBJECT", &(_buffer[*_idx]), 10) == 0,
-            @"invalid parser state ..");
+  NSCAssert(_ucIsCaseEqual(&(_buffer[*_idx]), "<WEBOBJECT", 10),
+            @"Invalid parser state (expected <WEBOBJECT in buffer)!");
   
   // skip '<WEBOBJECT'
   *_idx += 10;
@@ -757,11 +788,11 @@ static WOElement *_parseWOElement(NSZone *_zone, const char *_buffer,
   return element;
 }
 
-static inline NSString *_makeTextString(NSZone *_zone, const char *_buffer,
+static inline NSString *_makeTextString(NSZone *_zone, const unichar *_buffer,
                                         unsigned _len, WOHTMLParser *self)
 {
   NSString *result = nil;
-  register unsigned char *buffer;
+  register unichar  *buffer;
   register unsigned pos, bufPos;
   
   if (_len == 0) // empty string
@@ -769,10 +800,10 @@ static inline NSString *_makeTextString(NSZone *_zone, const char *_buffer,
 
   if (!compressHTMLWhitespace)
     /* deliver whitespace as in template */
-    return [self _makeStringForBuffer:_buffer length:_len];
+    return [[StrClass alloc] initWithCharacters:_buffer length:_len];
+  
+  buffer = calloc(_len + 3, sizeof(unichar));
   
-  buffer = malloc(_len + 3);
-
   for (pos = 0, bufPos = 0; pos < _len; ) {
       buffer[bufPos] = _buffer[pos];
 
@@ -791,13 +822,13 @@ static inline NSString *_makeTextString(NSZone *_zone, const char *_buffer,
       }
   }
   
-  result = [self _makeStringForBuffer:buffer length:bufPos];
-  if (buffer) free(buffer);
+  result = [[StrClass alloc] initWithCharacters:buffer length:bufPos];
+  if (buffer != NULL) free(buffer);
   return result;
 }
 
 static WOElement *_parseElement(NSZone *_zone,
-                                const char *_buffer, unsigned *_idx,
+                                const unichar *_buffer, unsigned *_idx,
                                 unsigned _len, NSException **_exception,
                                 WOHTMLParser *self)
 {
index 412e2cf921f6187f658523d161f41399274c2d25..838727c448645fa4a186599c83b124a03b9db0ff 100644 (file)
@@ -101,8 +101,9 @@ static NSStringEncoding parserEncoding;
 - (BOOL)_parseDeclarationsFile:(NSData *)_decl {
   NSDictionary *defs;
   WODParser *parser;
-    
-  parser = [[[WODParser alloc] initWithHandler:(id)self] autorelease];
+   
+  parser = [WODParser alloc]; /* keep gcc happy */
+  parser = [[parser initWithHandler:(id)self] autorelease];
   defs = [parser parseDeclarationData:_decl];
   return defs ? YES : NO;
 }
@@ -120,7 +121,8 @@ static NSStringEncoding parserEncoding;
     return nil;
   
   /* parse HTML file */
-  parser = [[[WOHTMLParser alloc] initWithHandler:(id)self] autorelease];
+  parser = [WOHTMLParser alloc]; /* keep gcc happy */
+  parser = [[parser initWithHandler:(id)self] autorelease];
   if ((topLevel = [parser parseHTMLData:_html]) == nil)
     exception = [parser parsingException];
   
index b053245015e7366a8a964da44824ebb2a5670f4f..ff9362ce28bc8be118935e2d7705f79c1e327465 100644 (file)
@@ -71,7 +71,7 @@
 
 /* operations */
 
-- (WOComponent *)instantiateWithResourceManager:(WOResourceManager *)_rm
+- (id)instantiateWithResourceManager:(WOResourceManager *)_rm
   languages:(NSArray *)_languages
 {
   static Class FaultClass = Nil;
index 8cf51b052df68fb93b2a5ace837e1f8cd4473dff..ae5132fa525a58a3c397a66d025cecb610f4762e 100644 (file)
@@ -1,6 +1,6 @@
 # version file
 
-SUBMINOR_VERSION:=152
+SUBMINOR_VERSION:=153
 
 # v4.5.122 requires libNGExtensions v4.5.153
 # v4.5.91  requires libNGExtensions v4.5.134