summaryrefslogtreecommitdiffstats
path: root/src.scim2/file.c
diff options
context:
space:
mode:
authormongo <mongo@iomega>2016-04-11 19:52:28 -0300
committermongo <mongo@iomega>2016-04-11 19:52:28 -0300
commitbb8c6cb8463afe6bc93fd01479124f5aa301e6e4 (patch)
treebed87ad3c999a46b99ac2a9d3ccc64e7c8a93a6c /src.scim2/file.c
parent91433311f06a81a42da697daff0878f0edcc65c6 (diff)
Fix when importing large CSV files
Diffstat (limited to 'src.scim2/file.c')
-rwxr-xr-xsrc.scim2/file.c32
1 files changed, 28 insertions, 4 deletions
diff --git a/src.scim2/file.c b/src.scim2/file.c
index a544366..a78c144 100755
--- a/src.scim2/file.c
+++ b/src.scim2/file.c
@@ -774,7 +774,6 @@ int import_csv(char * fname, char d) {
//int pid = 0;
//int rfd = STDOUT_FILENO;
int r = 0, c = 0;
- char line_in[ABSMAXCOLS * COLS];
wchar_t line_interp[FBUFLEN] = L"";
char * token;
@@ -790,6 +789,11 @@ int import_csv(char * fname, char d) {
}
loading = 1;
+ // Check max length of line
+ int max = max_length(f);
+ char line_in[max];
+ rewind(f);
+
// CSV file traversing
while ( ! feof(f) && (fgets(line_in, sizeof(line_in), f) != NULL) ) {
// this hack is for importing file that have DOS eol
@@ -1054,14 +1058,34 @@ void export_delim(char * fname, char coldelim, int r0, int c0, int rn, int cn) {
}
/* unspecial (backquote -> ") things that are special chars in a table */
-void unspecial(FILE *f, char *str, int delim) {
- int backquote = 0;
+void unspecial(FILE * f, char * str, int delim) {
+ int backquote = 0;
if (str_in_str(str, ",") != -1) backquote = 1;
if (backquote) putc('\"', f);
while (*str) {
- putc(*str, f);
+ putc(*str, f);
str++;
}
if (backquote) putc('\"', f);
}
+
+// check max length of lines in a file
+// FILE * f shall be opened.
+int max_length(FILE * f) {
+ int count = 0, max = 0;
+ int c = fgetc(f);
+
+ while (c != EOF) {
+ if (c != '\n') {
+ count++;
+ } else if (count > max) {
+ max = count;
+ count = 0;
+ } else {
+ count = 0;
+ }
+ c = fgetc(f);
+ }
+ return max + 1;
+}