Applied patch from Brent Priddy <brent.priddy@adtran.com> to handle the
special-case of using newlines as field delimiters.
This commit is contained in:
parent
9028e2c96a
commit
0053087587
@ -102,10 +102,10 @@ static void decompose_list(const char *list)
|
|||||||
static void cut_file(FILE *file)
|
static void cut_file(FILE *file)
|
||||||
{
|
{
|
||||||
char *line;
|
char *line;
|
||||||
|
unsigned int cr_hits = 0;
|
||||||
|
|
||||||
/* go through every line in the file */
|
/* go through every line in the file */
|
||||||
for (line = NULL; (line = get_line_from_file(file)) != NULL; free(line)) {
|
for (line = NULL; (line = get_line_from_file(file)) != NULL; free(line)) {
|
||||||
|
|
||||||
/* cut based on chars/bytes */
|
/* cut based on chars/bytes */
|
||||||
if (part == 'c' || part == 'b') {
|
if (part == 'c' || part == 'b') {
|
||||||
int i;
|
int i;
|
||||||
@ -129,34 +129,47 @@ static void cut_file(FILE *file)
|
|||||||
char *start = line;
|
char *start = line;
|
||||||
unsigned int delims_hit = 0;
|
unsigned int delims_hit = 0;
|
||||||
|
|
||||||
for (ptr = line; (ptr = strchr(ptr, delim)) != NULL; ptr++) {
|
if (delim == '\n') {
|
||||||
delims_hit++;
|
cr_hits++;
|
||||||
if (delims_hit == (startpos - 1)) {
|
if (cr_hits >= startpos && cr_hits <= endpos) {
|
||||||
start = ptr+1;
|
while (*start && *start != '\n') {
|
||||||
}
|
|
||||||
if (delims_hit == endpos) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/* we didn't hit any delimeters */
|
|
||||||
if (delims_hit == 0 && !supress_non_delimited_lines) {
|
|
||||||
fputs(line, stdout);
|
|
||||||
}
|
|
||||||
/* we =did= hit some delimiters */
|
|
||||||
else if (delims_hit > 0) {
|
|
||||||
/* we have a fixed end point */
|
|
||||||
if (ptr) {
|
|
||||||
while (start < ptr) {
|
|
||||||
fputc(*start, stdout);
|
fputc(*start, stdout);
|
||||||
start++;
|
start++;
|
||||||
}
|
}
|
||||||
fputc('\n', stdout);
|
fputc('\n', stdout);
|
||||||
}
|
}
|
||||||
/* or we're just going til the end of the line */
|
}
|
||||||
else {
|
else {
|
||||||
while (*start) {
|
for (ptr = line; (ptr = strchr(ptr, delim)) != NULL; ptr++) {
|
||||||
fputc(*start, stdout);
|
delims_hit++;
|
||||||
start++;
|
if (delims_hit == (startpos - 1)) {
|
||||||
|
start = ptr+1;
|
||||||
|
}
|
||||||
|
if (delims_hit == endpos) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* we didn't hit any delimeters */
|
||||||
|
if (delims_hit == 0 && !supress_non_delimited_lines) {
|
||||||
|
fputs(line, stdout);
|
||||||
|
}
|
||||||
|
/* we =did= hit some delimiters */
|
||||||
|
else if (delims_hit > 0) {
|
||||||
|
/* we have a fixed end point */
|
||||||
|
if (ptr) {
|
||||||
|
while (start < ptr) {
|
||||||
|
fputc(*start, stdout);
|
||||||
|
start++;
|
||||||
|
}
|
||||||
|
fputc('\n', stdout);
|
||||||
|
}
|
||||||
|
/* or we're just going til the end of the line */
|
||||||
|
else {
|
||||||
|
while (*start) {
|
||||||
|
fputc(*start, stdout);
|
||||||
|
start++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
61
cut.c
61
cut.c
@ -102,10 +102,10 @@ static void decompose_list(const char *list)
|
|||||||
static void cut_file(FILE *file)
|
static void cut_file(FILE *file)
|
||||||
{
|
{
|
||||||
char *line;
|
char *line;
|
||||||
|
unsigned int cr_hits = 0;
|
||||||
|
|
||||||
/* go through every line in the file */
|
/* go through every line in the file */
|
||||||
for (line = NULL; (line = get_line_from_file(file)) != NULL; free(line)) {
|
for (line = NULL; (line = get_line_from_file(file)) != NULL; free(line)) {
|
||||||
|
|
||||||
/* cut based on chars/bytes */
|
/* cut based on chars/bytes */
|
||||||
if (part == 'c' || part == 'b') {
|
if (part == 'c' || part == 'b') {
|
||||||
int i;
|
int i;
|
||||||
@ -129,34 +129,47 @@ static void cut_file(FILE *file)
|
|||||||
char *start = line;
|
char *start = line;
|
||||||
unsigned int delims_hit = 0;
|
unsigned int delims_hit = 0;
|
||||||
|
|
||||||
for (ptr = line; (ptr = strchr(ptr, delim)) != NULL; ptr++) {
|
if (delim == '\n') {
|
||||||
delims_hit++;
|
cr_hits++;
|
||||||
if (delims_hit == (startpos - 1)) {
|
if (cr_hits >= startpos && cr_hits <= endpos) {
|
||||||
start = ptr+1;
|
while (*start && *start != '\n') {
|
||||||
}
|
|
||||||
if (delims_hit == endpos) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/* we didn't hit any delimeters */
|
|
||||||
if (delims_hit == 0 && !supress_non_delimited_lines) {
|
|
||||||
fputs(line, stdout);
|
|
||||||
}
|
|
||||||
/* we =did= hit some delimiters */
|
|
||||||
else if (delims_hit > 0) {
|
|
||||||
/* we have a fixed end point */
|
|
||||||
if (ptr) {
|
|
||||||
while (start < ptr) {
|
|
||||||
fputc(*start, stdout);
|
fputc(*start, stdout);
|
||||||
start++;
|
start++;
|
||||||
}
|
}
|
||||||
fputc('\n', stdout);
|
fputc('\n', stdout);
|
||||||
}
|
}
|
||||||
/* or we're just going til the end of the line */
|
}
|
||||||
else {
|
else {
|
||||||
while (*start) {
|
for (ptr = line; (ptr = strchr(ptr, delim)) != NULL; ptr++) {
|
||||||
fputc(*start, stdout);
|
delims_hit++;
|
||||||
start++;
|
if (delims_hit == (startpos - 1)) {
|
||||||
|
start = ptr+1;
|
||||||
|
}
|
||||||
|
if (delims_hit == endpos) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* we didn't hit any delimeters */
|
||||||
|
if (delims_hit == 0 && !supress_non_delimited_lines) {
|
||||||
|
fputs(line, stdout);
|
||||||
|
}
|
||||||
|
/* we =did= hit some delimiters */
|
||||||
|
else if (delims_hit > 0) {
|
||||||
|
/* we have a fixed end point */
|
||||||
|
if (ptr) {
|
||||||
|
while (start < ptr) {
|
||||||
|
fputc(*start, stdout);
|
||||||
|
start++;
|
||||||
|
}
|
||||||
|
fputc('\n', stdout);
|
||||||
|
}
|
||||||
|
/* or we're just going til the end of the line */
|
||||||
|
else {
|
||||||
|
while (*start) {
|
||||||
|
fputc(*start, stdout);
|
||||||
|
start++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user