[svn-commits] tilghman: branch tilghman/issue16461 r271515 - in /team/tilghman/issue16461: ...
SVN commits to the Digium repositories
svn-commits at lists.digium.com
Fri Jun 18 17:50:12 CDT 2010
Author: tilghman
Date: Fri Jun 18 17:50:08 2010
New Revision: 271515
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=271515
Log:
Most read's knocked out
Modified:
team/tilghman/issue16461/funcs/func_env.c
team/tilghman/issue16461/tests/test_func_file.c
Modified: team/tilghman/issue16461/funcs/func_env.c
URL: http://svnview.digium.com/svn/asterisk/team/tilghman/issue16461/funcs/func_env.c?view=diff&rev=271515&r1=271514&r2=271515
==============================================================================
--- team/tilghman/issue16461/funcs/func_env.c (original)
+++ team/tilghman/issue16461/funcs/func_env.c Fri Jun 18 17:50:08 2010
@@ -527,8 +527,6 @@
ast_str_append_substr(buf, len, fbuf, toappend);
}
- ast_str_append_substr(buf, len, "", 1);
-
return 0;
}
@@ -553,6 +551,8 @@
if (offset < 0 && length <= offset) {
/* Length eliminates all content */
return -1;
+ } else if (offset == 0) {
+ offset_offset = 0;
}
if (!(ff = fopen(args.filename, "r"))) {
@@ -570,22 +570,21 @@
/* For negative offset and/or negative length */
if (offset < 0 || length < 0) {
int64_t count = 0;
+ /* Start with an even multiple of fbuf, so at the end of reading with a
+ * 0 offset, we don't try to go past the beginning of the file. */
for (i = (flength / sizeof(fbuf)) * sizeof(fbuf); i >= 0; i -= sizeof(fbuf)) {
size_t end;
char *pos;
if (fseeko(ff, i, SEEK_SET)) {
ast_log(LOG_ERROR, "Cannot seek to offset %" PRId64 ": %s\n", i, strerror(errno));
}
- if ((end = fread(fbuf, 1, sizeof(fbuf), ff)) < sizeof(fbuf)) {
- /* Not really necessary... */
- fbuf[end] = '\0';
- }
- for (pos = end < sizeof(fbuf) ? fbuf + end : fbuf + sizeof(fbuf) - 1; pos > fbuf - 1; pos--) {
+ end = fread(fbuf, 1, sizeof(fbuf), ff);
+ for (pos = end < sizeof(fbuf) ? fbuf + end - 1 : fbuf + sizeof(fbuf) - 1; pos > fbuf - 1; pos--) {
LINE_COUNTER(pos, format, count);
if (length < 0 && count * -1 == length) {
- length_offset = i + (pos - fbuf) + 1;
- } else if (count * -1 == offset) {
+ length_offset = i + (pos - fbuf);
+ } else if (offset < 0 && count * -1 == (offset - 1)) {
/* Found our initial offset. We're done with reverse motion! */
if (format == FF_DOS) {
offset_offset = i + (pos - fbuf) + 2;
@@ -595,9 +594,13 @@
break;
}
}
- if (offset_offset >= 0) {
+ if ((offset < 0 && offset_offset >= 0) || (offset >= 0 && length_offset >= 0)) {
break;
}
+ }
+ /* We're at the beginning, and the negative offset indicates the exact number of lines in the file */
+ if (offset < 0 && offset_offset < 0 && offset == count * -1) {
+ offset_offset = 0;
}
}
@@ -635,17 +638,20 @@
* to buf. Otherwise, we need to run byte-by-byte forward until the
* length is complete. */
if (length_offset >= 0) {
+ ast_debug(1, "offset=%" PRId64 ", length=%" PRId64 ", offset_offset=%" PRId64 ", length_offset=%" PRId64 "\n", offset, length, offset_offset, length_offset);
for (i = offset_offset; i < length_offset; i += sizeof(fbuf)) {
if ((readlen = fread(fbuf, 1, sizeof(fbuf), ff) < sizeof(fbuf))) {
/* This is expected on the last time through the loop */
- memset(&fbuf[readlen], 0, sizeof(fbuf) - readlen);
}
ast_str_append_substr(buf, len, fbuf, i + sizeof(fbuf) > length_offset ? length_offset - i : sizeof(fbuf));
}
+ } else if (length == 0) {
+ /* Nothing to do */
} else {
/* Positive line offset */
int64_t current_length = 0;
char dos_state = 0;
+ ast_debug(1, "offset=%" PRId64 ", length=%" PRId64 ", offset_offset=%" PRId64 ", length_offset=%" PRId64 "\n", offset, length, offset_offset, length_offset);
for (i = offset_offset; i < flength; i += sizeof(fbuf)) {
char *pos;
if ((readlen = fread(fbuf, 1, sizeof(fbuf), ff)) < sizeof(fbuf)) {
@@ -659,7 +665,8 @@
break;
}
}
- ast_str_append_substr(buf, len, fbuf, length_offset >= 0 ? i - length_offset : sizeof(fbuf));
+ ast_debug(1, "length_offset=%" PRId64 ", length_offset - i=%" PRId64 "\n", length_offset, length_offset - i);
+ ast_str_append_substr(buf, len, fbuf, length_offset >= 0 ? length_offset - i : sizeof(fbuf));
if (length_offset >= 0) {
break;
Modified: team/tilghman/issue16461/tests/test_func_file.c
URL: http://svnview.digium.com/svn/asterisk/team/tilghman/issue16461/tests/test_func_file.c?view=diff&rev=271515&r1=271514&r2=271515
==============================================================================
--- team/tilghman/issue16461/tests/test_func_file.c (original)
+++ team/tilghman/issue16461/tests/test_func_file.c Fri Jun 18 17:50:08 2010
@@ -112,13 +112,33 @@
{ "123456789", "-9,-8", "fg", "fg23456789" },
};
+static char *file2display(struct ast_str **buf, ssize_t len, const char *input)
+{
+ const char *ptr;
+ ast_str_reset(*buf);
+ for (ptr = input; *ptr; ptr++) {
+ if (*ptr == '\n') {
+ ast_str_append(buf, len, "\\n");
+ } else if (*ptr == '\r') {
+ ast_str_append(buf, len, "\\r");
+ } else if (*ptr == '\t') {
+ ast_str_append(buf, len, "\\t");
+ } else if (*ptr < ' ' || *ptr > 125) {
+ ast_str_append(buf, len, "\\x%hhX", *ptr);
+ } else {
+ ast_str_append(buf, len, "%c", *ptr);
+ }
+ }
+ return ast_str_buffer(*buf);
+}
+
AST_TEST_DEFINE(test_func_file)
{
int res = AST_TEST_PASS;
int i;
char dir[] = "/tmp/test_func_file.XXXXXX";
char file[80], expression[256];
- struct ast_str *buf;
+ struct ast_str *buf, *disp[2] = { NULL, NULL };
char fbuf[256];
FILE *fh;
@@ -139,7 +159,12 @@
return AST_TEST_FAIL;
}
- if (!(buf = ast_str_create(16))) {
+ disp[0] = ast_str_create(16);
+ disp[1] = ast_str_create(16);
+ if (!(buf = ast_str_create(16)) || !disp[0] || !disp[1]) {
+ ast_free(buf);
+ ast_free(disp[0]);
+ ast_free(disp[1]);
rmdir(dir);
return AST_TEST_FAIL;
}
@@ -149,6 +174,9 @@
for (i = 0; i < ARRAY_LEN(read_tests); i++) {
if (!(fh = fopen(file, "w"))) {
ast_test_status_update(test, "Cannot open test file: %s\n", strerror(errno));
+ ast_free(buf);
+ ast_free(disp[0]);
+ ast_free(disp[1]);
unlink(file);
rmdir(dir);
return AST_TEST_FAIL;
@@ -156,6 +184,9 @@
if (fwrite(read_tests[i].contents, 1, strlen(read_tests[i].contents), fh) < strlen(read_tests[i].contents)) {
ast_test_status_update(test, "Cannot write initial values into test file: %s\n", strerror(errno));
+ ast_free(buf);
+ ast_free(disp[0]);
+ ast_free(disp[1]);
fclose(fh);
unlink(file);
rmdir(dir);
@@ -168,19 +199,19 @@
ast_str_substitute_variables(&buf, 0, NULL, expression);
if (strcmp(ast_str_buffer(buf), read_tests[i].value)) {
- if (strchr(read_tests[i].value, '\n')) {
- ast_test_status_update(test, "Expression '${FILE(...,%s)}' did not produce the expected value\n", read_tests[i].args);
- } else {
- ast_test_status_update(test, "Expression '${FILE(...,%s)}' did not produce ('%s') the expected value ('%s')\n",
- read_tests[i].args, ast_str_buffer(buf), read_tests[i].value);
- }
+ ast_test_status_update(test, "Expression '${FILE(...,%s)}' did not produce ('%s') the expected value ('%s')\n",
+ read_tests[i].args, file2display(&disp[0], 0, ast_str_buffer(buf)), file2display(&disp[1], 0, read_tests[i].value));
res = AST_TEST_FAIL;
}
}
+
+ ast_free(buf);
for (i = 0; i < ARRAY_LEN(write_tests); i++) {
if (!(fh = fopen(file, "w"))) {
ast_test_status_update(test, "Cannot open test file: %s\n", strerror(errno));
+ ast_free(disp[0]);
+ ast_free(disp[1]);
unlink(file);
rmdir(dir);
return AST_TEST_FAIL;
@@ -188,6 +219,8 @@
if (fwrite(write_tests[i].contents, 1, strlen(write_tests[i].contents), fh) < strlen(write_tests[i].contents)) {
ast_test_status_update(test, "Cannot write initial values into test file: %s\n", strerror(errno));
+ ast_free(disp[0]);
+ ast_free(disp[1]);
fclose(fh);
unlink(file);
rmdir(dir);
@@ -201,6 +234,8 @@
if (!(fh = fopen(file, "r"))) {
ast_test_status_update(test, "Cannot open test file: %s\n", strerror(errno));
+ ast_free(disp[0]);
+ ast_free(disp[1]);
unlink(file);
rmdir(dir);
return AST_TEST_FAIL;
@@ -209,6 +244,8 @@
memset(fbuf, 0, sizeof(fbuf));
if (!fread(fbuf, 1, sizeof(fbuf), fh)) {
ast_test_status_update(test, "Cannot read write results from test file: %s\n", strerror(errno));
+ ast_free(disp[0]);
+ ast_free(disp[1]);
fclose(fh);
unlink(file);
rmdir(dir);
@@ -223,12 +260,14 @@
write_tests[i].args, write_tests[i].value);
} else {
ast_test_status_update(test, "Expression 'FILE(...,%s)=%s' did not produce ('%s') the expected result ('%s')\n",
- write_tests[i].args, write_tests[i].value, fbuf, write_tests[i].contents2);
+ write_tests[i].args, write_tests[i].value, file2display(&disp[0], 0, fbuf), file2display(&disp[1], 0, write_tests[i].contents2));
}
res = AST_TEST_FAIL;
}
}
+ ast_free(disp[0]);
+ ast_free(disp[1]);
unlink(file);
rmdir(dir);
More information about the svn-commits
mailing list