2008-10-13 06:53:25 -04:00
|
|
|
#include <stdio.h>
|
2008-10-12 20:53:28 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
2008-12-26 22:04:58 -05:00
|
|
|
#include <math.h>
|
2008-10-12 20:53:28 -04:00
|
|
|
|
|
|
|
#include "picoc.h"
|
|
|
|
|
|
|
|
|
2009-01-23 22:15:02 -05:00
|
|
|
void StrCopy(Str *Dest, const Str *Source)
|
|
|
|
{
|
|
|
|
Dest->Str = Source->Str;
|
|
|
|
Dest->Len = Source->Len;
|
|
|
|
}
|
|
|
|
|
2008-10-12 20:53:28 -04:00
|
|
|
void StrToC(char *Dest, int DestSize, const Str *Source)
|
|
|
|
{
|
|
|
|
int CopyLen = min(DestSize-1, Source->Len);
|
|
|
|
memcpy(Dest, Source->Str, CopyLen);
|
|
|
|
Dest[CopyLen] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
void StrFromC(Str *Dest, const char *Source)
|
|
|
|
{
|
|
|
|
Dest->Str = Source;
|
|
|
|
Dest->Len = strlen(Source);
|
|
|
|
}
|
|
|
|
|
|
|
|
int StrEqual(const Str *Str1, const Str *Str2)
|
|
|
|
{
|
|
|
|
if (Str1->Len != Str2->Len)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return memcmp(Str1->Str, Str2->Str, Str1->Len) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int StrEqualC(const Str *Str1, const char *Str2)
|
|
|
|
{
|
|
|
|
return strncmp(Str1->Str, Str2, Str1->Len) == 0 && Str2[Str1->Len] == '\0';
|
|
|
|
}
|
|
|
|
|
2008-10-14 07:18:43 -04:00
|
|
|
void StrPrintInt(int Num, FILE *Stream)
|
|
|
|
{
|
|
|
|
int Div;
|
2008-12-19 02:26:41 -05:00
|
|
|
int Remainder = 0;
|
|
|
|
int Printing = FALSE;
|
2008-10-14 07:18:43 -04:00
|
|
|
|
|
|
|
if (Num < 0)
|
|
|
|
{
|
|
|
|
fputc('-', Stream);
|
|
|
|
Num = -Num;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Num == 0)
|
|
|
|
fputc('0', Stream);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Div = LARGE_INT_POWER_OF_TEN;
|
|
|
|
while (Div > 0)
|
|
|
|
{
|
|
|
|
Remainder = Num / Div;
|
2008-12-19 02:26:41 -05:00
|
|
|
if (Printing || Remainder > 0)
|
|
|
|
{
|
|
|
|
fputc('0' + Remainder, Stream);
|
|
|
|
Printing = TRUE;
|
|
|
|
}
|
2008-10-14 07:18:43 -04:00
|
|
|
Num -= Remainder * Div;
|
|
|
|
Div /= 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-10-12 20:53:28 -04:00
|
|
|
|
2008-12-26 22:04:58 -05:00
|
|
|
void StrPrintFP(double Num, FILE *Stream)
|
|
|
|
{
|
|
|
|
int Exponent = 0;
|
|
|
|
|
|
|
|
if (abs(Num) >= 1e7)
|
|
|
|
Exponent = log(Num) / LOG10E;
|
|
|
|
else if (abs(Num) <= 1e-7)
|
|
|
|
Exponent = log(Num) / LOG10E - 0.999999999;
|
|
|
|
|
|
|
|
Num /= pow(10.0, Exponent);
|
|
|
|
StrPrintInt((int)Num, Stream);
|
|
|
|
fputc('.', Stream);
|
|
|
|
for (Num -= (int)Num; Num != 0.0; Num *= 10.0)
|
|
|
|
fputc('0' + (int)Num, Stream);
|
|
|
|
|
|
|
|
if (Exponent)
|
|
|
|
{
|
|
|
|
fputc('e', Stream);
|
|
|
|
StrPrintInt(Exponent, Stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-13 06:53:25 -04:00
|
|
|
void StrPrintf(const char *Format, ...)
|
2008-10-12 20:53:28 -04:00
|
|
|
{
|
|
|
|
va_list Args;
|
|
|
|
|
|
|
|
va_start(Args, Format);
|
2008-10-14 07:18:43 -04:00
|
|
|
vStrPrintf(Format, Args);
|
|
|
|
va_end(Args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void vStrPrintf(const char *Format, va_list Args)
|
|
|
|
{
|
|
|
|
Str *str;
|
|
|
|
const char *FPos;
|
|
|
|
|
2008-10-13 06:53:25 -04:00
|
|
|
for (FPos = Format; *FPos != '\0'; FPos++)
|
2008-10-12 20:53:28 -04:00
|
|
|
{
|
|
|
|
if (*FPos == '%')
|
|
|
|
{
|
2008-10-13 06:53:25 -04:00
|
|
|
FPos++;
|
|
|
|
switch (*FPos)
|
|
|
|
{
|
2008-10-14 07:18:43 -04:00
|
|
|
case 'S': str = va_arg(Args, Str *); fwrite(str->Str, 1, str->Len, stdout); break;
|
2008-10-13 06:53:25 -04:00
|
|
|
case 's': fputs(va_arg(Args, char *), stdout); break;
|
2008-10-14 07:18:43 -04:00
|
|
|
case 'd': StrPrintInt(va_arg(Args, int), stdout); break;
|
2008-10-13 06:53:25 -04:00
|
|
|
case 'c': fputc(va_arg(Args, int), stdout); break;
|
2008-12-26 22:04:58 -05:00
|
|
|
case 'f': StrPrintFP(va_arg(Args, double), stdout); break;
|
2008-10-13 06:53:25 -04:00
|
|
|
case '%': fputc('%', stdout); break;
|
|
|
|
case '\0': FPos--; break;
|
|
|
|
}
|
2008-10-12 20:53:28 -04:00
|
|
|
}
|
2008-10-13 06:53:25 -04:00
|
|
|
else
|
|
|
|
putchar(*FPos);
|
2008-10-12 20:53:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|