|
// Created by Taras Kalapun on 14/02/2019. |
|
|
|
#include <stdio.h> |
|
#include <stdlib.h> |
|
#include <string.h> |
|
#include <ctype.h> |
|
|
|
char *receipt_trim(char *str, int max_n) { |
|
int size = (int)strlen(str); |
|
int chars_to_trim = size - max_n; |
|
int pos_char_start = 0; |
|
int pos_char_end = size - 1; |
|
int pos_char_end_n = 0; |
|
int i; |
|
|
|
while(pos_char_start < size && isspace(str[pos_char_start])) pos_char_start++; |
|
while(pos_char_end > 0 && isspace(str[pos_char_end])) pos_char_end--; |
|
pos_char_end_n = size - pos_char_end; |
|
int char_str_len = pos_char_end - pos_char_start; |
|
|
|
// printf("to trim: %i, start: %i, end:%i, %i\n", chars_to_trim, pos_char_start, pos_char_end, pos_char_end_n); |
|
|
|
// trimming from both sides |
|
if (pos_char_start > 1 && pos_char_end_n > 1 && (pos_char_start + pos_char_end_n) >= chars_to_trim) { |
|
if (pos_char_start >= chars_to_trim/2) { |
|
int shift_from = chars_to_trim/2; |
|
int shift_end = shift_from + char_str_len + chars_to_trim/2; |
|
int start = pos_char_start - shift_from; |
|
for (i=shift_from; i < shift_end; i++){ |
|
str[i] = str[i + start]; |
|
} |
|
for (; i < max_n-1; i++) str[i] = ' '; |
|
if (shift_end >= max_n) i = max_n-1; //hack |
|
str[++i] = '\0'; |
|
return str; |
|
} |
|
} else if (pos_char_start >= chars_to_trim) { |
|
int shift_from = chars_to_trim; |
|
int shift_end = shift_from + char_str_len; |
|
int start = pos_char_start - shift_from; |
|
for (i=chars_to_trim; i < shift_end; i++){ |
|
str[i] = str[i + start]; |
|
} |
|
for (; i < max_n-1; i++){ |
|
str[i] = ' '; |
|
} |
|
if (shift_end >= max_n) i = max_n-1; //hack |
|
str[++i] = '\0'; |
|
return str; |
|
} else if (pos_char_end_n >= chars_to_trim) { |
|
for (i=pos_char_end+1; i < max_n-1; i++){ |
|
str[i] = ' '; |
|
} |
|
str[++i] = '\0'; |
|
return str; |
|
} else { |
|
char *ptr; |
|
int pos = -1; |
|
char *sp_buf = (char *)malloc(chars_to_trim); |
|
|
|
memset(sp_buf, '-', chars_to_trim); |
|
sp_buf[chars_to_trim] = 0; |
|
ptr = strstr(str, sp_buf); |
|
if (ptr != NULL) { |
|
pos = (int)(ptr - str); |
|
//printf("Will trim '-' - %i\n", pos); |
|
int start = pos + chars_to_trim; |
|
int shift_end = size; |
|
for (i=pos; i < shift_end; i++){ |
|
str[i] = str[i + start]; |
|
} |
|
//str[++i] = '\0'; |
|
free(sp_buf); |
|
return str; |
|
} |
|
|
|
memset(sp_buf, ' ', chars_to_trim); |
|
sp_buf[chars_to_trim] = 0; |
|
ptr = strstr(str, sp_buf); |
|
if (ptr != NULL) { |
|
pos = (int)(ptr - str); |
|
//printf("Will trim ' ' - %i\n", pos); |
|
int start = pos + chars_to_trim; |
|
int shift_end = size; |
|
for (i=pos; i < shift_end; i++){ |
|
// printf("%c", str[i + chars_to_trim]); |
|
str[i] = str[i + chars_to_trim]; |
|
} |
|
//str[++i] = '\0'; |
|
free(sp_buf); |
|
return str; |
|
} |
|
free(sp_buf); |
|
} |
|
return str; |
|
} |
|
|
|
int main(int argc, const char * argv[]) { |
|
char str_in[] = " Oracle HGBU HDS EMEA # VAT: 123 456 789 #440010051 UK Manager #----------------------------------------#CHK 1005 TBL 9/1#----------------------------------------# 1 Poached Pear 4.50# 1 Poached Pear 4.50# 1 Soup of the Week 4.95# 1 Fish Terrine 5.60# 1 Pate 5.00# White Toast # 1 Poached Pear 4.50# 1 Poached Pear 4.50# 1 Fish Terrine 5.60# 1 Fish Terrine 5.60# 1 Soup of the Week 4.95# 1 Poached Pear 4.50# 1 Fish Terrine 5.60# 1 Soup of the Week 4.95# Food €64.75#Total Due €64.75# Thank You for your Custom # Oracle Hospitality # www.oracle.com #"; |
|
|
|
char *token; |
|
char *rstr; |
|
const char sep_nl[] = "#"; |
|
token = strtok(str_in, sep_nl); |
|
|
|
while(token != NULL){ |
|
//printf("%i| %s\n", (int)strlen(token), token); |
|
rstr = receipt_trim(token, 32); |
|
printf("%i| %s |\n", (int)strlen(rstr), rstr); |
|
token = strtok(NULL, sep_nl); |
|
} |
|
return 0; |
|
} |