File: ppm_comment.c

package info (click to toggle)
gnuift 0.1.14%2Bds-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 5,632 kB
  • ctags: 2,973
  • sloc: cpp: 15,867; sh: 8,281; ansic: 1,812; perl: 1,007; php: 651; makefile: 483; lisp: 344
file content (55 lines) | stat: -rw-r--r-- 1,299 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <math.h>
#include <string.h>
#include <unistd.h>

#include "ppm.h"

/* PPM and PGM utilities */

/* David Squire 980924 */

void add_comment(PPM *the_ppm, char *the_comment) {

	char *original_comments = NULL, *c;
	char *new_comment;
	int i = 0, j;

	if (the_comment[0] != '#') {
		new_comment = (char *)malloc((strlen(the_comment) + 3)*sizeof(char));
		new_comment[0] = '#';
		new_comment[1] = ' ';
		strcpy(&(new_comment[2]), the_comment);
	}
	else {
		new_comment = (char *)malloc((strlen(the_comment) + 1)*sizeof(char));
		strcpy(new_comment, the_comment);
	}

	if (the_ppm->comment_length != 0) {
		original_comments = malloc(the_ppm->comment_length*sizeof(char));
		c = the_ppm->comments;
		while (*c != '\0')
			original_comments[i++] = *c++;
		original_comments[i] = '\0';
	}

	if (the_ppm->comments != NULL)
		free(the_ppm->comments);

	the_ppm->comment_length = i + strlen(new_comment);
	the_ppm->comments = malloc(the_ppm->comment_length*sizeof(char));
	i = 0;
	if (original_comments != NULL) {
		c = original_comments;
		while (*c != '\0')
			the_ppm->comments[i++] = *c++;
	}
	for (j = 0; j < strlen(new_comment); j++, i++)
		the_ppm->comments[i] = new_comment[j];
	the_ppm->comments[i] = '\0';

	free(new_comment);
}