Revisiting Line Count

This is just a re-post from my comments on EggHead thread about line counts which I was using in an HTML Editor. I want to improve this code but for its intended purposes was functional. The WYSIWIG was the old Free Textbox from days of yor … The objective was to give Admins the ability to set the count and place those page breaks as the Tech or Transcriptionist  entered the report. Und Zo !!

Original post >>>>

Ok so, I need a line count that is consistent. this is giving me only relatively consistent results.
Have a look and give me your suggestions. Happy Coding.
<>>>>>>>>>>>>>>>>>>>>
public string PageBreaksHere(string inputText, string lineCount)
{
string strprocess = inputText;
string targId = Guid.NewGuid().ToString();
string filetoprocess = “wysi” + Left(Regex.Replace(targId, “-“, “”), 7);
string pcount = lineCount; //<<<< From database or Config
if (!(CInt(pcount.Length) < 1))
{
_linecount = CInt(pcount);
}
_tempString = inputText;
String strOut = ProcessInputRequest(Createfile(strprocess, filetoprocess));
RemoveTempFile();
return strOut;
}
string RemoveBreaks()
{
return _tempString;
}
protected void RemoveTempFile()
{
FileInfo f = new FileInfo(_targetFile);
f.Delete();
}
//End Firing Methods <<<<<<<<<<<<<<<<<<<<<< 0 | s.LastIndexOf(” “) > 0 | s.LastIndexOf(“\r”) > 0
| s.LastIndexOf(“\r\n”) > 0
| s.LastIndexOf(“\t”) > 0 | s.LastIndexOf(”
“) > 0
| s.LastIndexOf(ch0) > 0
| s.LastIndexOf(ch1) > 0 | s.LastIndexOf(ch1 +””+ ch0) > 0
| s.LastIndexOf(ch0 +””+ ch1) > 0 | s.LastIndexOf(Environment.NewLine) > 0)
{
if (IsPageBreak(Convert.ToString(i)))
{
sb.Append(“—– Page Break —–“);
}
++i;
}
}
sr.Close();
return sb.ToString();
}
string Createfile(string input, string nameoffile)
{
// string tpath = Path.GetTempPath().ToString();
string path = HttpContext.Current.Server.MapPath(@”\EMP_temp\”);
string fname = nameoffile + @”.txt”;
string fpath = path + fname;
_targetFile = fpath; // Creating this global to wack later: quote() == Tony Saprano.
FileInfo f = new FileInfo(fpath);
if (!f.Exists)
{
DirectoryInfo di = Directory.CreateDirectory(path);
f.Create().Close();
StreamWriter sw2 = f.AppendText();
sw2.WriteLine(input);
sw2.Close();
//f.Delete();
}
return f.FullName.ToString();
}
protected bool IsWholeNumber(string d)
{
return Regex.IsMatch(d, @”^[-+]?\d*$”);
}
protected bool IsPageBreak(string checkval)
{
bool result = false;
int dresult = (Convert.ToInt32(checkval) / _linecount);
result = IsWholeNumber(Convert.ToString(dresult));
return result;
}
}

Advertisements

One comment

  1. Still quite not quite ready for prime time but I didnt want to lose this code so I will share:

    public class PageBreakImplied
    {
    // Fields
    private int _linecount;
    public bool _pagebreakIsOn;
    private string _targetFile = string.Empty;
    private string _tempString = string.Empty;

    // Methods
    private static int CInt(object o)
    {
    return Convert.ToInt32(o);
    }

    private string Createfile(string input, string nameoffile)
    {
    string path = HttpContext.Current.Server.MapPath(@”\EMP_temp\”);
    string str2 = nameoffile + “.txt”;
    string fileName = path + str2;
    this._targetFile = fileName;
    FileInfo info = new FileInfo(fileName);
    if (!info.Exists)
    {
    Directory.CreateDirectory(path);
    info.Create().Close();
    StreamWriter writer = info.AppendText();
    writer.WriteLine(input);
    writer.Close();
    }
    return info.FullName.ToString();
    }

    protected bool IsPageBreak(string checkval)
    {
    bool flag = false;
    if (this.Modulus(CInt(checkval), this._linecount) == 0)
    {
    flag = true;
    }
    return flag;
    }

    public string Left(string param, int length)
    {
    return param.Substring(0, length);
    }

    private int Modulus(int n, int p)
    {
    return (n % p);
    }

    public string PageBreaksHere(string inputText, string lineCount)
    {
    string input = inputText;
    string str2 = Guid.NewGuid().ToString();
    string nameoffile = “wysi” + this.Left(Regex.Replace(str2, “-“, “”), 7);
    string o = lineCount;
    if (CInt(o.Length) >= 1)
    {
    this._linecount = CInt(o);
    }
    this._tempString = inputText;
    string str5 = this.ProcessInputRequest(this.Createfile(input, nameoffile));
    this.RemoveTempFile();
    return str5;
    }

    private string ProcessInputRequest(string filename)
    {
    StreamReader reader = new FileInfo(filename).OpenText();
    string str = “”;
    string str2 = Convert.ToString(‘\n’);
    string str3 = Convert.ToString(‘\r’);
    StringBuilder builder = new StringBuilder();
    int num = 0;
    while ((str = reader.ReadLine()) != null)
    {
    num++;
    builder.Append(str);
    if (((((((((((str.LastIndexOf(“\n”) > 0) | (str.LastIndexOf(” “) > 0)) | (str.LastIndexOf(“\r”) > 0)) | (str.LastIndexOf(“\r\n”) > 0)) | (str.LastIndexOf(“\t”) > 0)) | (str.LastIndexOf(“
    “) > 0)) | (str.LastIndexOf(str2) > 0)) | (str.LastIndexOf(str3) > 0)) | (str.LastIndexOf(str3 + “” + str2) > 0)) | (str.LastIndexOf(str2 + “” + str3) > 0)) | (str.LastIndexOf(Environment.NewLine) > 0))
    {
    if (this.IsPageBreak(Convert.ToString(num)))
    {
    builder.Append(“

    —– Page Break —–

    “);
    }
    num++;
    }
    }
    reader.Close();
    return builder.ToString();
    }

    private string RemoveBreaks()
    {
    return this._tempString;
    }

    protected void RemoveTempFile()
    {
    new FileInfo(this._targetFile).Delete();
    }
    }

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s