Simple Operating System
e.c
Go to the documentation of this file.
1 
6 #include <io.h>
7 #include <types.h>
8 #include <file.h>
9 #include <conio.h>
10 #include <string.h>
11 
12 __start int main() {
13  char data[2001];
14  char view[80][25];
15  memset(view, 0, 80 * 25);
16  memset(data, 0, 2000);
17  FILE* file = open(0x80, "");
18  if(file == NULL)
19  return -1;
20 
21  size_t size = read(data, 2000, file);
22 
23  cls();
24  size_t n = 0;
25  for(int j = 0; j < 24; j++) {
26  for(int i = 0; i < 79; i++) {
27  if(n > size)
28  break;
29  if(data[n] == '\n') {
30  j++;
31  i = -1;
32  n++;
33  continue;
34  }
35  view[i][j] = data[n];
36  n++;
37  }
38  }
39  Key key;
40  Cursor cursor;
41  Cursor def = getCursorPosition();
42  int sizeY = 0;
43  Attributes underCursor;
44  underCursor.foreground = Black;
45  underCursor.background = LightGrey;
46  do {
47  setCursorPosition(def);
48  bool wasNewline;
49  sizeY = 0;
50  for(int j = 0; j < 24; j++) {
51  wasNewline = false;
52  for(int i = 0; i < 79; i++) {
53  if(view[i][j] != 0) {
54  if(i == cursor.x && j == cursor.y)
55  cputc(view[i][j], underCursor.attributes, 1);
56  else
57  cputc(view[i][j], LightGrey, 1);
58  wasNewline = true;
59  }
60  else if(i == cursor.x && j == cursor.y) {
61  if(i != 0)
62  while(cursor.x > 0 && view[cursor.x - 1][cursor.y] == 0)cursor.x--;
63  cputc(view[i][j], underCursor.attributes, 1);
64  }
65  }
66  if(!wasNewline)
67  putc('~');
68  else
69  sizeY++;
70  putc('\n');
71  }
72  printf("File: %s size: %i", 0x80, size);
73  setCursorPosition(cursor);
74  key = getc();
75  if(key.character == 0 && key.scancode == 72) {//UP
76  cursor = getCursorPosition();
77  if(cursor.y > 0)
78  cursor.y--;
79  setCursorPosition(cursor);
80  }
81  else if(key.character == 0 && key.scancode == 80 ) {//DOWN
82  cursor = getCursorPosition();
83  if(cursor.y < sizeY)
84  cursor.y++;
85  if(cursor.y == sizeY)
86  cursor.x = 0;
87  setCursorPosition(cursor);
88  }
89  else if(key.character == 0 && key.scancode == 77) {//RIGHT
90  cursor = getCursorPosition();
91  cursor.x++;
92  setCursorPosition(cursor);
93  }
94  else if(key.character == 0 && key.scancode == 75) {//LEFT
95  cursor = getCursorPosition();
96  cursor.x--;
97  setCursorPosition(cursor);
98  }
99  else if(key.character == 8 && key.scancode == 14 && cursor.x > 0) { //backspace
100  cursor = getCursorPosition();
101  view[cursor.x ][cursor.y] = 0;
102  cursor.x--;
103  setCursorPosition(cursor);
104  }
105  else if(key.character == 13 && key.scancode == 28) {//enter
106  cursor = getCursorPosition();
107  if(cursor.y < sizeY)
108  cursor.y++;
109  cursor.x = 0;
110  setCursorPosition(cursor);
111  }
112  else if(key.character == 19 && key.scancode == 31) {//save
113  n = 0;
114  bool isEmpty = true;
115  cls();
116  for(int j = 0; j < 24; j++) {
117  isEmpty = true;
118  for(int i = 0; i < 79; i++) {
119  if(view[i][j] == 0)
120  continue;
121  isEmpty = false;
122  data[n] = view[i][j];
123  n++;
124  }
125  if(!isEmpty) {
126  data[n] = '\n';
127  n++;
128  }
129  }
130  if(data[n - 1] == '\n')
131  n--;
132 
133  printf("Saved %iB = %i sectors and %i Bytes\n", n, n / 512, n % 512);
134  int ret = write(data, n, file);
135  return (ret > 0 ? 0 : ret);
136  }
137  else {
138  view[cursor.x][cursor.y] = key.character;//TODO: edit in data, not in view!!!
139  cursor.x++;
140  }
141  }
142  while(key.character != 27 && key.scancode != 1);//escape
143  cls();
144  return 0;
145 }
Console input output control.
void cputc(char c, Color color, Byte times)
Color put character.
Definition: conio.h:102
void setCursorPosition(Cursor cursor)
Set position of cursor.
Definition: conio.h:71
Cursor getCursorPosition(void)
Get position of Cursor.
Definition: conio.h:58
void cls(void)
Clear screen.
Definition: conio.h:80
File IO.
int write(int buf, size_t size, FILE *stream)
Write to file.
Definition: file.h:66
FILE * open(int filename, int mode)
Open file.
Definition: file.h:33
int read(int buf, size_t size, FILE *stream)
Read from file.
Definition: file.h:53
Standard input output library.
Key getc(void)
Wait for key in buffer, get it and clear buffer.
Definition: io.h:83
void printf(const int str,...)
C-like printf function.
Definition: io.h:72
void putc(Byte character)
Put character.
Definition: io.h:31
__start void main()
Definition: kernel.c:131
Simple string handling.
int memset(int ptr, int value, size_t count)
Set count Bytes of *ptr to value.
Definition: string.h:97
Position of cursor.
Definition: conio.h:20
Information about file.
Definition: file.h:20
useful macros, definitions, enums etc.
#define NULL
pointer to NULL
Definition: types.h:22
#define __start
Must be before main function of program to put this function as first in binary file.
Definition: types.h:35
Attributes of character on screen.
Definition: conio.h:27
Byte foreground
Definition: conio.h:29
Byte background
Definition: conio.h:30
Byte attributes
Definition: conio.h:33
Key information, got from buffer.
Definition: io.h:14