exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

test-custom-write.c (2935B)


      1 /*
      2  Author: José Bollo <jobol@nonadev.net>
      3 
      4  https://gitlab.com/jobol/mustach
      5 
      6  Licensed under the Apache License, Version 2.0 (the "License");
      7  you may not use this file except in compliance with the License.
      8  You may obtain a copy of the License at
      9 
     10      http://www.apache.org/licenses/LICENSE-2.0
     11 
     12  Unless required by applicable law or agreed to in writing, software
     13  distributed under the License is distributed on an "AS IS" BASIS,
     14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15  See the License for the specific language governing permissions and
     16  limitations under the License.
     17 */
     18 
     19 #ifndef _GNU_SOURCE
     20 #define _GNU_SOURCE
     21 #endif
     22 
     23 #include <stdlib.h>
     24 #include <stdio.h>
     25 #include <ctype.h>
     26 #include <sys/stat.h>
     27 #include <unistd.h>
     28 #include <fcntl.h>
     29 #include <string.h>
     30 #include <libgen.h>
     31 
     32 #include "../mustach-json-c.h"
     33 
     34 static const size_t BLOCKSIZE = 8192;
     35 
     36 static char *readfile(const char *filename)
     37 {
     38 	int f;
     39 	struct stat s;
     40 	char *result, *ptr;
     41 	size_t size, pos;
     42 	ssize_t rc;
     43 
     44 	result = NULL;
     45 	if (filename[0] == '-' &&  filename[1] == 0)
     46 		f = dup(0);
     47 	else
     48 		f = open(filename, O_RDONLY);
     49 	if (f < 0) {
     50 		fprintf(stderr, "Can't open file: %s\n", filename);
     51 		exit(1);
     52 	}
     53 
     54 	fstat(f, &s);
     55 	switch (s.st_mode & S_IFMT) {
     56 	case S_IFREG:
     57 		size = s.st_size;
     58 		break;
     59 	case S_IFSOCK:
     60 	case S_IFIFO:
     61 		size = BLOCKSIZE;
     62 		break;
     63 	default:
     64 		fprintf(stderr, "Bad file: %s\n", filename);
     65 		exit(1);
     66 	}
     67 
     68 	pos = 0;
     69 	result = malloc(size + 1);
     70 	do {
     71 		if (result == NULL) {
     72 			fprintf(stderr, "Out of memory\n");
     73 			exit(1);
     74 		}
     75 		rc = read(f, &result[pos], (size - pos) + 1);
     76 		if (rc < 0) {
     77 			fprintf(stderr, "Error while reading %s\n", filename);
     78 			exit(1);
     79 		}
     80 		if (rc > 0) {
     81 			pos += (size_t)rc;
     82 			if (pos > size) {
     83 				size = pos + BLOCKSIZE;
     84 				ptr = realloc(result, size + 1);
     85 				if (!ptr)
     86 					free(result);
     87 				result = ptr;
     88 			}
     89 		}
     90 	} while(rc > 0);
     91 
     92 	close(f);
     93 	result[pos] = 0;
     94 	return result;
     95 }
     96 
     97 enum { None, Upper, Lower } mode = None;
     98 
     99 int uwrite(void *closure, const char *buffer, size_t size)
    100 {
    101 	switch(mode) {
    102 	case None:
    103 		fwrite(buffer, size, 1, stdout);
    104 		break;
    105 	case Upper:
    106 		while(size--)
    107 			fputc(toupper(*buffer++), stdout);
    108 		break;
    109 	case Lower:
    110 		while(size--)
    111 			fputc(tolower(*buffer++), stdout);
    112 		break;
    113 	}
    114 	return 0;
    115 }
    116 
    117 int main(int ac, char **av)
    118 {
    119 	struct json_object *o;
    120 	char *t;
    121 	char *prog = *av;
    122 	int s;
    123 
    124 	if (*++av) {
    125 		o = json_object_from_file(av[0]);
    126 		if (o == NULL) {
    127 			fprintf(stderr, "Aborted: null json (file %s)\n", av[0]);
    128 			exit(1);
    129 		}
    130 		while(*++av) {
    131 			if (!strcmp(*av, "-U"))
    132 				mode = Upper;
    133 			else if  (!strcmp(*av, "-l"))
    134 				mode = Lower;
    135 			else if  (!strcmp(*av, "-x"))
    136 				mode = None;
    137 			else {
    138 				t = readfile(*av);
    139 				s = mustach_json_c_write(t, 0, o, Mustach_With_AllExtensions, uwrite, NULL);
    140 				if (s != 0)
    141 					fprintf(stderr, "Template error %d\n", s);
    142 				free(t);
    143 			}
    144 		}
    145 		json_object_put(o);
    146 	}
    147 	return 0;
    148 }
    149