Example #1
0
void CEx27bView::OnEditCopyto() 
{
    // Copy text to an .STG file (nothing special about STG ext)
    CFileDialog dlg(FALSE, "stg", "*.stg");
    if (dlg.DoModal() != IDOK) {
        return;
    }
    CEx27bDoc* pDoc = GetDocument();
    // Create a structured storage home for the object (m_pStgSub).
    //  Create a root storage file, then a substorage named "sub."
	LPSTORAGE pStgRoot;
	VERIFY(::StgCreateDocfile(dlg.GetPathName().AllocSysString(),
		   STGM_READWRITE|STGM_SHARE_EXCLUSIVE|STGM_CREATE,
		   0, &pStgRoot) == S_OK);
	ASSERT(pStgRoot != NULL);
	
	LPSTORAGE pStgSub;
    VERIFY(pStgRoot->CreateStorage(CEx27bDoc::s_szSub,
    	   STGM_CREATE|STGM_READWRITE|STGM_SHARE_EXCLUSIVE,
    	   0, 0, &pStgSub) == S_OK); 
	ASSERT(pStgSub != NULL);

	// Get the IPersistStorage* for the object
	LPPERSISTSTORAGE pPS = NULL;
	VERIFY(pDoc->m_lpOleObj->QueryInterface(IID_IPersistStorage,
		  (void**) &pPS) == S_OK);
    // Finally, save the object in its new home in the user's file

	VERIFY(::OleSave(pPS, pStgSub, FALSE) == S_OK); 
	// FALSE means different stg
	pPS->SaveCompleted(NULL);  // What does this do?
	pPS->Release();

	pStgSub->Release();
	pStgRoot->Release();
}
Example #2
0
void ReadDirectory(const char* szPath, LPSTORAGE pStg)
{
    // recursive function
    USES_CONVERSION;
    WIN32_FIND_DATA fData;
    HANDLE h;
    char szNewPath[MAX_PATH];
    char szStorageName[100];
    char szStreamName[100];
	char szData[81];
    char* pch = NULL;

    LPSTORAGE pSubStg = NULL;
    LPSTREAM pStream = NULL;

    g_nIndent++;
    strcpy(szNewPath, szPath);
    strcat(szNewPath, "*.*");
    h = ::FindFirstFile(szNewPath, &fData);
    if (h == (HANDLE) 0xFFFFFFFF) return;  // can't find directory
    do {
      if (!strcmp(fData.cFileName, "..") ||
          !strcmp(fData.cFileName, ".") ) continue;
	  while((pch = strchr(fData.cFileName, '!')) != NULL) {
	      *pch = '|';
	  }
      if (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
		// It's a directory, so make a storage
        strcpy(szNewPath, szPath);
        strcat(szNewPath,fData.cFileName);
        strcat(szNewPath, "\\");

        strcpy(szStorageName, fData.cFileName);
        szStorageName[31] = '\0';    // limit imposed by OLE
        TRACE("%0.*sStorage = %s\n", (g_nIndent - 1) * 4,
              g_szBlanks, szStorageName);
        VERIFY(pStg->CreateStorage(T2COLE(szStorageName),
            STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
            0, 0, &pSubStg) == S_OK);
        ASSERT(pSubStg != NULL);
        ReadDirectory(szNewPath, pSubStg);
        pSubStg->Release();
      }
      else {
        if ((pch = strrchr(fData.cFileName, '.')) != NULL) {
          if (!stricmp(pch, ".TXT")) {
			// It's a text file, so make a stream
			strcpy(szStreamName, fData.cFileName);
            strcpy(szNewPath, szPath);
            strcat(szNewPath, szStreamName);
 			szStreamName[32] = '\0'; // OLE max length
            TRACE("%0.*sStream = %s\n", (g_nIndent - 1) * 4,
                g_szBlanks, szNewPath);
			CStdioFile file(szNewPath, CFile::modeRead);
            // Ignore zero-length files
			if(file.ReadString(szData, 80)) {
              TRACE("%s\n", szData);
              VERIFY(pStg->CreateStream(T2COLE(szStreamName),
                     STGM_CREATE | STGM_READWRITE | 
				     STGM_SHARE_EXCLUSIVE,
                     0, 0, &pStream) == S_OK);
              ASSERT(pStream != NULL);
			  // Include the null terminator in the stream
              pStream->Write(szData, strlen(szData) + 1, NULL);
              pStream->Release();
            }
          }
        }
      }
    } while (::FindNextFile(h, &fData));
    g_nIndent--;
}