rendered paste body/* Aloe
* Copyright (c) 2005 Michael Cohen, Robert C., and Zachary Howe
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU GPL license. See LICENSE for more details
*
* THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <fstream>
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
#include "aloe.h"
#include "file.h"
AloeObj* File::reference()
{
refcount++;
return this;
}
void File::dereference()
{
refcount--;
if (!refcount) delete this;
}
Context* File::protcon()
{
return &context;
}
std::string File::className()
{
return "File";
}
double File::asNum()
{
return 1;
}
std::string File::asString()
{
std::string line;
std::string full;
std::vector<std::string> d;
while(getline(file, line))
d.push_back(line);
for (int i = 0; i<d.size(); i++)
full= full + std::string(d[i]);
return full;
}
bool File::asBool()
{
return file.is_open();
}
bool File::equals(AloeObj* oobj)
{
return oobj == this;
}
AloeObj* File::getvar(std::string varname)
{
return AloeNil::create();
}
void File::setvar(std::string varname, AloeObj* obj)
{}
AloeObj* File::callfunc(std::string funcname, std::vector<AloeObj*> args)
{
if (funcname == "read")
{
std::string line;
std::string full;
std::vector<std::string> d;
while(getline(file, line))
d.push_back(line);
for (int i = 0; i<d.size(); i++)
full = full + d[i] + "\n";
file.seekp(std::ios_base::beg); //reset cursor position
return AloeString::create(full);
}
if (funcname == "getline")
{
std::string line;
getline(file, line);
file.seekp(std::ios_base::beg);//reset cursor position
return AloeString::create(line);
}
if (funcname == "size")
{
double l, m;
l = file.tellg();
file.seekg (0, std::ios_base::end);
m = file.tellg();
file.seekp(std::ios_base::beg); // reset cursor position
return AloeNum::create(m-l);
}
if (funcname == "close")
{
file.close();
return AloeNum::create(1);
}
if (funcname == "write")
{
if(file.good()){
file << args[0]->asString();
file << "Hi\n";
return AloeString::create(args[0]->asString());
}
}
if (funcname == "input")
{
std::string str;
std::getline(std::cin, str);
file << str;
return AloeString::create(str);
}
}
File* File::create(std::string name, std::string mode)
{
return new File(name, mode);
}
File::File(std::string name, std::string mode) : refcount(1), name(name)
{
file.open(name.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::ate);
if(!file){
std::cout<<"Error: file was not opened, mode was " << mode;
}
}
File::~File()
{}
AloeObj* File::FileContext::getvar(std::string varname)
{
return AloeNil::create();
}
void File::FileContext::setvar(std::string varname, AloeObj* obj)
{}
AloeObj* File::FileContext::callfunc(std::string funcname, std::vector<AloeObj*> args)
{}