Finding end of line (EOL) in windows platform

Hello Free Java World,

I have a problem with finding end of line in text pane with Windows platform.

I use a caret listener to get position of cursor

Here is my code to count line number in text pane

publicvoid caretUpdate(CaretEvent e){

int lineNum = getLineNum(e.getDot());

statusLine.setLineNum(lineNum);

statusLine.updateStatus();

}

//

protectedint getLineNum(finalint dot){

int pos=0;

int lineNum = 0;

boolean stop =false;

String s = editZone.getText();

String nlf = System.getProperty("line.separator");

debug("nlf long : "+nlf.length());

int lenNlf = nlf.length();

do{

//pos = s.indexOf("\n",pos);

pos = s.indexOf(nlf,pos);//win

if (pos == -1){// NL not found we are at EOT

debug("EOT");

stop =true;

}

elseif (pos < dot){// cursor is on the next lines

pos += lenNlf;// next NL

debug(" next NL : dot - pos "+dot+"-"+pos);

}

elseif (pos > dot){// we are in the line

debug(" in the line : dot - pos "+dot+"-"+pos);

stop=true;

}

elseif (pos == dot){// cursor at EOL

debug(" EOL : dot - pos "+dot+"-"+pos);

stop =true;

}

lineNum++;

debug(" lineNum "+lineNum);

}

while (!stop);

return lineNum;

}

This code works fine with my linux box but under Win EOL's length is 2 characters and the function "indexOf(nlf,pos)" behave strangely. it reports EOL in a wrong way.

For example in this text :

123456

234567

345678

456789

567890

Caret in first line : indexOf(nlf,pos) reports : 6

Caret in second line : indexOf(nlf,pos) reports : 14

Caret in third line : indexOf(nlf,pos) reports : 22

Caret in fourth line : indexOf(nlf,pos) reports : 30

...

I wonder if caret position mecanism ignore EOL's length ?

In this case caret position must be computed before use.

Thanks for your help

[3761 byte] By [yeshouaa] at [2007-11-27 1:35:01]
# 1

Well, in fact caret listener don't care about memory representation of text viewed.

Then we have a difference between caret position and position of finding substring in text.

We have to translate real world (text postion with string functions) in user viewed world (caret position)

For Unix no problem, because NL is one char length, but in windows world transformation is necessary.

Here is the new code

public void caretUpdate(CaretEvent e) {

int lineNum = getLineNum(e.getDot());

statusLine.setLineNum(lineNum);

statusLine.updateStatus();

}

// adapt if necessary pos of NL found in respect of position of caret listener (user view)

protected int getLineNum(final int dot) {

int pos=0; // real pos of NL

int mpos = 0; // modified pos of NL

int lineNum = 0;

boolean stop = false;

boolean notUnix = false;

String s = editZone.getText();

String nlf = System.getProperty("line.separator");

int lenNlf = nlf.length();

if (lenNlf > 1)

notUnix = true;

do{

pos = s.indexOf(nlf,pos);

mpos = pos;

if (notUnix)

mpos -=lineNum;// particular case of Win's length of 2 Chars

if (mpos <= -1){ // NL not found we are at EOT

stop =true;

}

else if (mpos < dot){ // cursor is on the next lines

pos++; // next NL

}

else if (mpos > dot){ // we are in the line

stop=true;

}

else if (mpos == dot){ // cursor at EOL

stop = true;

}

lineNum++;

}

while (!stop);

return lineNum;

}

yeshouaa at 2007-7-12 0:42:59 > top of Java-index,Java Essentials,Java Programming...