Welcome to mirror list, hosted at ThFree Co, Russian Federation.

util.cpp « src « irstlm - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 42dd19f1cfee08f4c97b38fc299266848137647b (plain)
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
149
150
151
152
153
154

#ifdef WIN32
#include <windows.h>
#endif

#include <sys/types.h>
#include <sys/mman.h>

#include "util.h"

using namespace std;

string gettempfolder()
{	
#ifdef _WIN32
	char *tmpPath = getenv("TMP");
	string str(tmpPath);
	if (str.substr(str.size() - 1, 1) != "\\")
		str += "\\";
	return str;
#else
	return "/tmp/";
#endif
}


void createtempfile(ofstream  &fileStream, string &filePath, std::ios_base::openmode flags)
{	
#ifdef _WIN32
	char buffer[BUFSIZ];
	::GetTempFileNameA(gettempfolder().c_str(), "", 0, buffer);
	filePath = buffer;
#else
	char buffer[L_tmpnam];
	strcpy(buffer, gettempfolder().c_str());
	strcat(buffer, "dskbuff--XXXXXX");
	mkstemp(buffer);
	filePath = buffer;
#endif
	fileStream.open(filePath.c_str(), flags);
}

void removefile(const std::string &filePath)
{
#ifdef _WIN32
	::DeleteFileA(filePath.c_str());
#else
  char cmd[100];
  sprintf(cmd,"rm %s",filePath.c_str());
  system(cmd);
#endif
}



inputfilestream::inputfilestream(const std::string &filePath)
: std::istream(0),
m_streambuf(0)
{
  if (filePath.size() > 3 &&
      filePath.substr(filePath.size() - 3, 3) == ".gz")
  {
    m_streambuf = new gzfilebuf(filePath.c_str());
  } else {
    std::filebuf* fb = new std::filebuf();
    fb->open(filePath.c_str(), std::ios::in);
    m_streambuf = fb;
  }
  this->init(m_streambuf);
}

inputfilestream::~inputfilestream()
{
  delete m_streambuf; m_streambuf = 0;
}

void inputfilestream::close()
{
}



//MemoryMap Management
//Code kindly provided by Fabio Brugnara, ITC-irst Trento.
/* How to use it: 
call MMap with offset and required size (psgz):
pg->b = MMap(fd, rdwr,offset,pgsz,&g);
correct returned pointer with the alignment gap and save the gap:
pg->b += pg->gap = g;
when releasing mapped memory, subtract the gap from the pointer and add 
the gap to the requested dimension 		
Munmap(pg->b-pg->gap, pgsz+pg->gap, 0);
*/

void *MMap(int	fd, int	access, off_t	offset, size_t	len, off_t	*gap)
{
	void	*p;
	int	pgsz,g=0;
  
#if defined(_WIN32)
	HANDLE	fh,
		mh;
  
	fh = (HANDLE)_get_osfhandle(fd)
    if(offset) {
      /* bisogna accertarsi che l'offset abbia la granularita`
      * corretta, MAI PROVATA! */
      SYSTEM_INFO	si;
      
      GetSystemInfo(&si);
      g = *gap = offset%si.dwPageSize;
    } else if(gap) {
      *gap=0;
    }
	if(!(mh=CreateFileMapping(fh, NULL, PAGE_READWRITE, 0, len+g, NULL))) {
		return 0;
	}
	p = (char*)MapViewOfFile(mh, FILE_MAP_ALL_ACCESS, 0,
                           offset-*gap, len+*gap);
	CloseHandle(mh);
#else
	if(offset) {
		pgsz = sysconf(_SC_PAGESIZE);
		g = *gap = offset%pgsz;
	} else if(gap) {
		*gap=0;
	}
	p = mmap((void*)0, len+g, access,
           MAP_SHARED|MAP_FILE,
           fd, offset-g);
	if((long)p==-1L) {
		perror("mmap failed");
		p=0;
	}
#endif
	return p;
}


int Munmap(void	*p,size_t	len,int	sync)
{
	int	r=0;
  
#if defined(_WIN32)
	if(sync) FlushViewOfFile(p, len);
	UnmapViewOfFile(p);
#else
	if(sync) msync(p, len, MS_SYNC);
	if((r=munmap((void*)p, len))) perror("munmap() failed");
#endif
	return r;
}