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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>
#include <windows.h>
using namespace std ;
/* these is line struct */
struct line
{
int length ;
char * str1 ;
line ( char str[] , int length )
{
// create the new arreay
str = new char [length+1] ;
// then copy it
for ( int i = 0 ; i < length ; i++ )
str1[i] = str [i];
str [length ] = 0;
this->length = length ;
}
};
/* this is the lines structure then */
struct lines
{
vector<line> * lines_vector ;
int a ;
// the constructor will create get a char* and torkernize it into the
// lines and make line objects and push them into the vector.
char * getline (int line )
{
// this will output line number 'line'
//line & requested_line ;
//requested_line = lines_vector[line];
return (lines_vector->operator[] (line) ).str1 ;
}
int get_num_lines ()
{
return lines_vector->size();
}
lines ( char * str , int length )
{
// thus we are using the lines_vector thus initialize it
lines_vector = new vector<line>;
//lines_vector = new vector<line>;
int current_position = 0 ;
int current_line_position = 0 ;
char *current_line = new char[200] ; // we are assuming that the current line is not
// greater than the 200 characters... however this is NUTS
for ( ; current_position < length ; current_position++)
{
// if we current char '\n' then
if ( str[current_position] == '\n' )
{
current_line [current_line_position] = 0;
line * new_line = new line ( current_line , current_line_position + 1 );
cout << "current line is " << current_line << "current length is : " << strlen ( current_line) << endl;
lines_vector->push_back ( *new_line );
cout << "debug -- line 2" << endl ;
current_line_position = 0 ;
}else{
current_line [ current_line_position ] = str [current_position];
current_line_position++ ;
}
}
}
};
/* This is the Model of our window */
struct Model
{
int first_print_line ;
TEXTMETRIC tm ;
int cyCaps ;
// this will holds the number of lines on the screen
int num_lines ;
int num_lines_in_file ;
// the current y size of the screen
int window_height ;
int window_width ;
// this is the lines
lines * lines_object ;
char * getline ( int line )
{
return lines_object->getline(line);
}
Model (HWND hwnd )
{
RECT rect ;
// Get Device Context
HDC hdc = GetDC ( hwnd ) ;
//GetTextMetrics ( hdc , &tm );
GetClientRect ( hwnd , &rect ) ;
window_height = rect.bottom - rect.top ;
window_width = rect.right - rect.left ;
// then calculate the cyCaps from it;
cyCaps = tm.tmHeight + tm.tmExternalLeading ;
// then we can know how many lines that we can actually
// print
num_lines = window_height / cyCaps ;
ReleaseDC ( hwnd , hdc ) ;
/* This function just open the file and fill the lines
Structure
*/
first_print_line = 0 ;
char* str = new char [2001];
// just open the file and read it to the str array
ifstream infile ;
infile.open( "reading.txt" ) ;
/*
MessageBox(NULL , "File open" , infile.is_open() ? "File has been Opened" : "file is not open " , MB_OK );
//infile>>.get ( str , 2000) ;
infile.read ( str , 2000);
// debug ---
char * str11 = new char [200];
for ( int i = 0 ; i < 100 ; i++ )
str11[i] = str[i];
str11[99] ='\n';
cout << str11 << endl;
// debug --
*/
lines_object = new lines ( str , 2001 ) ;
num_lines_in_file = lines_object->get_num_lines();
}
// THE WM_SIZE mesage should call this function.
void WM_PAINT_callback ( HWND hwnd )
{
RECT rect ;
GetClientRect ( hwnd , &rect ) ;
window_height = rect.bottom - rect.top ;
window_width = rect.right - rect.left ;
// then we can know how many lines that we can actually
// print
num_lines = window_height / cyCaps ;
}
} * window_model ;
int main()
{
// create a model
char * myString = "This is the case\nThis is you\nand you gonna to be a \ncool\nthis is it\nit is that\n";
lines* new_lines = new lines ( myString , strlen ( myString));
cout <<( new_lines->getline ( 3));
system("PAUSE");
return 0;
}
|