aboutsummaryrefslogtreecommitdiff
path: root/vo/votools/prettystr.cl
blob: a9915af5c1878b4bd40d1c3507378acbb7201287 (plain) (blame)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#{  PRETTYSTR -- Pretty-print a string.  A pretty abusive way to use the
#   CL string functions.

procedure prettystr (instr)

struct	instr				{ prompt = "Input string"	}

int	start_col = 18			{ prompt = "Starting column"	}
int	end_col   = 75			{ prompt = "Ending column"	}
bool	comment   = no			{ prompt = "Comment string?"	}

begin
	struct  ip, op, cp
	string  spaces
	int     scol, ecol, lnum
	int     sp, ep, oep, slen, ncols
 	bool	done, comm

	scol = start_col
	ecol = end_col + 2
	ip   = instr	
	comm = comment
	if (comm)
	    scol = scol + 1

	lnum   = 1
	done   = no
	spaces = "                                                      "
	op     = ""

	slen = strlen (ip)
	if (slen >= 511)
	    slen = 512
	ncols= (ecol - scol + 1)
	sp   = 1
	ep   = ncols
	oep  = 1


	# Handle the easy case first.  If it's a short string or there are
	# no spaces just print it.
	if (slen <= ep) {
	    print (ip)

	} else {
	    while (!done) {
		# Get char at ending guess.
		cp = substr (ip,ep,ep)
		if (cp != " " && cp != "\t" && cp != "\n" && cp != ",") {
		    # back up to the previous whitespace
		    while (ep > oep) {
		        cp = substr (ip,ep,ep)
			if (cp == " " || cp == "\t" || cp == "\n" || cp == ",")
			    break
			else
			    ep = ep - 1
		    }
		    if (ep <= oep) {
			# Long string, no commas, just dump it
	    		print (ip)
			return
		    }
		}

		# Whitespace - break here
		line = substr (ip, sp, ep)
		printf ("%s\n", line)
		if (comm) printf ("#")
		if (lnum >= 1)
		    printf ("%s", substr (spaces, 1, (scol-1)))

		# Update pointers
		sp = ep + 1
		if ((ep + ncols) > slen) {
		    # last chunk
		    ep = slen
		    line = substr (ip, sp, ep)
		    printf ("%s\n", line)

		    done = yes

		} else {
		    oep = ep
		    ep = ep + ncols
		}
		lnum = lnum + 1
	    }
	}
end