Subject: | [PATCH] Assorted C cleanups |
This module uses some fairly recent C features that many compilers that
support Perl do not allow. I've included a patch below with the various
fixes. Also, when compiling, I ran into some additional warnings.
Here's a quick summary of the fixes I've made.
- changed all "inline" to "static" otherwise compiles with recent
Solaris cc's fail. Many older gcc's fail as well.
- removed "//" comments. Many C compilers dislike these.
- removed several unused variables and functions.
- fixed a couple of warnings for type casts and formats.
- changed several functions to return NULL instead of zero when a
pointer return is expected.
- placed code conditional on DEBUG inside of "#if"s instead of a regular
"if". This resulted in smaller code, and should improve performance in
normal builds.
Enjoy!
Steve Peters
steve@fisharerojo.org
--- GetParserXS.xs.old Fri Nov 30 09:37:01 2007
+++ GetParserXS.xs Fri Nov 30 09:38:15 2007
@@ -25,81 +25,47 @@
return 0;
}
-inline void set_key (AV* self, const char *key, int len) {
+static void set_key (AV* self, const char *key, int len) {
av_store(self, KEY, newSVpv(key, len));
}
-inline SV *get_key_sv (AV* self) {
- SV** svp = av_fetch(self, KEY, 0);
- if (svp)
- return (SV*) *svp;
- return 0;
-}
-
-inline SV *get_on_item (AV* self) {
- SV** svp = av_fetch(self, ON_ITEM, 0);
- if (svp)
- return (SV*) *svp;
- return 0;
-}
-
-inline SV *get_offset_sv (AV* self) {
- SV** svp = av_fetch(self, OFFSET, 0);
- if (svp)
- return (SV*) *svp;
-
- *svp = newSViv(0);
- av_store(self, OFFSET, *svp);
- return (SV*) *svp;
-}
-
-inline void clear_on_item (AV* self) {
- SV** svp = av_store(self, ON_ITEM, newSV(0) );
-}
-
-inline void set_flags (AV* self, int flags) {
+static void set_flags (AV* self, int flags) {
av_store(self, FLAGS, newSViv(flags));
}
-inline void set_offset (AV* self, int offset) {
+static void set_offset (AV* self, int offset) {
av_store(self, OFFSET, newSViv(offset));
}
-inline void set_state (AV* self, int state) {
+static void set_state (AV* self, int state) {
av_store(self, STATE, newSViv(state));
}
-inline HV* get_dest (AV* self) {
+static HV* get_dest (AV* self) {
SV** svp = av_fetch(self, DEST, 0);
if (svp)
return (HV*) SvRV(*svp);
- return 0;
+ return NULL;
}
-inline HV* get_finished (AV* self) {
+static HV* get_finished (AV* self) {
SV** svp = av_fetch(self, FINISHED, 0);
if (svp)
return (HV*) SvRV(*svp);
- return 0;
+ return NULL;
}
-inline IV get_state (AV* self) {
- SV** svp = av_fetch(self, STATE, 0);
- if (svp)
- return SvIV((SV*) *svp);
- return 0;
-}
-
-inline SV* get_buffer (AV* self) {
+static SV* get_buffer (AV* self) {
SV** svp = av_fetch(self, BUF, 0);
if (svp)
return *svp;
- return 0;
+ return NULL;
}
/* returns an answer, but also unsets ON_ITEM */
int final_answer (AV* self, int ans) {
-// av_store(self, ON_ITEM, newSV(0));
+/* av_store(self, ON_ITEM, newSV(0)); */
+ PERL_UNUSED_ARG(self);
return ans;
}
@@ -111,9 +77,7 @@
char* buf;
unsigned int itemlen;
unsigned int flags;
- int scanned;
int nslen = get_nslen(self);
- SV* on_item = get_on_item(self);
register signed char c;
char *key;
register char *p;
@@ -123,15 +87,15 @@
HV* finished = get_finished(self);
- if (DEBUG)
- printf("get_buffer (nslen = %d)...\n", nslen);
+#if DEBUG
+ printf("get_buffer (nslen = %d)...\n", nslen);
+#endif
while (1) {
- int rv;
buf = SvPV(bufsv, len);
p = buf;
- if (DEBUG) {
+#if DEBUG
char first_line[1000];
int i;
char *end;
@@ -140,37 +104,41 @@
end += 10;
strncpy (first_line, buf, end - buf + 1);
first_line[end - buf + 1] = '\0';
- printf("GOT buf (len=%d)\nFirst line: %s\n", len, first_line);
- }
+ printf("GOT buf (len=%lu)\nFirst line: %s\n", len, first_line);
+#endif
if ((c = *p++) == 'V') {
if (*p++ != 'A' || *p++ != 'L' || *p++ != 'U' || *p++ != 'E' ||
*p++ != ' ') {
- if (DEBUG)
- puts ("ERROR: Illegal command beginning with V");
+#if DEBUG
+ puts ("ERROR: Illegal command beginning with V");
+#endif
goto recover_from_partial_line;
}
- // Parsing VALUE %s<key> %u<flags> %u<bytes>
+ /* Parsing VALUE %s<key> %u<flags> %u<bytes> */
for (key = p; *p++ > ' ';)
;
key_len = p - key - 1;
if (*(p - 1) != ' ') {
- if (DEBUG)
- printf ("ERROR: key not space-terminated: key %s, char %c\n",
key, *(p - 1));
+#if DEBUG
+ printf ("ERROR: key not space-terminated: key %s, char %c\n",
key, *(p - 1));
+#endif
goto recover_from_partial_line;
}
- // Note that key just points into the buffer and is not
null-terminated
- // yet. Leave it that way in case we're dealing with a partial line.
+ /*
+ * Note that key just points into the buffer and is not
null-terminated
+ * yet. Leave it that way in case we're dealing with a partial line.
- // Get flags and itemlen as integers. Note invalid characters
- // are not caught and will result in strange numbers.
-
+ * Get flags and itemlen as integers. Note invalid characters
+ * are not caught and will result in strange numbers.
+ */
for (flags = 0; (c = *p++ - '0') >= 0; flags = flags * 10 + c)
;
if (c != (signed char)' ' - '0') {
- if (DEBUG)
- puts ("ERROR: Flags not space terminated");
+#if DEBUG
+ puts ("ERROR: Flags not space terminated");
+#endif
goto recover_from_partial_line;
}
@@ -178,35 +146,37 @@
for (itemlen = 0; (c = *p++ - '0') >= 0; itemlen = itemlen * 10 + c)
;
if (c != (signed char)'\r' - '0' || *p++ != '\n') {
- if (DEBUG)
- puts ("ERROR: byte count not CRLF-terminated");
+#if DEBUG
+ puts ("ERROR: byte count not CRLF-terminated");
+#endif
goto recover_from_partial_line;
}
- // p is left at the start of the value data.
+ /* p is left at the start of the value data. */
new_p = p - buf;
state = itemlen + 2; /* 2 to include reading final \r\n, a
different \r\n */
- copy = len - new_p > state ? state : len - new_p;
+ copy = (int)len - new_p > state ? state : (int)len - new_p;
barekey = key + nslen;
barelen = key_len - nslen;
- if (DEBUG) {
+#if DEBUG
char temp_key[256];
strncpy (temp_key, key, key_len);
temp_key[key_len] = '\0';
printf("key=[%s], state=%d, copy=%d\n", key, state, copy);
- }
+#endif
if (copy) {
- *(key + key_len) = '\0'; // Null-terminate the key in-buffer
+ *(key + key_len) = '\0'; /* Null-terminate the key
in-buffer */
hv_store(ret, barekey, barelen, newSVpv(buf + new_p, copy), 0);
buf[new_p + copy - 1] = '\0';
- if (DEBUG)
- printf("doing store: len=%d key=[%s] of data [%c]\n",
- strlen(barekey), barekey, *(buf + new_p));
+#if DEBUG
+ printf("doing store: len=%lu key=[%s] of data [%c]\n",
+ strlen(barekey), barekey, *(buf + new_p));
+#endif
}
/* delete the stuff we used */
@@ -226,24 +196,23 @@
set_key(self, barekey, barelen);
set_state(self, state);
- if (DEBUG)
- printf("don't have it all.... have '%d' of '%d'\n",
- copy, state);
+#if DEBUG
+ printf("don't have it all.... have '%d' of '%d'\n",
+ copy, state);
+#endif
return 0; /* return saying '0', not done */
}
}
else if (c == 'E') {
- // Parsing END
+ /* Parsing END */
if (*p++ == 'N' && *p++ == 'D' && *p++ == '\r' && *p == '\n')
return final_answer(self, 1);
}
- // Just fall through if after 'E' was not "ND\r\n"
-
- else
- ; // Unknown command: not 'E' or 'V' at [0]
+ /* Just fall through if after 'E' was not "ND\r\n" */
+ /* Unknown command: not 'E' or 'V' at [0] */
/* # if we're here probably means we only have a partial VALUE