File: WinSystem.cpp

package info (click to toggle)
contextfree 2.2%2Bdfsg1-2.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 4,336 kB
  • sloc: cpp: 20,388; yacc: 507; objc: 494; ansic: 270; makefile: 113; lex: 92; xml: 24
file content (148 lines) | stat: -rw-r--r-- 3,959 bytes parent folder | download | duplicates (2)
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
// WinSystem.cpp
// this file is part of Context Free
// ---------------------
// Copyright (C) 2005-2008 John Horigan - john@glyphic.com
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// 
// John Horigan can be contacted at john@glyphic.com or at
// John Horigan, 1209 Villa St., Mountain View, CA 94041-1123, USA
//
//

#define _CRT_SECURE_NO_WARNINGS 1

#include "WinSystem.h"
#include "cfdg.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <shlwapi.h>
#include <stdio.h>
#include <string.h>
#include <windows.h>

using namespace std;

//map<const string, WinSystem*> WinSystem::PathMap;
map<const string, string> WinSystem::ExampleMap;

WinSystem::WinSystem(void* h)
:   mWindow(h)
{
}

WinSystem::~WinSystem()
{
}

void WinSystem::AddExample(const char* newName, const char* newText)
{
    ExampleMap[newName] = newText;
}

bool WinSystem::updateInfo(const char* newName, const char* newText)
{
    mName = newName;

    if (!strcmp(newText, mText.c_str()))
        return false;

    mText = newText;
    return true;
}

void WinSystem::message(bool, const char* fmt, ...)
{
    va_list args;
    va_start(args, fmt);
    char cbuf[256];
    _vsnprintf(cbuf, 255, fmt, args);
    cbuf[255] = '\0';
    va_end(args);
    
    if (!mErrorPath.empty()) {
        strcat(cbuf, " - ");
        strcat(cbuf, ::PathFindFileNameA(mErrorPath.c_str()));
        mErrorPath.clear();
    }

    char* buf = new char[strlen(cbuf) + 1];
    strcpy(buf, cbuf);

    if (!::PostMessage((HWND)mWindow, WM_USER_MESSAGE_UPDATE,(WPARAM)buf, NULL))
        delete[] buf;
}

void WinSystem::syntaxError(const std::string& filepath, int line)
{
    mErrorPath = filepath;

    if (filepath == mName)
        ::PostMessage((HWND)mWindow, WM_USER_SYNTAX_ERROR, line, NULL);
}

std::istream* WinSystem::openFileForRead(const std::string& path)
{
    map<const string, string>::iterator exText = ExampleMap.find(path);

    if (path == mName) {
        return new stringstream(mText);
    } else if (exText != ExampleMap.end()) {
        return new stringstream(exText->second);
    } else {
        return new ifstream(path.c_str(), ios::binary);
    }
}

std::istream* WinSystem::tempFileForRead(const std::string& path)
{
    return new ifstream(path.c_str(), ios::binary);
}

std::ostream* WinSystem::tempFileForWrite(std::string& prefixInNameOut)
{
    char* path = _tempnam(0, prefixInNameOut.c_str());
    prefixInNameOut = path;
    ofstream* f = new ofstream(path, ios::binary | ios::out | ios::trunc);
    free((void*)path);
    return f;
}

std::string WinSystem::relativeFilePath(const std::string& base, const std::string& rel)
{
    char buf[MAX_PATH+1];
    strcpy(buf, base.c_str());
    PathRemoveFileSpecA(buf);
    PathAppendA(buf, rel.c_str());
	if (PathFileExistsA(buf))
	    return string(buf);
	else
		return rel;
}

void WinSystem::stats(const Stats& s)
{
    Stats* stat = new Stats(s);
    if (!::PostMessage((HWND)mWindow, WM_USER_STATUS_UPDATE,(WPARAM)stat, NULL))
        delete stat;
}

void WinSystem::statusUpdate()
{
    ::PostMessage((HWND)mWindow, WM_USER_STATUS_UPDATE, NULL, NULL);
}