Trying to somewhat move to 80char line limit and tabs as spaces
This commit is contained in:
parent
5e7356f58e
commit
d474de8228
8 changed files with 303 additions and 252 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,5 +1,6 @@
|
|||
*.elf
|
||||
*.o
|
||||
*.img
|
||||
*.bin
|
||||
mnt/
|
||||
build/
|
||||
|
|
|
@ -6,26 +6,43 @@ static unsigned short fb_width, fb_height;
|
|||
static unsigned char fb_bpp, fb_bytespp;
|
||||
static unsigned char fb_rpos, fb_bpos, fb_gpos;
|
||||
|
||||
void initfb(unsigned long addr, unsigned short w, unsigned short h, unsigned char bpp, unsigned long pitch, unsigned char rpos, unsigned char gpos, unsigned char bpos) {
|
||||
fb_address = addr;
|
||||
fb_pitch = pitch;
|
||||
fb_width = w;
|
||||
fb_height = h;
|
||||
fb_bpp = bpp;
|
||||
fb_bytespp = bpp/8;
|
||||
fb_rpos = rpos;
|
||||
fb_gpos = gpos;
|
||||
fb_bpos = bpos;
|
||||
void initfb(
|
||||
unsigned long addr,
|
||||
unsigned short w,
|
||||
unsigned short h,
|
||||
unsigned char bpp,
|
||||
unsigned long pitch,
|
||||
unsigned char rpos,
|
||||
unsigned char gpos,
|
||||
unsigned char bpos
|
||||
){
|
||||
fb_address = addr;
|
||||
fb_pitch = pitch;
|
||||
fb_width = w;
|
||||
fb_height = h;
|
||||
fb_bpp = bpp;
|
||||
fb_bytespp = bpp/8;
|
||||
fb_rpos = rpos;
|
||||
fb_gpos = gpos;
|
||||
fb_bpos = bpos;
|
||||
}
|
||||
|
||||
void putpixel(unsigned short x, unsigned short y, unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
|
||||
if (x>fb_width || y> fb_height) return;
|
||||
|
||||
if (fb_bpp == 32) {
|
||||
*((unsigned long *)(fb_address + y*fb_pitch + x*fb_bytespp)) = r<<fb_rpos | g<<fb_gpos | b<<fb_bpos;
|
||||
} else {
|
||||
*((unsigned char *)(fb_address + y*fb_pitch + x*fb_bytespp + fb_rpos/8)) = r;
|
||||
*((unsigned char *)(fb_address + y*fb_pitch + x*fb_bytespp + fb_gpos/8)) = g;
|
||||
*((unsigned char *)(fb_address + y*fb_pitch + x*fb_bytespp + fb_bpos/8)) = b;
|
||||
}
|
||||
void putpixel(
|
||||
unsigned short x,
|
||||
unsigned short y,
|
||||
unsigned char r,
|
||||
unsigned char g,
|
||||
unsigned char b,
|
||||
unsigned char a
|
||||
){
|
||||
if (x > fb_width || y > fb_height)
|
||||
return;
|
||||
unsigned long dest_addr = fb_address + y*fb_pitch + x*fb_bytespp;
|
||||
if (fb_bpp == 32) {
|
||||
*((unsigned long *)(dest_addr)) = r<<fb_rpos | g<<fb_gpos | b<<fb_bpos;
|
||||
} else {
|
||||
*((unsigned char *)(dest_addr + fb_rpos/8)) = r;
|
||||
*((unsigned char *)(dest_addr + fb_gpos/8)) = g;
|
||||
*((unsigned char *)(dest_addr + fb_bpos/8)) = b;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,23 @@
|
|||
#ifndef _FRAMEBUFFER_H
|
||||
#define _FRAMEBUFFER_H 1
|
||||
|
||||
void initfb(unsigned long addr, unsigned short w, unsigned short h, unsigned char bpp, unsigned long pitch, unsigned char rpos, unsigned char gpos, unsigned char bpos);
|
||||
void putpixel(unsigned short x, unsigned short y, unsigned char r, unsigned char g, unsigned char b, unsigned char a);
|
||||
void initfb(
|
||||
unsigned long addr,
|
||||
unsigned short w,
|
||||
unsigned short h,
|
||||
unsigned char bpp,
|
||||
unsigned long pitch,
|
||||
unsigned char rpos,
|
||||
unsigned char gpos,
|
||||
unsigned char bpos
|
||||
);
|
||||
void putpixel(
|
||||
unsigned short x,
|
||||
unsigned short y,
|
||||
unsigned char r,
|
||||
unsigned char g,
|
||||
unsigned char b,
|
||||
unsigned char a
|
||||
);
|
||||
|
||||
#endif
|
||||
|
|
159
src/kernel.c
159
src/kernel.c
|
@ -4,93 +4,100 @@
|
|||
#include "serial.h"
|
||||
#include "xtoa.h"
|
||||
|
||||
static inline void outb(unsigned short port, unsigned char val) {
|
||||
asm volatile ("outb %0, %1" : : "a"(val), "Nd"(port) : "memory");
|
||||
static inline void outb(unsigned short port, unsigned char val)
|
||||
{
|
||||
asm volatile("outb %0, %1" : : "a"(val), "Nd"(port) : "memory");
|
||||
}
|
||||
static inline unsigned char inb(unsigned short port) {
|
||||
unsigned char ret;
|
||||
asm volatile("inb %1, %0" : "=a"(ret) : "Nd"(port) : "memory");
|
||||
return ret;
|
||||
static inline unsigned char inb(unsigned short port)
|
||||
{
|
||||
unsigned char ret;
|
||||
asm volatile("inb %1, %0" : "=a"(ret) : "Nd"(port) : "memory");
|
||||
return ret;
|
||||
}
|
||||
|
||||
void kmain (unsigned int mbootmagick, multiboot_info_t* mbootinfo) {
|
||||
void kmain (unsigned int mbootmagick, multiboot_info_t* mbootinfo)
|
||||
{
|
||||
// Cursor disabling
|
||||
outb(0x3D4, 0x0A);
|
||||
outb(0x3D5, 0x20);
|
||||
|
||||
// Cursor disabling
|
||||
outb(0x3D4, 0x0A);
|
||||
outb(0x3D5, 0x20);
|
||||
int serial_initialized = serial_init();
|
||||
serial_write_string("\n=== rOSka ===\n");
|
||||
|
||||
int serial_initialized = serial_init();
|
||||
serial_write_string("\n=== rOSka ===\n");
|
||||
// Check multiboot header
|
||||
if (mbootmagick != MULTIBOOT_BOOTLOADER_MAGIC) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check multiboot header
|
||||
if (mbootmagick != MULTIBOOT_BOOTLOADER_MAGIC) {
|
||||
return;
|
||||
}
|
||||
serial_write_string("\nMultiboot flags: ");
|
||||
serial_write_string(itoa(mbootinfo->flags, 2));
|
||||
|
||||
serial_write_string("\nMultiboot flags: ");
|
||||
serial_write_string(itoa(mbootinfo->flags, 2));
|
||||
// Check videomode
|
||||
if (mbootinfo->flags & MULTIBOOT_INFO_VBE_INFO) {
|
||||
serial_write_string("\nVBE Mode: 0x");
|
||||
serial_write_string(itoa(mbootinfo->vbe_mode, 16));
|
||||
}
|
||||
|
||||
// Check videomode
|
||||
if (mbootinfo->flags & MULTIBOOT_INFO_VBE_INFO) {
|
||||
serial_write_string("\nVBE Mode: 0x");
|
||||
serial_write_string(itoa(mbootinfo->vbe_mode, 16));
|
||||
}
|
||||
if (!(mbootinfo->flags & MULTIBOOT_INFO_FRAMEBUFFER_INFO)) {
|
||||
serial_write_string("\nVideo info not available");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(mbootinfo->flags & MULTIBOOT_INFO_FRAMEBUFFER_INFO)) {
|
||||
serial_write_string("\nVideo info not available");
|
||||
return;
|
||||
}
|
||||
serial_write_string("\nFramebuffer: ");
|
||||
serial_write_string("\n Address: 0x");
|
||||
serial_write_string(ultoa(mbootinfo->framebuffer_addr, 16));
|
||||
|
||||
serial_write_string("\nFramebuffer: ");
|
||||
serial_write_string("\n Address: 0x");
|
||||
serial_write_string(ultoa(mbootinfo->framebuffer_addr, 16));
|
||||
serial_write_string("\n Dimensions: ");
|
||||
serial_write_string(itoa(mbootinfo->framebuffer_width, 10));
|
||||
serial_write_string("X");
|
||||
serial_write_string(itoa(mbootinfo->framebuffer_height, 10));
|
||||
serial_write_string("x");
|
||||
serial_write_string(itoa(mbootinfo->framebuffer_bpp, 10));
|
||||
serial_write_string("\n Pitch: ");
|
||||
serial_write_string(itoa(mbootinfo->framebuffer_pitch, 10));
|
||||
serial_write_string("\n RPos:");
|
||||
serial_write_string(itoa(mbootinfo->framebuffer_red_field_position, 10));
|
||||
serial_write_string("\n GPos:");
|
||||
serial_write_string(itoa(mbootinfo->framebuffer_green_field_position, 10));
|
||||
serial_write_string("\n BPos:");
|
||||
serial_write_string(itoa(mbootinfo->framebuffer_blue_field_position, 10));
|
||||
|
||||
serial_write_string("\n Dimensions: ");
|
||||
serial_write_string(itoa(mbootinfo->framebuffer_width, 10));
|
||||
serial_write_string("X");
|
||||
serial_write_string(itoa(mbootinfo->framebuffer_height, 10));
|
||||
serial_write_string("x");
|
||||
serial_write_string(itoa(mbootinfo->framebuffer_bpp, 10));
|
||||
serial_write_string("\n Pitch: ");
|
||||
serial_write_string(itoa(mbootinfo->framebuffer_pitch, 10));
|
||||
serial_write_string("\n RPos:");
|
||||
serial_write_string(itoa(mbootinfo->framebuffer_red_field_position, 10));
|
||||
serial_write_string("\n GPos:");
|
||||
serial_write_string(itoa(mbootinfo->framebuffer_green_field_position, 10));
|
||||
serial_write_string("\n BPos:");
|
||||
serial_write_string(itoa(mbootinfo->framebuffer_blue_field_position, 10));
|
||||
serial_write_string("\nMemory: ");
|
||||
serial_write_string("\n lower: ");
|
||||
serial_write_string(uitoa(mbootinfo->mem_lower, 10));
|
||||
serial_write_string(" k");
|
||||
serial_write_string("\n upper: ");
|
||||
serial_write_string(uitoa(mbootinfo->mem_upper/1024, 10));
|
||||
serial_write_string(" M");
|
||||
|
||||
serial_write_string("\nMemory: ");
|
||||
serial_write_string("\n lower: ");
|
||||
serial_write_string(uitoa(mbootinfo->mem_lower, 10));
|
||||
serial_write_string(" k");
|
||||
serial_write_string("\n upper: ");
|
||||
serial_write_string(uitoa(mbootinfo->mem_upper/1024, 10));
|
||||
serial_write_string(" M");
|
||||
initfb(
|
||||
mbootinfo->framebuffer_addr,
|
||||
mbootinfo->framebuffer_width,
|
||||
mbootinfo->framebuffer_height,
|
||||
mbootinfo->framebuffer_bpp,
|
||||
mbootinfo->framebuffer_pitch,
|
||||
mbootinfo->framebuffer_red_field_position,
|
||||
mbootinfo->framebuffer_green_field_position,
|
||||
mbootinfo->framebuffer_blue_field_position
|
||||
);
|
||||
int x, y, i;
|
||||
unsigned char c = 0;
|
||||
for (;;) {
|
||||
for (;c<255;c+=5) {
|
||||
for(y=0; y < mbootinfo->framebuffer_height; y++) {
|
||||
for(x=0; x < mbootinfo->framebuffer_width; x++) {
|
||||
putpixel(x, y, 0, c, c, 0xff);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (;c>0;c-=5) {
|
||||
for(y=mbootinfo->framebuffer_height; y>=0; y--) {
|
||||
for(x=mbootinfo->framebuffer_width; x>=0; x--) {
|
||||
putpixel(x, y, 0, c, c, 0xff);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
initfb(mbootinfo->framebuffer_addr, mbootinfo->framebuffer_width, mbootinfo->framebuffer_height, mbootinfo->framebuffer_bpp, mbootinfo->framebuffer_pitch, mbootinfo->framebuffer_red_field_position, mbootinfo->framebuffer_green_field_position, mbootinfo->framebuffer_blue_field_position);
|
||||
int x, y, i;
|
||||
unsigned char c = 0;
|
||||
for(;;) {
|
||||
//serial_write_string("\n");
|
||||
for (;c<255;c+=5) {
|
||||
//serial_write_string("/");
|
||||
for(y=0; y < mbootinfo->framebuffer_height; y++) {
|
||||
for(x=0; x < mbootinfo->framebuffer_width; x++) {
|
||||
putpixel(x, y, 0, c, c, 0xff);
|
||||
}
|
||||
}
|
||||
}
|
||||
//serial_write_string("\n");
|
||||
for (;c>0;c-=5) {
|
||||
//serial_write_string("\\");
|
||||
for(y=mbootinfo->framebuffer_height; y>=0; y--) {
|
||||
for(x=mbootinfo->framebuffer_width; x>=0; x--) {
|
||||
putpixel(x, y, 0, c, c, 0xff);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
serial_write_string("\nExecution finished, halting...\n");
|
||||
serial_write_string("\nExecution finished, halting...\n");
|
||||
}
|
||||
|
|
80
src/serial.c
80
src/serial.c
|
@ -1,50 +1,56 @@
|
|||
#include "serial.h"
|
||||
#define PORT 0x3f8 // COM1
|
||||
|
||||
static inline void outb(unsigned short port, unsigned char val) {
|
||||
asm volatile ("outb %0, %1" : : "a"(val), "Nd"(port) : "memory");
|
||||
static inline void outb(unsigned short port, unsigned char val)
|
||||
{
|
||||
asm volatile("outb %0, %1" : : "a"(val), "Nd"(port) : "memory");
|
||||
}
|
||||
static inline unsigned char inb(unsigned short port) {
|
||||
unsigned char ret;
|
||||
asm volatile("inb %1, %0" : "=a"(ret) : "Nd"(port) : "memory");
|
||||
return ret;
|
||||
static inline unsigned char inb(unsigned short port)
|
||||
{
|
||||
unsigned char ret;
|
||||
asm volatile("inb %1, %0" : "=a"(ret) : "Nd"(port) : "memory");
|
||||
return ret;
|
||||
}
|
||||
|
||||
int serial_init() {
|
||||
outb(PORT + 1, 0x00); // Disable all interrupts
|
||||
outb(PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
|
||||
outb(PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
|
||||
outb(PORT + 1, 0x00); // (hi byte)
|
||||
outb(PORT + 3, 0x03); // 8 bits, no parity, one stop bit
|
||||
outb(PORT + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
|
||||
outb(PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
|
||||
outb(PORT + 4, 0x1E); // Set in loopback mode, test the serial chip
|
||||
outb(PORT + 0, 0xAE); // Test serial chip (send byte 0xAE and check if serial
|
||||
// returns same byte)
|
||||
int serial_init()
|
||||
{
|
||||
outb(PORT + 1, 0x00); // Disable all interrupts
|
||||
outb(PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
|
||||
outb(PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
|
||||
outb(PORT + 1, 0x00); // (hi byte)
|
||||
outb(PORT + 3, 0x03); // 8 bits, no parity, one stop bit
|
||||
outb(PORT + 2, 0xC7); // Enable FIFO, clear them, 14-byte threshold
|
||||
outb(PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
|
||||
outb(PORT + 4, 0x1E); // Set in loopback mode, test the serial chip
|
||||
outb(PORT + 0, 0xAE); // Test serial chip (send byte 0xAE and check
|
||||
// if serial returns same byte)
|
||||
|
||||
// Check if serial is faulty (i.e: not same byte as sent)
|
||||
if (inb(PORT + 0) != 0xAE) {
|
||||
return 1;
|
||||
}
|
||||
// Check if serial is faulty (i.e: not same byte as sent)
|
||||
if (inb(PORT + 0) != 0xAE)
|
||||
return 1;
|
||||
|
||||
// If serial is not faulty set it in normal operation mode
|
||||
// (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled)
|
||||
outb(PORT + 4, 0x0F);
|
||||
return 0;
|
||||
// If serial is not faulty set it in normal operation mode
|
||||
// (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled)
|
||||
outb(PORT + 4, 0x0F);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int serial_is_transmit_empty() { return inb(PORT + 5) & 0x20; }
|
||||
|
||||
static void serial_write_char(char chr) {
|
||||
while (serial_is_transmit_empty() == 0);
|
||||
outb(PORT, chr);
|
||||
static int serial_is_transmit_empty()
|
||||
{
|
||||
return inb(PORT + 5) & 0x20;
|
||||
}
|
||||
|
||||
void serial_write_string(const char* text) {
|
||||
int i = 0;
|
||||
while(text[i]) {
|
||||
serial_write_char(text[i]);
|
||||
i++;
|
||||
}
|
||||
|
||||
static void serial_write_char(char chr)
|
||||
{
|
||||
while (serial_is_transmit_empty() == 0);
|
||||
outb(PORT, chr);
|
||||
}
|
||||
|
||||
void serial_write_string(const char* text)
|
||||
{
|
||||
int i = 0;
|
||||
while (text[i]) {
|
||||
serial_write_char(text[i]);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,48 +1,48 @@
|
|||
.set MULTIBOOT_PAGE_ALIGN, 1 << 0
|
||||
.set MULTIBOOT_MEMORY_INFO, 1 << 1
|
||||
.set MULTIBOOT_VIDEO_REQUEST, 1 << 2
|
||||
.set MULTIBOOT_AOUT_KLUDGE, 0 << 16
|
||||
.set MULTIBOOT_HEADER_MAGIC, 0x1BADB002
|
||||
.set MULTIBOOT_PAGE_ALIGN, 1 << 0
|
||||
.set MULTIBOOT_MEMORY_INFO, 1 << 1
|
||||
.set MULTIBOOT_VIDEO_REQUEST, 1 << 2
|
||||
.set MULTIBOOT_AOUT_KLUDGE, 0 << 16
|
||||
.set MULTIBOOT_HEADER_MAGIC, 0x1BADB002
|
||||
.set MULTIBOOT_HEADER_FLAGS, \
|
||||
MULTIBOOT_PAGE_ALIGN |\
|
||||
MULTIBOOT_MEMORY_INFO |\
|
||||
MULTIBOOT_VIDEO_REQUEST |\
|
||||
MULTIBOOT_AOUT_KLUDGE
|
||||
.set MULTIBOOT_CHECKSUM, -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
|
||||
MULTIBOOT_PAGE_ALIGN \
|
||||
| MULTIBOOT_MEMORY_INFO \
|
||||
| MULTIBOOT_VIDEO_REQUEST \
|
||||
| MULTIBOOT_AOUT_KLUDGE
|
||||
.set MULTIBOOT_CHECKSUM, -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
|
||||
|
||||
#.section ".multiboottbabeee"
|
||||
.align 4
|
||||
.long MULTIBOOT_HEADER_MAGIC
|
||||
.long MULTIBOOT_HEADER_FLAGS
|
||||
.long MULTIBOOT_CHECKSUM
|
||||
.long 0 # header address
|
||||
.long 0 # load address
|
||||
.long 0 # load end address
|
||||
.long 0 # bss end address
|
||||
.long 0 # entry address
|
||||
.long 0 # video mode_type (0:fb, 1:txt) (set flags[2]!)
|
||||
.long 640 # video width
|
||||
.long 480 # video height
|
||||
.long 32 # video depth
|
||||
.long MULTIBOOT_HEADER_MAGIC
|
||||
.long MULTIBOOT_HEADER_FLAGS
|
||||
.long MULTIBOOT_CHECKSUM
|
||||
.long 0 # header address
|
||||
.long 0 # load address
|
||||
.long 0 # load end address
|
||||
.long 0 # bss end address
|
||||
.long 0 # entry address
|
||||
.long 0 # video mode_type (0:fb, 1:txt) (set flags[2]!)
|
||||
.long 1024 # video width
|
||||
.long 768 # video height
|
||||
.long 32 # video depth
|
||||
|
||||
.bss
|
||||
.align 16
|
||||
stack_bottom:
|
||||
.skip 16384 # 16k
|
||||
stack_top:
|
||||
stack_bottom:
|
||||
.skip 16384 # 16k
|
||||
stack_top:
|
||||
|
||||
.text
|
||||
.global _start
|
||||
_start:
|
||||
# Setup stack
|
||||
mov $stack_top, %esp
|
||||
# Setup stack
|
||||
mov $stack_top, %esp
|
||||
|
||||
# Call the main kernel function.
|
||||
push %ebx
|
||||
push %eax
|
||||
call kmain
|
||||
# Call the main kernel function.
|
||||
push %ebx
|
||||
push %eax
|
||||
call kmain
|
||||
|
||||
|
||||
cli
|
||||
1: hlt
|
||||
jmp 1b
|
||||
cli
|
||||
1: hlt
|
||||
jmp 1b
|
||||
|
|
162
src/xtoa.c
162
src/xtoa.c
|
@ -1,100 +1,104 @@
|
|||
#include "xtoa.h"
|
||||
char* itoa(int value, int base) {
|
||||
char* result;
|
||||
char* itoa(int value, int base)
|
||||
{
|
||||
char* result;
|
||||
|
||||
// check that the base if valid
|
||||
if (base < 2 || base > 36) { *result = '\0'; return result; }
|
||||
// check that the base if valid
|
||||
if (base < 2 || base > 36) { *result = '\0'; return result; }
|
||||
|
||||
char* ptr = result, *ptr1 = result, tmp_char;
|
||||
int tmp_value;
|
||||
char* ptr = result, *ptr1 = result, tmp_char;
|
||||
int tmp_value;
|
||||
|
||||
do {
|
||||
tmp_value = value;
|
||||
value /= base;
|
||||
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
|
||||
} while ( value );
|
||||
|
||||
// Apply negative sign
|
||||
if (tmp_value < 0) *ptr++ = '-';
|
||||
*ptr-- = '\0';
|
||||
while(ptr1 < ptr) {
|
||||
tmp_char = *ptr;
|
||||
*ptr--= *ptr1;
|
||||
*ptr1++ = tmp_char;
|
||||
}
|
||||
return result;
|
||||
do {
|
||||
tmp_value = value;
|
||||
value /= base;
|
||||
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
|
||||
} while ( value );
|
||||
|
||||
// Apply negative sign
|
||||
if (tmp_value < 0) *ptr++ = '-';
|
||||
*ptr-- = '\0';
|
||||
while (ptr1 < ptr) {
|
||||
tmp_char = *ptr;
|
||||
*ptr--= *ptr1;
|
||||
*ptr1++ = tmp_char;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
char* uitoa(unsigned int value, int base) {
|
||||
char* result;
|
||||
|
||||
// check that the base if valid
|
||||
if (base < 2 || base > 36) { *result = '\0'; return result; }
|
||||
char* uitoa(unsigned int value, int base)
|
||||
{
|
||||
char* result;
|
||||
|
||||
char* ptr = result, *ptr1 = result, tmp_char;
|
||||
int tmp_value;
|
||||
// check that the base if valid
|
||||
if (base < 2 || base > 36) { *result = '\0'; return result; }
|
||||
|
||||
do {
|
||||
tmp_value = value;
|
||||
value /= base;
|
||||
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
|
||||
} while ( value );
|
||||
char* ptr = result, *ptr1 = result, tmp_char;
|
||||
int tmp_value;
|
||||
|
||||
do {
|
||||
tmp_value = value;
|
||||
value /= base;
|
||||
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
|
||||
} while ( value );
|
||||
|
||||
*ptr-- = '\0';
|
||||
while(ptr1 < ptr) {
|
||||
tmp_char = *ptr;
|
||||
*ptr--= *ptr1;
|
||||
*ptr1++ = tmp_char;
|
||||
}
|
||||
return result;
|
||||
*ptr-- = '\0';
|
||||
while (ptr1 < ptr) {
|
||||
tmp_char = *ptr;
|
||||
*ptr--= *ptr1;
|
||||
*ptr1++ = tmp_char;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
char* ltoa(long value, int base) {
|
||||
char* result;
|
||||
|
||||
// check that the base if valid
|
||||
if (base < 2 || base > 36) { *result = '\0'; return result; }
|
||||
char* ltoa(long value, int base)
|
||||
{
|
||||
char* result;
|
||||
|
||||
char* ptr = result, *ptr1 = result, tmp_char;
|
||||
int tmp_value;
|
||||
// check that the base if valid
|
||||
if (base < 2 || base > 36) { *result = '\0'; return result; }
|
||||
|
||||
do {
|
||||
tmp_value = value;
|
||||
value /= base;
|
||||
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
|
||||
} while ( value );
|
||||
char* ptr = result, *ptr1 = result, tmp_char;
|
||||
int tmp_value;
|
||||
|
||||
// Apply negative sign
|
||||
if (tmp_value < 0) *ptr++ = '-';
|
||||
*ptr-- = '\0';
|
||||
while(ptr1 < ptr) {
|
||||
tmp_char = *ptr;
|
||||
*ptr--= *ptr1;
|
||||
*ptr1++ = tmp_char;
|
||||
}
|
||||
return result;
|
||||
do {
|
||||
tmp_value = value;
|
||||
value /= base;
|
||||
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
|
||||
} while ( value );
|
||||
|
||||
// Apply negative sign
|
||||
if (tmp_value < 0) *ptr++ = '-';
|
||||
*ptr-- = '\0';
|
||||
while (ptr1 < ptr) {
|
||||
tmp_char = *ptr;
|
||||
*ptr--= *ptr1;
|
||||
*ptr1++ = tmp_char;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
char* ultoa(unsigned long value, int base) {
|
||||
char* result;
|
||||
|
||||
// check that the base if valid
|
||||
if (base < 2 || base > 36) { *result = '\0'; return result; }
|
||||
char* ultoa(unsigned long value, int base)
|
||||
{
|
||||
char* result;
|
||||
|
||||
char* ptr = result, *ptr1 = result, tmp_char;
|
||||
int tmp_value;
|
||||
// check that the base if valid
|
||||
if (base < 2 || base > 36) { *result = '\0'; return result; }
|
||||
|
||||
do {
|
||||
tmp_value = value;
|
||||
value /= base;
|
||||
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
|
||||
} while ( value );
|
||||
char* ptr = result, *ptr1 = result, tmp_char;
|
||||
int tmp_value;
|
||||
|
||||
// Apply negative sign
|
||||
//if (tmp_value < 0) *ptr++ = '-';
|
||||
*ptr-- = '\0';
|
||||
while(ptr1 < ptr) {
|
||||
tmp_char = *ptr;
|
||||
*ptr--= *ptr1;
|
||||
*ptr1++ = tmp_char;
|
||||
}
|
||||
return result;
|
||||
do {
|
||||
tmp_value = value;
|
||||
value /= base;
|
||||
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
|
||||
} while ( value );
|
||||
|
||||
*ptr-- = '\0';
|
||||
while (ptr1 < ptr) {
|
||||
tmp_char = *ptr;
|
||||
*ptr--= *ptr1;
|
||||
*ptr1++ = tmp_char;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
#ifndef HEADER_XTOA
|
||||
#define HEADER_XTOA
|
||||
|
||||
char* itoa(int value, int base);
|
||||
char* uitoa(unsigned int value, int base);
|
||||
char* ltoa(long value, int base);
|
||||
char* ultoa(unsigned long value, int base);
|
||||
char *itoa(int value, int base);
|
||||
char *uitoa(unsigned int value, int base);
|
||||
char *ltoa(long value, int base);
|
||||
char *ultoa(unsigned long value, int base);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue