1### Copyright (c) 2007-, Tyzx Corporation. All rights reserved.
2
3### [isFmt, fmtContents, labelFmt] = _g_parse_oct_fmt (FMT, plotn, wantLabel)
4###
5### Take a string FMT, e.g. "-5;Foobar;", in octave's plot (x,y,FMT) format and
6### tries to translate it into fmtContents, a gnuplot string like
7###
8###     "with lines linetype 5 title 'Foobar'".
9##
10### isFmt is 1 if FMT makes sense in octave's plot syntax, 0 otherwise.
11###
12### If the wantLabel is true, or if the "format" part of FMT ("-5", in the
13### example above) contains a "#", then labelFmt will hold a gnuplot string that
14### plots a label with each data point.
15function [isFmt, fmtContents, labelFmt] = _g_parse_oct_fmt (str,plotn, wantLabel)
16
17  if nargin<2, plotn =     0; endif
18  if nargin<3, wantLabel = 1; endif
19
20  isFmt =        0;
21  defaultTitle = "";
22  if plotn, defaultTitle = sprintf ("Line %i",plotn); endif
23
24  fmtContents = sprintf ("with lines lt %i title '%s'", plotn, defaultTitle);
25
26  labelFmt = "";
27  if (iscell (wantLabel) && !isempty (wantLabel)) || (!iscell (wantLabel) && wantLabel)
28    wantLabel = 1;
29    labelFmt = sprintf ("with labels tc lt %i offset 0,1 notitle ", plotn);
30  end
31
32  if !ischar(str)
33    return;
34  endif
35
36  ## Matches style, color, key
37  ##re = "^(\\.|@|-[@+*x]?|[L\\+\\*ox\\^]|)([1-6][1-6]?|[krgbmcw]|)(;[^;]*;|)$";
38  re10 = "^(\\.|@|-[@\\+\\*xo]?|[L\\+\\*ox\\^]|)([#1-6krgbmcw][1-6]?|)";
39  re11 = "^(with[^;]*)";
40  re2 = "(;[^;]*;)$";
41  ## re seems to fail on ';aaa;'
42  ##[S, E, TE, M, T, NM] = regexp (str, re)
43
44  style = "with lines lw 5 ";
45  color = "";
46  title = "title ''";
47  ##pointstyle = ""; Unused
48
49  T = regexp (str, re11, "tokens"); # Try gnuplot-like style
50  if ! isempty (T)
51    ## workaround for regexp's bug
52    if ! isempty (T) && length (T{1}) == 1
53      T{1} = {"",T{1}{:}};
54    end
55    style = T{1}{2};
56    color = "";
57    title = "";
58  else
59    T = regexp (str, re10, "tokens");
60
61    if !isempty (T)
62      ## workaround for regexp's bug
63      if ! isempty (T) && length (T{1}) == 1
64	T{1} = {"",T{1}{:}};
65      end
66
67      xt = " lw 3 ";
68				# TODO: more correct way of dealing w/ T{1}{1}
69      style0 = "lines";
70      ##T{1}{1}
71      if length (T{1}{1})		# Style
72	switch T{1}{1}
73	  case "-+", style0 = "linespoints"; xt = " pointtype 1 ";
74	  case "-*", style0 = "linespoints"; xt = " pointtype 3 ";
75	  case "-o", style0 = "linespoints"; xt = " pointtype 6 ";
76	  case "-@", style0 = "linespoints"; xt = " pointtype 6 ";
77	  case "^",  style0 = "impulses";    xt = "";
78	  case "@",  style0 = "points";      xt = " pointtype 6 ";
79	  case "*",  style0 = "points";      xt = " pointtype 3 ";
80	  case "+",  style0 = "points";      xt = " pointtype 1 ";
81	  case "x",  style0 = "points";      xt = " pointtype 2 ";
82	  case "o",  style0 = "points";      xt = " pointtype 6 ";
83	  case "-",  style0 = "lines";       xt = "";
84	  case "L",  style0 = "steps";       xt = "";
85	  case ".",  style0 = "dots";        xt = "";
86	  otherwise  style0 = "lines";       xt = " lw 3 ";
87	endswitch
88      endif
89      style = ["with ",style0, xt];
90
91      if length (T{1}{2})		# Label wanted?
92	if index (T{1}{2}, "#")
93	  wantLabel = 1;
94	  T{1}{2} = T{1}{2}(T{1}{2} != "#");
95	end			# Now, there is no "#" in T{1}{2}, so T{1}{2}
96				# should be color and/or point
97      end				#
98      if length (T{1}{2})		# Color (and maybe pointstyle)
99				# First character is color, as number or letter
100
101	if index ("123456789", T{1}{2}(1))
102	  color = T{1}{2}(1);
103	else			# Convert letter to digit
104	  color = struct(qw("k 6 r 1 g 2 b 3 m 4 c 5 w 7"){:}).(T{1}{2}(1));
105	endif
106	if length (T{1}{2}) > 1 && index ("123456789", T{1}{2}(2))
107	  ## pointstyle = ["pt ",T{1}{2}(2)];
108	endif
109	if strcmp (style0, "lines")
110	  color = ["ls ",color];
111	else
112	  color = ["lt ",color];
113	end
114      endif
115    end				# EOF !isempty (T): Matlab-like style spec
116  endif
117  T = regexp (str, re2, "tokens");
118
119  if !isempty (T)
120    if length (T{1}{1})		# title (aka key, label)
121      title = ["title '",T{1}{1}(2:end-1),"'"];
122    endif
123  endif
124  ##fmtContents = [style," ",color," ",title," lw 3 "];
125  fmtContents = [style," ",color," ",title];
126  isFmt = 1;
127
128  if iscell (wantLabel)
129    wantLabel = ! isempty (wantLabel);
130  end
131  if wantLabel
132    labelColor = "";
133    if length (color)
134      labelColor = [" tc ", color];
135    end
136
137    labelFmt = ["with labels ",labelColor," offset 0,1 notitle "];
138  end
139
140endfunction
141