Last active
February 19, 2018 01:24
-
-
Save bwedding/1df6f582b18b9495b976ab7095d71129 to your computer and use it in GitHub Desktop.
Ultra Fast Standard C Program to Add a Line Feed to CSV files to Transpose from All Columns to All Rows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <stdlib.h> | |
#include <stdio.h> | |
void GetLen(FILE * fp, int &len); | |
void ReadAll(char * &buf, int len, FILE * fp); | |
void ChangeExtension(char *fname, char *newExt); | |
int StrReplace(char *target, const char *needle, const char *replacement, int len); | |
int main(int c, char**v) | |
{ | |
FILE *fp = NULL; | |
int len = 0; | |
char *buf = NULL; | |
char fname[256]; | |
if (c < 2) | |
{ | |
puts("I need a filename, jackass"); | |
return 0; | |
} | |
for (int i = 1; i < c; i++) | |
{ | |
fopen_s(&fp, v[i], "r"); | |
GetLen(fp, len); | |
buf = (char*)calloc(1, len * 2); | |
if (!buf) | |
{ | |
puts("Wow! That file's too big for me. Out of memory!"); | |
return -1; | |
} | |
fread(buf, len, 1, fp); | |
fclose(fp); | |
int result = StrReplace(buf, ",", ",\n", len*2); | |
if(result) | |
{ | |
puts("Something went wrong. I have to quit!"); | |
return -1; | |
} | |
strcpy(fname, v[i]); | |
ChangeExtension(fname, "csv"); | |
fopen_s(&fp, fname, "w"); | |
fwrite(buf, strlen(buf), 1, fp); | |
fclose(fp); | |
} | |
return 0; | |
} | |
void ReadAll(char * &buf, int len, FILE * fp) | |
{ | |
buf = (char*)calloc(1, len * 2); | |
fread(buf, len, 1, fp); | |
fclose(fp); | |
} | |
void GetLen(FILE * fp, int &len) | |
{ | |
fseek(fp, 0L, SEEK_END); | |
len = ftell(fp); | |
rewind(fp); | |
} | |
void ChangeExtension(char *fname, char *newExt) | |
{ | |
#define EVAL_MAX_LEN (300) | |
size_t nLen; | |
char szOut[EVAL_MAX_LEN] = { 0 }; | |
char szPath[EVAL_MAX_LEN]; | |
strcpy(szPath, fname); | |
nLen = strlen(szPath); | |
if ((nLen > 0) && (nLen < EVAL_MAX_LEN)) | |
{ | |
while (nLen) | |
{ | |
if (szPath[nLen] == '.') | |
{ | |
szPath[nLen] = '\0'; | |
break; | |
} | |
nLen--; | |
} | |
sprintf(szOut, "%s.%s", szPath, newExt); | |
strcpy(fname, szOut); | |
} | |
return; | |
} | |
int StrReplace(char *target, const char *needle, const char *replacement, int len) | |
{ | |
char *buffer = (char*)calloc(1, len); | |
if (!buffer) | |
{ | |
puts("Wow! That file's too big for me. Out of memory!"); | |
return -1; | |
} | |
char *insert_point = &buffer[0]; | |
const char *tmp = target; | |
size_t needle_len = strlen(needle); | |
size_t repl_len = strlen(replacement); | |
while (1) | |
{ | |
const char *p = strstr(tmp, needle); | |
if (p == NULL) | |
{ | |
strcpy(insert_point, tmp); | |
break; | |
} | |
memcpy(insert_point, tmp, p - tmp); | |
insert_point += p - tmp; | |
memcpy(insert_point, replacement, repl_len); | |
insert_point += repl_len; | |
tmp = p + needle_len; | |
} | |
strcpy(target, buffer); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment