//
//  CSTextReader.m
//  duality4
//
//  Created by Colin Cornaby on Fri Feb 15 2002.
//  Copyright (c) 2001 Colin Cornaby. All rights reserved.
//

#import "CSTextReader.h"

// This must be included for C string functions
#include <string.h>


@implementation CSTextReader
+ (id)getLine:(NSString *)stringval:(int)lineNumber{
    
    /********************************
    The new variables needed
    
    stringValAsCCharArray - will hold a C stle string of the entire text to be parsed.
    newStringVal - temporary string to be added to the array as the string is tokenized
    theArray - a mutable string where each object is one line of text
    tokenPoint - a pointer to the location within the C string for the start of a new partial string.
    */
    char * stringValAsCCharArray, *tokenPoint;
    NSString	*newStringVal;
    NSMutableArray	*theArray;
    
    //********************************
    // New segment of code
    //
    theArray = [[NSMutableArray alloc] init];
    stringValAsCCharArray = malloc([stringval length]+1);
    [stringval getCString:stringValAsCCharArray];
    tokenPoint= strtok(stringValAsCCharArray,"\r\n");
    newStringVal = [NSString stringWithCString:tokenPoint];
    [theArray addObject:newStringVal];
    while(tokenPoint=strtok(NULL,"\r\n"))
    {
        newStringVal = [NSString stringWithCString:tokenPoint];
        [theArray addObject:newStringVal];
    }
    free(stringValAsCCharArray);
    //     
    //Until this point
    //********************************
    
   // NSLog(@"%@",[theArray description]);
    return [[theArray autorelease] objectAtIndex:(lineNumber)];
}

+ (int)linetotal:(NSString *)stringval{
    
    /********************************
    The new variables needed
    
    stringValAsCCharArray - will hold a C stle string of the entire text to be parsed.
    newStringVal - temporary string to be added to the array as the string is tokenized
    theArray - a mutable string where each object is one line of text
    tokenPoint - a pointer to the location within the C string for the start of a new partial string.
    */
    char * stringValAsCCharArray, *tokenPoint;
    NSString	*newStringVal;
    NSMutableArray	*theArray;
    
    //********************************
    // New segment of code
    //
    theArray = [[NSMutableArray alloc] init];
    stringValAsCCharArray = malloc([stringval length]+1);
    [stringval getCString:stringValAsCCharArray];
    tokenPoint= strtok(stringValAsCCharArray,"\r\n");
    newStringVal = [NSString stringWithCString:tokenPoint];
    [theArray addObject:newStringVal];
    while(tokenPoint=strtok(NULL,"\r\n"))
    {
        newStringVal = [NSString stringWithCString:tokenPoint];
        [theArray addObject:newStringVal];
    }
    free(stringValAsCCharArray);
    
    return [[theArray autorelease] count];

}

@end
