bool FileSystem::listFiles(const char* dirPath, std::vector<std::string>& files) { #ifdef WIN32 std::string path(FileSystem::getResourcePath()); if (dirPath && strlen(dirPath) > 0) { path.append(dirPath); } path.append("/*"); // Convert char to wchar std::basic_string<TCHAR> wPath; wPath.assign(path.begin(), path.end()); WIN32_FIND_DATA FindFileData; HANDLE hFind = FindFirstFile(wPath.c_str(), &FindFileData); if (hFind == INVALID_HANDLE_VALUE) { return false; } do { // Add to the list if this is not a directory if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) { // Convert wchar to char std::basic_string<TCHAR> wfilename(FindFileData.cFileName); std::string filename; filename.assign(wfilename.begin(), wfilename.end()); files.push_back(filename); } } while (FindNextFile(hFind, &FindFileData) != 0); FindClose(hFind); return true; #else std::string path(FileSystem::getResourcePath()); if (dirPath && strlen(dirPath) > 0) { path.append(dirPath); } path.append("/."); bool result = false; struct dirent* dp; DIR* dir = opendir(path.c_str()); if (dir != NULL) { while ((dp = readdir(dir)) != NULL) { std::string filepath(path); filepath.append("/"); filepath.append(dp->d_name); struct stat buf; if (!stat(filepath.c_str(), &buf)) { // Add to the list if this is not a directory if (!S_ISDIR(buf.st_mode)) { files.push_back(dp->d_name); } } } closedir(dir); result = true; } #ifdef __ANDROID__ // List the files that are in the android APK at this path AAssetDir* assetDir = AAssetManager_openDir(__assetManager, dirPath); if (assetDir != NULL) { AAssetDir_rewind(assetDir); const char* file = NULL; while ((file = AAssetDir_getNextFileName(assetDir)) != NULL) { std::string filename(file); // Check if this file was already added to the list because it was copied to the SD card. if (find(files.begin(), files.end(), filename) == files.end()) { files.push_back(filename); } } AAssetDir_close(assetDir); result = true; } #endif return result; #endif }
types::Function::ReturnValue sci_hdf5_load(types::typed_list &in, int _iRetCount, types::typed_list& out) { int rhs = static_cast<int>(in.size()); if (rhs < 1) { Scierror(999, _("%s: Wrong number of input argument(s): at least %d expected.\n"), fname.data(), 1); return types::Function::Error; } if (in[0]->getId() != types::InternalType::IdScalarString) { Scierror(999, _("%s: Wrong size for input argument #%d: string expected.\n"), fname.data(), 1); return types::Function::Error; } wchar_t* wcfilename = expandPathVariableW(in[0]->getAs<types::String>()->get()[0]); char* cfilename = wide_string_to_UTF8(wcfilename); std::string filename(cfilename); std::wstring wfilename(wcfilename); FREE(cfilename); FREE(wcfilename); if (FileExistW(wfilename.data()) == FALSE) { Scierror(999, _("%s: Unable to open file: '%s'.\n"), fname.data(), filename.data()); return types::Function::Error; } //library ? if (isHDF5File(filename.data()) == false) { //lib file int err = 0; types::Library* lib = loadlib(in[0]->getAs<types::String>()->get()[0], &err); switch (err) { case 0: break; case 2: Scierror(999, "%s: %s", fname.data(), _("Redefining permanent variable.\n")); return types::Function::Error; default: Scierror(999, _("%s: %s is not a valid lib file.\n"), fname.data(), filename.data()); return types::Function::Error; } lib->killMe(); return types::Function::OK; } int iFile = openHDF5File(filename.data(), 0); if (iFile < 0) { Scierror(999, _("%s: Unable to open file: %s\n"), fname.data(), filename.data()); return types::Function::Error; } std::wstring wstFuncName; //manage version information int version = getSODFormatAttribute(iFile); closeHDF5File(iFile); bool needReprocess = false; switch (version) { case -1: case 1: { wstFuncName = L"hdf5_load_v1"; needReprocess = true; break; } case 2: { wstFuncName = L"hdf5_load_v2"; needReprocess = true; break; } case 3: { wstFuncName = L"hdf5_load_v3"; break; } default : { Scierror(999, _("%s: Wrong SOD file format version. Max Expected: %d Found: %d\n"), fname.data(), SOD_FILE_VERSION, version); return types::Function::Error; } } types::typed_list out1; types::Function::ReturnValue ret = Overload::call(wstFuncName, in, _iRetCount, out1); if (ret != types::Function::OK) { Scierror(999, _("%s: Unable to load '%s'\n"), fname.data(), filename.data()); return types::Function::Error; } if (needReprocess) { //call %sodload types::String* vars = out1[0]->getAs<types::String>(); vars->IncreaseRef(); int size = vars->getSize(); types::typed_list in2(1, vars); types::typed_list out2; std::wstring wstFuncName = L"%_sodload"; ret = Overload::call(wstFuncName, in2, size, out2); vars->DecreaseRef(); symbol::Context* ctx = symbol::Context::getInstance(); wchar_t** names = vars->get(); //update context with values return by %_sodload for (int i = 0; i < size; ++i) { ctx->put(symbol::Symbol(names[i]), out2[i]); } vars->killMe(); } else { out.push_back(out1.front()); } return ret; }
LRESULT CALLBACK WndProc (HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { HDC hdc; LPDCB lpDCB = &cc.dcb; static int cxClient,cyClient; PAINTSTRUCT ps; static int iVertPos = 0; SCROLLINFO si; char buffer[128] = {0}; DWORD bytesRead; DWORD bytesWritten; //OVERLAPPED ov = {0,0,0}; std::fstream ifs; OPENFILENAME ofn; static TCHAR szFilter[] = TEXT ("All Files (*.*)\0*.*\0\0") ; static TCHAR szFileName[MAX_PATH], szTitleName[MAX_PATH] ; char packet[1024]; char packet2[2]; if (cxClient && cyClient) { /*text_area.left = 0; text_area.top = 0; text_area.right = cxClient; text_area.bottom = cyClient; areaset = true;*/ topright.left = cxClient - cxClient/3 + 1; topright.top = 1; topright.right = cxClient; topright.bottom = cyClient - cyClient/3 - 1; topleft.left = 1; topleft.top = 1; topleft.right = cxClient - cxClient/3 - 1; topleft.bottom = cyClient - cyClient/3 - 1; } switch (Message) { case WM_CREATE: ov.Offset = 0; ov.OffsetHigh = 0; ov.Pointer = 0; ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); semm = CreateSemaphore(NULL, 0, 1, NULL); global.hSem = &semm; InitializeCriticalSection(§ion); break; case WM_COMMAND: switch (LOWORD (wParam)) { case IDM_COM1: cc.dwSize = sizeof(COMMCONFIG); cc.wVersion = 0x100; if (!CommConfigDialog (lpszCommName1, hwnd, &cc)) break; else { comset = true; curCom = 1; } break; case IDM_COM2: cc.dwSize = sizeof(COMMCONFIG); cc.wVersion = 0x100; if (!CommConfigDialog (lpszCommName2, hwnd, &cc)) break; else { comset = true; curCom = 2; } break; case IDM_COM3: cc.dwSize = sizeof(COMMCONFIG); cc.wVersion = 0x100; if (!CommConfigDialog (lpszCommName3, hwnd, &cc)) break; else { comset = true; curCom = 3; } break; case IDM_COM4: cc.dwSize = sizeof(COMMCONFIG); cc.wVersion = 0x100; if (!CommConfigDialog (lpszCommName4, hwnd, &cc)) { break; }else { comset = true; curCom = 4; } break; case IDM_Connect: //WaitForSingleObject(ov.hEvent, INFINITE); //MessageBox(hwnd, TEXT(""), TEXT(""), MB_OK); if (comset) { if (curCom == 1) { if ((hComm = CreateFile (lpszCommName1, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL)) == INVALID_HANDLE_VALUE) { MessageBox (NULL, TEXT("Error opening COM port:"), TEXT(""), MB_OK); return FALSE; } } else if (curCom == 2) { if ((hComm = CreateFile (lpszCommName2, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, NULL, NULL)) == INVALID_HANDLE_VALUE) { MessageBox (NULL, TEXT("Error opening COM port:"), TEXT(""), MB_OK); return FALSE; } } else if (curCom == 3) { if ((hComm = CreateFile (lpszCommName3, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, NULL, NULL)) == INVALID_HANDLE_VALUE) { MessageBox (NULL, TEXT("Error opening COM port:"), TEXT(""), MB_OK); return FALSE; } } else if (curCom == 4) { if ((hComm = CreateFile (lpszCommName4, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL)) == INVALID_HANDLE_VALUE) { MessageBox (NULL, TEXT("Error opening COM port:"), TEXT(""), MB_OK); return FALSE; } } lpDCB = &cc.dcb; if (!SetCommState(hComm, lpDCB)) { MessageBox (NULL, TEXT("Error setting COM state"), TEXT(""), MB_OK); return false; } COMMTIMEOUTS timeouts; timeouts.ReadIntervalTimeout = MAXDWORD; timeouts.ReadTotalTimeoutMultiplier = 0; timeouts.ReadTotalTimeoutConstant = 0; timeouts.WriteTotalTimeoutMultiplier = 0; timeouts.WriteTotalTimeoutConstant = 0; if (!SetCommTimeouts(hComm, &timeouts)) { MessageBox (NULL, TEXT("Error setting COM timeouts"), TEXT(""), MB_OK); return false; } if (hComm) { global.hComm = &hComm; global.ov = ov; //opThrd = CreateThread(NULL, 0, OutputThread, (LPVOID)hwnd, 0, &opThrdID ); recvThread = CreateThread(NULL, 0, receiverThread, &global, NULL, &opThrdID); MessageBox(NULL, TEXT("connected"), TEXT(""), MB_OK); } else { MessageBox(NULL, TEXT("Please select a COM port."), TEXT(""), MB_OK); } } else { MessageBox(NULL, TEXT("Please select a COM port."), TEXT(""), MB_OK); } break; case IDM_Disconnect: //kills the reading thread and closes the com port if they are active if (KillReader == false && hComm) { KillReader = true; CloseHandle(hComm); MessageBox(NULL, TEXT("disconnected"), TEXT(""), MB_OK); } break; case IDM_OpenFile: ofn.lStructSize = sizeof (OPENFILENAME) ; ofn.hwndOwner = hwnd ; ofn.hInstance = NULL ; ofn.lpstrFilter = szFilter ; ofn.lpstrCustomFilter = NULL ; ofn.nMaxCustFilter = 0 ; ofn.nFilterIndex = 0 ; ofn.lpstrFile = NULL ; // Set in Open and Close functions ofn.nMaxFile = MAX_PATH ; ofn.lpstrFileTitle = NULL ; // Set in Open and Close functions ofn.nMaxFileTitle = MAX_PATH ; ofn.lpstrInitialDir = NULL ; ofn.lpstrTitle = NULL ; ofn.Flags = 0 ; // Set in Open and Close functions ofn.nFileOffset = 0 ; ofn.nFileExtension = 0 ; ofn.lpstrDefExt = TEXT ("txt") ; ofn.lCustData = 0L ; ofn.lpfnHook = NULL ; ofn.lpTemplateName = NULL ; ofn.hwndOwner = hwnd ; ofn.lpstrFile = szFileName ; ofn.lpstrFileTitle = szTitleName ; ofn.Flags = OFN_HIDEREADONLY | OFN_CREATEPROMPT ; if (GetOpenFileName(&ofn)) { std::wstring wfilename(szFileName); std::string filename; for(int i = 0; i < wfilename.size(); i++) { filename += wfilename[i]; } ifs = OpenFile(filename); readFile(ifs); DWORD threadID; DWORD exitStatus; if (sendThread == 0 || (GetExitCodeThread(sendThread, &exitStatus) && exitStatus != STILL_ACTIVE)) { sendThread = CreateThread(NULL, 0, sendBufferThread, &global, NULL, &threadID); } } } break; case WM_CHAR: // Process keystroke break; case WM_LBUTTONDOWN: break; case WM_SIZE: cxClient = LOWORD(lParam); cyClient = HIWORD(lParam); si.cbSize = sizeof(si); si.fMask = SIF_ALL; si.nMin = 0; si.nMax = cyClient; si.nPos = 0; si.nPage = 50; SetScrollInfo(hwnd, SB_VERT, &si, TRUE); break; case WM_VSCROLL: si.cbSize = sizeof(si); si.fMask = SIF_ALL; GetScrollInfo(hwnd, SB_VERT, &si); iVertPos = si.nPos; switch(LOWORD(wParam)){ case SB_LINEUP: si.nPos -= 10; break; case SB_LINEDOWN: si.nPos += 10; break; case SB_PAGEUP: si.nPos -= si.nPage; break; case SB_PAGEDOWN: si.nPos += si.nPage; break; case SB_THUMBTRACK: si.nPos = si.nTrackPos; break; default: break; } si.fMask = SIF_POS; SetScrollInfo(hwnd, SB_VERT, &si, TRUE); GetScrollInfo(hwnd, SB_VERT, &si); //if there was change in the vertical scroll bar, make adjustments to redraw if (si.nPos != iVertPos){ //InvalidateRect(hwnd, &text_area, TRUE); } break; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); MoveToEx(hdc, cxClient - cxClient/3, 0, NULL); LineTo(hdc, cxClient - cxClient/3, cyClient); MoveToEx(hdc, 0, cyClient - cyClient/3, NULL); LineTo(hdc, cxClient, cyClient - cyClient/3); DisplayStatistics(topright, stats, hdc); //DisplayReceivedFileData(topleft, file, hdc); ReleaseDC(hwnd, hdc); break; case WM_DESTROY: // Terminate program if (hComm) { CloseHandle(hComm); } PostQuitMessage (0); break; default: return DefWindowProc (hwnd, Message, wParam, lParam); } return 0; }
bool FileSystem::listFiles(const char* dirPath, std::vector<std::string>& files) { #ifdef WIN32 std::string path(FileSystem::getResourcePath()); if (dirPath && strlen(dirPath) > 0) { path.append(dirPath); } path.append("/*"); // Convert char to wchar std::basic_string<TCHAR> wPath; wPath.assign(path.begin(), path.end()); WIN32_FIND_DATA FindFileData; HANDLE hFind = FindFirstFile(wPath.c_str(), &FindFileData); if (hFind == INVALID_HANDLE_VALUE) { return false; } do { // Add to the list if this is not a directory if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) { // Convert wchar to char std::basic_string<TCHAR> wfilename(FindFileData.cFileName); std::string filename; filename.assign(wfilename.begin(), wfilename.end()); files.push_back(filename); } } while (FindNextFile(hFind, &FindFileData) != 0); FindClose(hFind); return true; #else std::string path(FileSystem::getResourcePath()); if (dirPath && strlen(dirPath) > 0) { path.append(dirPath); } path.append("/."); struct dirent* dp; DIR* dir = opendir(path.c_str()); if (!dir) { return false; } while ((dp = readdir(dir)) != NULL) { std::string filepath(path); filepath.append("/"); filepath.append(dp->d_name); struct stat buf; if (!stat(filepath.c_str(), &buf)) { // Add to the list if this is not a directory if (!S_ISDIR(buf.st_mode)) { files.push_back(dp->d_name); } } } closedir(dir); return true; #endif }