Skip to content

Instantly share code, notes, and snippets.

@xslim
Last active February 14, 2019 16:38
Show Gist options
  • Save xslim/2569995134ddc37c207bcc20b9bf6559 to your computer and use it in GitHub Desktop.
Save xslim/2569995134ddc37c207bcc20b9bf6559 to your computer and use it in GitHub Desktop.
Trim receipt line in C

Before

40|           Oracle HGBU HDS EMEA          
40|             VAT: 123 456 789            
40| 440010051  UK Manager                   
40| ----------------------------------------
40| CHK 1005                         TBL 9/1
40| ----------------------------------------
40|  1 Poached Pear                     4.50
40|  1 Poached Pear                     4.50
40|  1 Soup of the Week                 4.95
40|  1 Fish Terrine                     5.60
40|  1 Pate                             5.00
40|      White Toast                        
40|  1 Poached Pear                     4.50
40|  1 Poached Pear                     4.50
40|  1 Fish Terrine                     5.60
40|  1 Fish Terrine                     5.60
40|  1 Soup of the Week                 4.95
40|  1 Poached Pear                     4.50
40|  1 Fish Terrine                     5.60
40|  1 Soup of the Week                 4.95
52|    Food                           €64.75
32| Total Due     €64.75
40|        Thank You for your Custom        
40|            Oracle Hospitality           
40|              www.oracle.com          

After

32|     Oracle HGBU HDS EMEA        
32|     VAT: 123 456 789            
32| 440010051  UK Manager           
32| --------------------------------
32| CHK 1005                 TBL 9/1
32| --------------------------------
32|  1 Poached Pear             4.50
32|  1 Poached Pear             4.50
32|  1 Soup of the Week         4.95
32|  1 Fish Terrine             5.60
32|  1 Pate                     5.00
32|     White Toast                 
32|  1 Poached Pear             4.50
32|  1 Poached Pear             4.50
32|  1 Fish Terrine             5.60
32|  1 Fish Terrine             5.60
32|  1 Soup of the Week         4.95
32|  1 Poached Pear             4.50
32|  1 Fish Terrine             5.60
32|  1 Soup of the Week         4.95
32|    Food       €64.75
32| Total Due     €64.75
32|     Thank You for your Custom   
32|     Oracle Hospitality          
32|     www.oracle.com     
// 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment