void BrowserApp::ReadyToRun() { // Since we will essentially run the GUI... set_thread_priority(Thread(), B_DISPLAY_PRIORITY); BWebPage::InitializeOnce(); BWebPage::SetCacheModel(B_WEBKIT_CACHE_MODEL_WEB_BROWSER); BPath path; if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) == B_OK && path.Append(kApplicationName) == B_OK && create_directory(path.Path(), 0777) == B_OK) { BWebSettings::SetPersistentStoragePath(path.Path()); } BString mainSettingsPath(kApplicationName); mainSettingsPath << "/Application"; fSettings = new SettingsMessage(B_USER_SETTINGS_DIRECTORY, mainSettingsPath.String()); #if ENABLE_NATIVE_COOKIES mainSettingsPath = kApplicationName; mainSettingsPath << "/Cookies"; fCookies = new SettingsMessage(B_USER_SETTINGS_DIRECTORY, mainSettingsPath.String()); BMessage cookieArchive; cookieArchive = fCookies->GetValue("cookies", cookieArchive); fCookieJar = new BNetworkCookieJar(cookieArchive); BWebPage::SetCookieJar(fCookieJar); #endif fLastWindowFrame = fSettings->GetValue("window frame", fLastWindowFrame); BRect defaultDownloadWindowFrame(-10, -10, 365, 265); BRect downloadWindowFrame = fSettings->GetValue("downloads window frame", defaultDownloadWindowFrame); BRect settingsWindowFrame = fSettings->GetValue("settings window frame", BRect()); bool showDownloads = fSettings->GetValue("show downloads", false); fDownloadWindow = new DownloadWindow(downloadWindowFrame, showDownloads, fSettings); if (downloadWindowFrame == defaultDownloadWindowFrame) { // Initially put download window in lower right of screen. BRect screenFrame = BScreen().Frame(); BMessage decoratorSettings; fDownloadWindow->GetDecoratorSettings(&decoratorSettings); float borderWidth = 0; if (decoratorSettings.FindFloat("border width", &borderWidth) != B_OK) borderWidth = 5; fDownloadWindow->MoveTo(screenFrame.Width() - fDownloadWindow->Frame().Width() - borderWidth, screenFrame.Height() - fDownloadWindow->Frame().Height() - borderWidth); } fSettingsWindow = new SettingsWindow(settingsWindowFrame, fSettings); BWebPage::SetDownloadListener(BMessenger(fDownloadWindow)); fInitialized = true; int32 pagesCreated = 0; bool fullscreen = false; if (fLaunchRefsMessage) { _RefsReceived(fLaunchRefsMessage, &pagesCreated, &fullscreen); delete fLaunchRefsMessage; fLaunchRefsMessage = NULL; } if (pagesCreated == 0) _CreateNewWindow("", fullscreen); PostMessage(PRELOAD_BROWSING_HISTORY); }
void gui_init(int argc, char** argv) { char buf[PATH_MAX]; if (pipe(sEventPipe) < 0) return; if (!replicated) { sBAppThreadID = spawn_thread(bapp_thread, "BApplication(NetSurf)", B_NORMAL_PRIORITY, (void *)find_thread(NULL)); if (sBAppThreadID < B_OK) return; /* #### handle errors */ if (resume_thread(sBAppThreadID) < B_OK) return; } // ui_color() gives hardcoded values before BApplication is created. nsbeos_update_system_ui_colors(); fetch_rsrc_register(); check_homedir(); // make sure the cache dir exists create_directory(TEMP_FILENAME_PREFIX, 0600); //nsbeos_completion_init(); /* This is an ugly hack to just get the new-style throbber going. * It, along with the PNG throbber loader, need making more generic. */ { #define STROF(n) #n #define FIND_THROB(n) filenames[(n)] = \ "throbber" STROF(n) ".png"; char *filenames[9]; FIND_THROB(0); FIND_THROB(1); FIND_THROB(2); FIND_THROB(3); FIND_THROB(4); FIND_THROB(5); FIND_THROB(6); FIND_THROB(7); FIND_THROB(8); nsbeos_throbber_initialise_from_png(9, filenames[0], filenames[1], filenames[2], filenames[3], filenames[4], filenames[5], filenames[6], filenames[7], filenames[8]); #undef FIND_THROB #undef STROF } if (nsbeos_throbber == NULL) die("Unable to load throbber image.\n"); find_resource(buf, "Choices", "%/Choices"); LOG(("Using '%s' as Preferences file", buf)); options_file_location = strdup(buf); nsoption_read(buf); /* check what the font settings are, setting them to a default font * if they're not set - stops Pango whinging */ #define SETFONTDEFAULT(OPTION,y) if (nsoption_charp(OPTION) == NULL) nsoption_set_charp(OPTION, strdup((y))) //XXX: use be_plain_font & friends, when we can check if font is serif or not. /* font_family family; font_style style; be_plain_font->GetFamilyAndStyle(&family, &style); SETFONTDEFAULT(font_sans, family); SETFONTDEFAULT(font_serif, family); SETFONTDEFAULT(font_mono, family); SETFONTDEFAULT(font_cursive, family); SETFONTDEFAULT(font_fantasy, family); */ #ifdef __HAIKU__ SETFONTDEFAULT(font_sans, "DejaVu Sans"); SETFONTDEFAULT(font_serif, "DejaVu Serif"); SETFONTDEFAULT(font_mono, "DejaVu Mono"); SETFONTDEFAULT(font_cursive, "DejaVu Sans"); SETFONTDEFAULT(font_fantasy, "DejaVu Sans"); #else SETFONTDEFAULT(font_sans, "Bitstream Vera Sans"); SETFONTDEFAULT(font_serif, "Bitstream Vera Serif"); SETFONTDEFAULT(font_mono, "Bitstream Vera Sans Mono"); SETFONTDEFAULT(font_cursive, "Bitstream Vera Serif"); SETFONTDEFAULT(font_fantasy, "Bitstream Vera Serif"); #endif nsbeos_options_init(); if (nsoption_charp(cookie_file) == NULL) { find_resource(buf, "Cookies", "%/Cookies"); LOG(("Using '%s' as Cookies file", buf)); nsoption_set_charp(cookie_file, strdup(buf)); } if (nsoption_charp(cookie_jar) == NULL) { find_resource(buf, "Cookies", "%/Cookies"); LOG(("Using '%s' as Cookie Jar file", buf)); nsoption_set_charp(cookie_jar, strdup(buf)); } if ((nsoption_charp(cookie_file) == NULL) || (nsoption_charp(cookie_jar) == NULL)) die("Failed initialising cookie options"); if (nsoption_charp(url_file) == NULL) { find_resource(buf, "URLs", "%/URLs"); LOG(("Using '%s' as URL file", buf)); nsoption_set_charp(url_file, strdup(buf)); } if (nsoption_charp(ca_path) == NULL) { find_resource(buf, "certs", "/etc/ssl/certs"); LOG(("Using '%s' as certificate path", buf)); nsoption_set_charp(ca_path, strdup(buf)); } //find_resource(buf, "mime.types", "/etc/mime.types"); beos_fetch_filetype_init(); urldb_load(nsoption_charp(url_file)); urldb_load_cookies(nsoption_charp(cookie_file)); //nsbeos_download_initialise(); if (!replicated) be_app->Unlock(); }
int32 RipView::RipThread(void *data) { acquire_sem(abort_thread); RipView *view = (RipView*)data; view->Window()->Lock(); view->fProgressBar->SetText(_T("Creating songs from CD...")); view->Window()->Unlock(); // Get all the preferences that we'll need in one shot to save on piddling // around with locking prefsLock.Lock(); int16 foldermode; if (preferences.FindInt16("foldermode",&foldermode)!=B_OK) foldermode=1; int16 trackname; if (preferences.FindInt16("namestyle",&trackname)!=B_OK) trackname=0; int16 bitrate; if (preferences.FindInt16("bitrate",&bitrate)!=B_OK) bitrate=1; BString destfolder; if (preferences.FindString("path",&destfolder)!=B_OK) destfolder="/boot/home/music"; bool use_mp3; if (preferences.FindBool("usemp3",&use_mp3)!=B_OK) use_mp3=false; if (use_mp3 && (!gMP3Format || !gMP3Codec)) use_mp3=false; bool make_playlist; if (preferences.FindBool("makeplaylist",&make_playlist)!=B_OK) make_playlist=true; BString playlistfolder; if (preferences.FindString("playlistfolder",&playlistfolder)!=B_OK) { playlistfolder=destfolder; playlistfolder << "/playlists"; } prefsLock.Unlock(); bool write_attributes=true; dev_t destDevice = dev_for_path(destfolder.String()); BVolume volume(destDevice); if (!volume.KnowsAttr()) { write_attributes=false; //printf("Volume for %s doesn't support attributes\n",destfolder.String()); } // If we are grouping by artist or artist/album, we need to make sure the // directory exists switch(foldermode) { case 1: // Artist { destfolder << "/" << gCDData.Artist() << "/"; break; } case 2: // Album { destfolder << "/" << gCDData.Album() << "/"; break; } case 3: // Artist & Album { destfolder << "/" << gCDData.Artist() << "/" << gCDData.Album() << "/"; break; } default: // no special grouping { break; } } if (create_directory(destfolder.String(),0777)!=B_OK) { BEntry dir(destfolder.String()); if (!dir.Exists()) { BString errormsg; #ifdef SYS_ZETA errormsg=_T("Uh-oh... SimplyVorbis couldn't create the folder '"); errormsg << destfolder << _T("RipViewMultiline1"); BAlert *alert = new BAlert("SimplyVorbis",errormsg.String(),_T("OK")); #else errormsg="Uh-oh... SimplyVorbis couldn't create the folder '"; errormsg << destfolder << "'.\n\nThis may have happened for a number of different reasons, but " "most often happens when making music files on a non-BeOS drive, such as one shared " "with Windows. Certain characters, such as question marks and slashes cause problems " "on these disks. You may want to check the names of the artist, album, and songs for " "such characters and put a good substitute in its place or remove the character entirely."; BAlert *alert = new BAlert("SimplyVorbis",errormsg.String(),"OK"); #endif alert->Go(); view->fRipThread = -1; view->Window()->PostMessage(M_STOP_ENCODING); release_sem(abort_thread); return 0; } } // make the directory only if the user wants to create playlists if (make_playlist && create_directory(playlistfolder.String(),0777)!=B_OK) { BEntry playdir(playlistfolder.String()); if (!playdir.Exists()) { BString errormsg; #ifdef SYS_ZETA errormsg=_T("Uh-oh... SimplyVorbis couldn't create the folder '"); errormsg << playlistfolder << _T("RIpViewMultiline2"); BAlert *alert = new BAlert("SimplyVorbis",errormsg.String(),_T("OK")); #else errormsg="Uh-oh... SimplyVorbis couldn't create the folder '"; errormsg << playlistfolder << "' for the playlists.\n\nThis may have happened for a number of different reasons, but " "most often happens when making playlists on a non-BeOS drive, such as one shared " "with Windows. Certain characters, such as question marks and slashes cause problems " "on these disks. You may want to check the names of the artist, album, and songs for " "such characters and put a good substitute in its place or remove the character entirely." "For the moment, your music will be created, but the playlist will not. Sorry."; BAlert *alert = new BAlert("SimplyVorbis",errormsg.String(),"OK"); #endif alert->Go(); make_playlist=false; } } // *Sigh* FAT32 volumes don't support use of question marks. I wonder what else... :( if (!write_attributes) { destfolder.RemoveAll("?"); playlistfolder.RemoveAll("?"); } BString trackPrefix; switch(trackname) { case 0: // Artist { trackPrefix = gCDData.Artist(); trackPrefix+=" - "; break; } case 1: // Album { trackPrefix = gCDData.Album(); trackPrefix+=" - "; break; } case 2: // Artist & Album { trackPrefix = gCDData.Artist(); trackPrefix << " - " << gCDData.Album() << " - "; break; } default: // no special grouping { break; } } // Populate the list of tracks to be ripped view->Window()->Lock(); for(int32 i=view->fRipList->CountItems(); i>0; i--) { BStringItem *item = (BStringItem *)view->fRipList->RemoveItem(i); delete item; } for(int32 i=0; i<gTrackList.CountItems(); i++) { if (*(gTrackList.ItemAt(i))) { if (gCDDrive.IsDataTrack(i)) { *(gTrackList.ItemAt(i)) = false; continue; } view->fRipList->AddItem(new BStringItem(gCDData.TrackAt(i))); } } view->Window()->Unlock(); // playlists are a nice thing for quite a few people, apparently. :) BFile playlistfile; BString playlistfilename=playlistfolder; if (make_playlist) { playlistfilename << "/" << gCDData.Artist() << " - " << gCDData.Album() << ".m3u"; // Append to playlists instead of overwriting them BEntry playlistentry(playlistfilename.String()); if (playlistentry.Exists()) { if (playlistfile.SetTo(playlistfilename.String(),B_READ_WRITE)==B_OK) { // HACK: This works around a bug in R5's BFile::Seek implementation // playlistfile.Seek(SEEK_END,0); off_t filesize; playlistfile.GetSize(&filesize); if (filesize>0) { char data[filesize]; playlistfile.Read(&data,filesize); } } else playlistfile.SetTo(playlistfilename.String(),B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE); } else playlistfile.SetTo(playlistfilename.String(),B_READ_WRITE | B_CREATE_FILE); if (playlistfile.InitCheck()!=B_OK) { BString errormsg; #ifdef SYS_ZETA errormsg=_T("Uh-oh... SimplyVorbis couldn't create the playlist '"); errormsg << playlistfilename << _T("RIpViewMultiline3"); BAlert *alert = new BAlert("SimplyVorbis",errormsg.String(),_T("OK")); #else errormsg="Uh-oh... SimplyVorbis couldn't create the playlist '"; errormsg << playlistfilename << "'.\n\nThis may have happened for a number of different reasons, but " "most often happens when making playlists on a non-BeOS drive, such as one shared " "with Windows. Certain characters, such as question marks and slashes cause problems " "on these disks. You may want to check the names of the artist, album, and songs for " "such characters and put a good substitute in its place or remove the character entirely." "For the moment, your music will be created, but the playlist will not. Sorry."; BAlert *alert = new BAlert("SimplyVorbis",errormsg.String(),"OK"); #endif alert->Go(); make_playlist = false; } } BString syscmd; BMessenger msgr(view); bool copyfailed = false; bool showfailalert = 0; for (int32 i = 0; i < gTrackList.CountItems(); i++) { if (*(gTrackList.ItemAt(i))) { view->Window()->Lock(); BStringItem *oldtrack = (BStringItem*)view->fRipList->RemoveItem(0L); view->Window()->Unlock(); delete oldtrack; BString filename(trackPrefix); filename += gCDData.TrackAt(i); // FAT32 is such a pain in the neck here. Certain characters which are *just* *fine* for // other OSes are illegal. Just for the user's sake, we will constrain ourselves to its limits. // This means that question marks, double quotes, and colons will be substituted or removed. // Although it has not popped up in testing, backslashes are also substituted just in case filename.RemoveSet("?"); filename.ReplaceAll("/", "-"); filename.ReplaceAll("\\", "-"); filename.ReplaceAll("\"", "'"); filename.ReplaceAll(":", "-"); // Set things up for the progress bar for ripping this particular track view->Window()->Lock(); view->fProgressLabel=_T("Converting '"); view->fProgressLabel << gCDData.TrackAt(i) << "'"; view->fProgressBar->SetText(view->fProgressLabel.String()); rgb_color blue={50,150,255,255}; view->fProgressBar->SetBarColor(blue); cdaudio_time tracklength; gCDDrive.GetTimeForTrack(i+1,tracklength); view->fProgressBar->SetMaxValue( (tracklength.minutes * 60) + tracklength.seconds); view->fProgressDelta = 100.0 / float( (tracklength.minutes * 60) + tracklength.seconds); view->fProgressBar->Reset(); view->Window()->Unlock(); BString ripname(filename); ripname += use_mp3 ? ".mp3" : ".ogg"; cdaudio_time rippedtime; status_t ripstat=B_OK; if (use_mp3) ripstat=ConvertTrack(gCDDrive.GetDrivePath(),ripname.String(),i+1,*gMP3Format, *gMP3Codec,&msgr,abort_thread); else ripstat=VorbifyTrack(gCDDrive.GetDrivePath(),ripname.String(),i+1,&msgr,abort_thread); if (ripstat==B_INTERRUPTED) { // This will unblock the window view->fRipThread = -1; view->Window()->PostMessage(M_STOP_ENCODING); release_sem(abort_thread); BEntry entry(ripname.String()); if (entry.Exists()) entry.Remove(); return -1; } else if (ripstat!=B_OK) { // Because things aren't really accurate on some CDs on the last audio track, // we bear with it. if (!gCDDrive.IsDataTrack(i+1)) { view->Window()->Lock(); view->fProgressBar->SetText(_T("Couldn't read song from the CD. Sorry!")); view->Window()->Unlock(); BEntry entry(ripname.String()); if (entry.Exists()) entry.Remove(); continue; } else { view->Window()->Lock(); rgb_color darkblue={0,0,127,255}; view->fProgressBar->Update(view->fProgressBar->MaxValue() - view->fProgressBar->CurrentValue(), _T("Finishing early. This is not a bad thing.")); view->fProgressBar->SetBarColor(darkblue); view->Window()->Unlock(); rippedtime = GetTimeRipped(); } } else { view->Window()->Lock(); rgb_color darkblue={0,0,127,255}; view->fProgressBar->SetBarColor(darkblue); view->Window()->Unlock(); } // This will ensure that the drive isn't running for an unnecesary amount of time gCDDrive.Stop(); // Set the mime type filename=destfolder; filename << trackPrefix << gCDData.TrackAt(i) << (use_mp3 ? ".mp3" : ".ogg"); BNode node(ripname.String()); #ifndef FAKE_RIPPING if (node.InitCheck()==B_OK) #endif { BNodeInfo nodeinfo(&node); if (nodeinfo.InitCheck()==B_OK) nodeinfo.SetType( use_mp3 ? "audio/x-mpeg" : "audio/x-vorbis"); if (write_attributes) { if (strlen(gCDData.Genre())>0) node.WriteAttr("Audio:Genre",B_STRING_TYPE,0,gCDData.Genre(),strlen(gCDData.Genre())+1); node.WriteAttr("Audio:Comment",B_STRING_TYPE,0,"Created by SimplyVorbis", strlen("Created by SimplyVorbis")+1); node.WriteAttr("Audio:Title",B_STRING_TYPE,0,gCDData.TrackAt(i), strlen(gCDData.TrackAt(i))+1); node.WriteAttr("Audio:Album",B_STRING_TYPE,0,gCDData.Album(), strlen(gCDData.Album())+1); node.WriteAttr("Audio:Artist",B_STRING_TYPE,0,gCDData.Artist(), strlen(gCDData.Artist())+1); int32 tracknum = i+1; node.WriteAttr("Audio:Track",B_INT32_TYPE,0,(const void *)&tracknum, sizeof(int32)); node.WriteAttr("Audio:Bitrate",B_STRING_TYPE,0,(const void *)"128", strlen("128")+1); cdaudio_time tracktime; if (gCDDrive.GetTimeForTrack(i+1,tracktime)) { char timestring[20]; // The only time when we will ever get this far when ripstat != B_OK is if // we have issues related to misreported track times on an enhanced CD. In this // case, we make use of the riptime variable declared above to find out // just how much was actually ripped in order to write the proper playing time if (ripstat!=B_OK) sprintf(timestring,"%.2ld:%.2ld",rippedtime.minutes,rippedtime.seconds); else sprintf(timestring,"%.2ld:%.2ld",tracktime.minutes,tracktime.seconds); node.WriteAttr("Audio:Length",B_STRING_TYPE,0,timestring, strlen(timestring)+1); } } // write the file's tags BString inString; syscmd = "tagwriter "; inString = gCDData.TrackAt(i); inString.CharacterEscape(FILE_ESCAPE_CHARACTERS,'\\'); syscmd << "-t " << inString; inString = gCDData.Artist(); inString.CharacterEscape("<> '\"\\|?[]{}():;`,",'\\'); syscmd << " -a " << inString; if (strlen(gCDData.Genre())>0) { inString = gCDData.Genre(); inString.CharacterEscape(FILE_ESCAPE_CHARACTERS,'\\'); syscmd << " -g " << inString; } if (strlen(gCDData.Album())>0) { inString = gCDData.Album(); inString.CharacterEscape(FILE_ESCAPE_CHARACTERS,'\\'); syscmd << " -A " << inString; } syscmd << " -T " << (i+1) << " "; syscmd << " -c 'Created by SimplyVorbis' "; inString = ripname; inString.ReplaceAll("/", " - "); inString.CharacterEscape(FILE_ESCAPE_CHARACTERS,'\\'); syscmd+=inString; //printf("Tag command: %s\n",syscmd.String()); system(syscmd.String()); // Move the file to the real destination BEntry entry(ripname.String()); #ifdef FAKE_RIPPING { { #else if (entry.Exists()) { BDirectory destination(destfolder.String()); // overwrite an existing file - allow re-ripping a file :) if (entry.MoveTo(&destination,NULL,true)!=B_OK) { #endif // chances are that if this failed, it's because the destination // path is not on the same volume view->Window()->Lock(); BString out(_T("Copying to ")); out << destfolder; view->fProgressBar->SetText(out.String()); view->Window()->Unlock(); BString cmddest(destfolder); cmddest.CharacterEscape("<> '\"\\|[]{}():;`,",'\\'); // *sigh* Certain characters are not allowed for FAT32 names. if (!write_attributes) { cmddest.RemoveAll("?"); syscmd="cp -fp "; syscmd << inString << " " << cmddest; } else { syscmd = "copyattr -d "; syscmd << inString << " " << cmddest; } //printf("Copy command: %s\n",syscmd.String()); if (system(syscmd.String())!=0) copyfailed=true; if (!copyfailed) { entry.Remove(); syscmd = "settype -t \"audio/x-vorbis\" "; syscmd << cmddest << inString; system(syscmd.String()); printf("type command: %s\n", syscmd.String()); } else { copyfailed=false; showfailalert++; } } BString playlistentry(destfolder.String()); playlistentry << ripname << "\n"; playlistfile.Write(playlistentry.String(),playlistentry.Length()); } } } gCDDrive.Stop(); #ifndef FAKE_RIPPING // This will show the alert once in the ripping process, as opposed to after every track. if (showfailalert == 1) { copyfailed=false; #ifdef SYS_ZETA BAlert *alert = new BAlert("SimplyVorbis",_T("RIpViewMultiline4"),_T("OK")); #else BAlert *alert = new BAlert("SimplyVorbis","SimplyVorbis ran into unexpected issues copying your " "music files to the music folder. They have not been lost, however. After clicking " "OK, a window will pop up and you can move them to wherever you want.","OK"); #endif alert->Go(); system("/boot/beos/system/Tracker . &"); } #endif } if (make_playlist) { BNodeInfo nodeinfo(&playlistfile); if (nodeinfo.InitCheck()==B_OK) nodeinfo.SetType("text/x-playlist"); playlistfile.Unset(); } view->Window()->Lock(); view->fProgressBar->SetText(_T("Finished.")); view->Window()->Unlock(); snooze(1000000); view->fRipThread = -1; view->Window()->PostMessage(M_STOP_ENCODING); release_sem(abort_thread); return 0; }
int main(int argc, char *argv[]) { char *bootfile = argv[1]; fprintf(stderr, "partition start=%d size=%d \n",PARTITION_START, PARTITION_SIZE); fprintf(stderr, "fat start=%d sectors=%d\n",FAT_START, FAT_SECTORS); fprintf(stderr, "data start=%d \n",DATA_START); memset(buffer, 0, BUF_SIZE); // MBR create_mbr(0, PARTITION_START, PARTITION_SIZE); // root block create_FAT16_root_block(PARTITION_START, PARTITION_START, PARTITION_SIZE, FAT_SECTORS, 0, 1, 6); // backup root block //create_FAT16_root_block(BACKUP_BPB, RESERVED_SECTORS, PARTITION_START, PARTITION_SIZE, FAT_SECTORS, 0, 1, 6); // FAT create_fat16_header(buffer+(512*FAT_START)); //fat_entry(2, EOC); uint8_t *root_dir = buffer+ROOT_DIR_START*512; create_sfn_entry(&root_dir, "PHATIO ", FAT_FILE_VOLUME, 0, 0); // start io directory in its own block free_cluster = 2; // create the io directory uint16_t io_cluster = free_cluster; create_file(&root_dir, "readme.txt", 0, 0, read_file("../fs/readme.txt"), -1); uint8_t *io_dir = create_directory(&root_dir, "io", 0, 0, 4); uint8_t *s = io_dir; uint8_t *mode_dir = create_directory(&io_dir, "mode", 0, io_cluster, 4); for (int i=0; i<20; i++) { char name[3]; sprintf(name, "%d", i); create_file(&mode_dir, name, 0, 0, "????", -1); } uint8_t *pins_dir = create_directory(&io_dir, "pins", 0, io_cluster, 4); for (int i=0; i<20; i++) { char name[3]; sprintf(name, "%d", i); create_file(&pins_dir, name, 0, 0, "????", -1); } uint8_t *boot_dir = create_directory(&io_dir, "boot", 0, io_cluster, 4); create_file(&boot_dir, bootfile+3, 0, 0, read_file(bootfile), -1); create_file(&io_dir, "status", 0, 0, "If you can see this phatio isn't working correctly.\n", 512); create_file(&io_dir, "run.lio", 0, 0, " ", -1); uint8_t *dev_dir = create_directory(&io_dir, "dev", 0, io_cluster, 4); uint8_t *etc_dir = create_directory(&io_dir, "etc", 0, io_cluster, 4); uint8_t *lib_dir = create_directory(&io_dir, "lib", 0, io_cluster, 4); #include "files.h" // create_file(&etc_dir, "config", 0, 0, read_file("../fs/io/etc/config"), -1); // create_file(&etc_dir, "pwm_pins", 0, 0, read_file("../fs/io/etc/pwm_pins"), -1); // create_file(&etc_dir, "adc_pins", 0, 0, read_file("../fs/io/etc/adc_pins"), -1); // create_file(&etc_dir, "digital_pins", 0, 0, read_file("../fs/io/etc/digital_pins"), -1); // create_file(&etc_dir, "license.txt", 0, 0, read_file("../fs/io/etc/license.txt"), -1); // create_file(&etc_dir, "lufa-license.txt", 0, 0, read_file("../fs/io/etc/lufa-license.txt"), -1); // stop os x creating sysfiles to drive // http://hostilefork.com/2009/12/02/trashes-fseventsd-and-spotlight-v100/ create_file(&root_dir, ".metadata_never_index", 0, 0x05, "Present to stop Mac Os X from indexing.\n", -1); create_file(&root_dir, ".Trashes", 0, 0x05, "Present to stop Mac Os X from create a Trash directory.\n", -1); uint8_t *event_dir = create_directory(&root_dir, ".fseventsd", 0, 0, 2); create_file(&event_dir, "no_log", 0, 0x05, "Present to stop Mac Os X from logging filesystem events.\n", -1); fprintf(stderr, "writing %d blocks \n", DATA_START+free_cluster); write(1, buffer, 512*(DATA_START+free_cluster+100)); }
bool FileWriter::writeToFile(int curWaveDirection) { ofstream myFileMotion; ofstream myFileVelocity; ofstream myFileAcceleration; string dirNum = boost::lexical_cast<string>(curWaveDirection); //cast int to string for comparison string currentDirectory = DIR_NAME + dirNum; //Create the current directory if (!create_directory(oFreq_Directory + currentDirectory)) { cerr << "Failed to create " + currentDirectory << endl; //This needs to be handled return false; } string filepath; filepath = currentDirectory + "/" + GLOBAL_MOTION_FILENAME; myFileMotion.open(filepath.c_str()); //Create the Motion file filepath = currentDirectory + "/" + GLOBAL_VELOCITY_FILENAME; myFileVelocity.open(filepath.c_str()); //Create the Velocity file filepath = currentDirectory + "/" + GLOBAL_ACCELERATION_FILENAME; myFileAcceleration.open(filepath.c_str()); //Create the Acceleration file setFileInfo(GLOBAL_ACCELERATION_OBJECT); myFileMotion << header << fileInfo << BREAK_TOP; setFileInfo(GLOBAL_VELOCITY_OBJECT); myFileVelocity << header << fileInfo << BREAK_TOP; setFileInfo(GLOBAL_ACCELERATION_OBJECT); myFileAcceleration << header << fileInfo << BREAK_TOP; for(unsigned int j = 0; j < theOutputsList.theBodyList.size(); j++) //iterate through all of the bodies { vector<cx_mat> globalMotSolutionList = theOutputsList.theOutputsBodyList[j].getOutputType(0); vector<cx_mat> globalVelSolutionList = theOutputsList.theOutputsBodyList[j].getOutputType(1); vector<cx_mat> globalAccSolutionList = theOutputsList.theOutputsBodyList[j].getOutputType(2); myFileMotion << BODY << " " << OBJECT_BEGIN2 << "\n " << NAME << " " << "\"" << theOutputsList.theBodyList[j].bodyName << "\"" << END << "\n"; myFileVelocity << BODY << " " << OBJECT_BEGIN2 << "\n " << NAME << " " << "\"" << theOutputsList.theBodyList[j].bodyName << "\"" << END << "\n"; myFileAcceleration << BODY << " " << OBJECT_BEGIN2 << "\n " << NAME << " " << "\"" << theOutputsList.theBodyList[j].bodyName << "\"" << END << "\n"; for(unsigned int k = 0; k < thefrequenciesList.size(); k++) //iterate through all of the frequencies { //Write Global Motion Files ******************************************************************************************* setFileInfo(GLOBAL_MOTION_OBJECT); myFileMotion << " " << DATA << " " << OBJECT_BEGIN2 << "\n" << " " << FREQUENCY << " " << (k+1) << "\n "<< VALUE << " " << LIST_BEGIN2 << "\n"; for(unsigned int a = 0; a < 6; a++) //print the 6 outputs per each frequency { myFileMotion.precision(15); myFileMotion << " " << globalMotSolutionList[k].at(a,0).real(); if(globalMotSolutionList[j].at(a,0).imag() < 0.0) myFileMotion << globalMotSolutionList[j].at(a,0).imag()<< "i\n"; else myFileMotion << "+" << globalMotSolutionList[j].at(a,0).imag()<< "i\n"; } myFileMotion << " " << LIST_END2 << "\n " << OBJECT_END2 << "\n"; //Write Global Velocity Files ***************************************************************************************** setFileInfo(GLOBAL_VELOCITY_OBJECT); myFileVelocity << " " << DATA << " " << OBJECT_BEGIN2 << "\n" << " " << FREQUENCY << " " << (k+1) << "\n "<< VALUE << " " << LIST_BEGIN2 << "\n"; for(unsigned int a = 0; a < 6; a++) //print the 6 outputs per each frequency { myFileVelocity.precision(15); myFileVelocity << " " << globalVelSolutionList[k].at(a,0).real(); if(globalVelSolutionList[j].at(a,0).imag() < 0.0) myFileVelocity << globalVelSolutionList[j].at(a,0).imag()<< "i\n"; else myFileVelocity << "+" << globalVelSolutionList[j].at(a,0).imag()<< "i\n"; } myFileVelocity << " " << LIST_END2 << "\n " << OBJECT_END2 << "\n"; //Write Global Acceleration Files ************************************************************************************* setFileInfo(GLOBAL_ACCELERATION_OBJECT); myFileAcceleration << " " << DATA << " " << OBJECT_BEGIN2 << "\n" << " " << FREQUENCY << " " << (k+1) << "\n "<< VALUE << " " << LIST_BEGIN2 << "\n"; for(unsigned int a = 0; a < 6; a++) //print the 6 outputs per each frequency { myFileAcceleration.precision(15); myFileAcceleration << " " << globalAccSolutionList[k].at(a,0).real(); if(globalAccSolutionList[j].at(a,0).imag() < 0.0) myFileAcceleration << globalAccSolutionList[j].at(a,0).imag()<< "i\n"; else myFileAcceleration << "+" << globalAccSolutionList[j].at(a,0).imag()<< "i\n"; } myFileAcceleration << " " << LIST_END2 << "\n " << OBJECT_END2 << "\n"; } myFileMotion << OBJECT_END2 << "\n"; myFileVelocity << OBJECT_END2 << "\n"; myFileAcceleration << OBJECT_END2 << "\n"; } myFileMotion << BREAK_BOTTOM; myFileVelocity << BREAK_BOTTOM; myFileAcceleration << BREAK_BOTTOM; myFileMotion.close(); myFileVelocity.close(); myFileAcceleration.close(); return true; }
//creates the full path of fullpath, return true on success bool DirectoryOp::create_full_path(const char* fullpath, bool with_file) { if (fullpath == NULL) return false; int32_t iLen = strlen(fullpath); if (iLen > MAX_PATH_LENGTH || iLen <= 0x01) return false; // a little waste char dirpath[MAX_PATH_LENGTH]; strncpy(dirpath, fullpath, iLen); dirpath[iLen] = '\0'; char* path = dirpath; if (with_file) { path = dirname(dirpath); } bool ret = true; struct stat stats; if ((lstat(path, &stats) != 0) || !S_ISDIR(stats.st_mode)) { if (ret) { while (*path == '/') path++; } while (ret) { path = strchr(path, '/'); if (path == NULL) { break; } *path = '\0'; if (!is_directory(dirpath)) { if (!create_directory(dirpath)) { ret = false; break; } } *path++ = '/'; while (*path == '/') { path++; } } if (ret) { if (!is_directory(dirpath)) { if (!create_directory(dirpath)) ret = false; } } } return ret; }
void Reprojector::load(IPC& ipc, bool flipped) { bool serial_first_non_zero = false; string serial = ""; for (int i = 4; i < 10; ++i) { string str_temp = ""; str_temp += serial_number[i]; if (!serial_first_non_zero && str_temp != "0") serial_first_non_zero = true; if (serial_first_non_zero) serial += serial_number[i]; } bool has_complete_calib_data = false; if (directory_exists(data_path_current_module)) if (file_exists(data_path_current_module + "\\0.jpg")) if (file_exists(data_path_current_module + "\\1.jpg")) if (file_exists(data_path_current_module + "\\stereoCalibData.txt")) if (file_exists(data_path_current_module + "\\rect0.txt")) if (file_exists(data_path_current_module + "\\rect1.txt")) has_complete_calib_data = true; if (!has_complete_calib_data) { static bool block_thread = true; ipc.send_message("menu_plus", "show window", ""); ipc.get_response("menu_plus", "show download", "", [](const string message_body) { COUT << "unblock" << endl; block_thread = false; }); while (block_thread) { ipc.update(); Sleep(100); } create_directory(data_path); create_directory(data_path_current_module); copy_file(executable_path + "\\rectifier.exe", data_path_current_module + "\\rectifier.exe"); copy_file(executable_path + "\\opencv_core249.dll", data_path_current_module + "\\opencv_core249.dll"); copy_file(executable_path + "\\opencv_highgui249.dll", data_path_current_module + "\\opencv_highgui249.dll"); copy_file(executable_path + "\\opencv_imgproc249.dll", data_path_current_module + "\\opencv_imgproc249.dll"); copy_file(executable_path + "\\opencv_calib3d249.dll", data_path_current_module + "\\opencv_calib3d249.dll"); copy_file(executable_path + "\\opencv_flann249.dll", data_path_current_module + "\\opencv_flann249.dll"); copy_file(executable_path + "\\opencv_features2d249.dll", data_path_current_module + "\\opencv_features2d249.dll"); //http://s3-us-west-2.amazonaws.com/ractiv.com/data/ //http://d2i9bzz66ghms6.cloudfront.net/data/ string param0 = "http://s3-us-west-2.amazonaws.com/ractiv.com/data/" + serial + "/0.jpg"; string param1 = data_path_current_module + "\\0.jpg"; string* serial_ptr = &serial; IPC* ipc_ptr = &ipc; ipc.get_response("menu_plus", "download", param0 + "`" + param1, [serial_ptr, ipc_ptr](const string message_body) { if (message_body == "false") ipc_ptr->send_message("daemon_plus", "exit", ""); else { string param0 = "http://s3-us-west-2.amazonaws.com/ractiv.com/data/" + *serial_ptr + "/1.jpg"; string param1 = data_path_current_module + "\\1.jpg"; ipc_ptr->get_response("menu_plus", "download", param0 + "`" + param1, [serial_ptr, ipc_ptr](const string message_body) { if (message_body == "false") ipc_ptr->send_message("daemon_plus", "exit", ""); else { string param0 = "http://s3-us-west-2.amazonaws.com/ractiv.com/data/" + *serial_ptr + "/stereoCalibData.txt"; string param1 = data_path_current_module + "\\stereoCalibData.txt"; ipc_ptr->get_response("menu_plus", "download", param0 + "`" + param1, [serial_ptr, ipc_ptr](const string message_body) { if (message_body == "false") ipc_ptr->send_message("daemon_plus", "exit", ""); else { bool has_complete_calib_data = false; while (!has_complete_calib_data) { system(("cd " + cmd_quote + data_path_current_module + cmd_quote + " && rectifier.exe").c_str()); if (directory_exists(data_path_current_module)) if (file_exists(data_path_current_module + "\\0.jpg")) if (file_exists(data_path_current_module + "\\1.jpg")) if (file_exists(data_path_current_module + "\\stereoCalibData.txt")) if (file_exists(data_path_current_module + "\\rect0.txt")) if (file_exists(data_path_current_module + "\\rect1.txt")) has_complete_calib_data = true; } block_thread = false; } }); } }); } }); block_thread = true; while (block_thread) { ipc.update(); Sleep(100); } } ifstream file_stereo_calib_data(data_path_current_module + "\\stereoCalibData.txt"); bool is_number_new = false; bool is_number_old = false; int block_count = 0; int block[4]; vector<Point> disparity_data; string str_num_temp = ""; string disparities_string = ""; while (getline(file_stereo_calib_data, disparities_string)) { const int i_max = disparities_string.length(); for (int i = 0; i < i_max; ++i) { string str_temp = ""; str_temp += disparities_string[i]; if (str_temp != "," && str_temp != ";") is_number_new = true; else is_number_new = false; if (is_number_new) { if (!is_number_old) str_num_temp = str_temp; else str_num_temp += str_temp; } else if (is_number_old) { block[block_count] = stoi(str_num_temp); ++block_count; } if (block_count == 3) { bool found = false; for (int a = 0; a < disparity_data.size(); ++a) { if (disparity_data[a].x == block[0]) { found = true; disparity_data[a].y = (disparity_data[a].y + abs(block[1] - block[2])) / 2; } else if (disparity_data[a].y == abs(block[1] - block[2])) { found = true; disparity_data[a].x = min(disparity_data[a].x, block[0]); } } if (!found) disparity_data.push_back(Point(block[0], abs(block[1] - block[2]))); block_count = 0; } is_number_old = is_number_new; } } sort(disparity_data.begin(), disparity_data.end(), compare_point_x()); double *t, *y; t = new double[disparity_data.size()]; y = new double[disparity_data.size()]; for (unsigned int a = 0; a < disparity_data.size(); a++) { t[a] = (double)(disparity_data[a].y); y[a] = (double)(disparity_data[a].x); } CCurveFitting cf; cf.curve_fitting4(t, disparity_data.size(), y, &a_out, &b_out, &c_out, &d_out); delete []t; delete []y; ifstream file0(data_path_current_module + "\\rect0.txt"); is_number_new = false; is_number_old = false; block_count = 0; rect_mat0 = new Point*[WIDTH_LARGE]; for (int i = 0; i < WIDTH_LARGE; ++i) rect_mat0[i] = new Point[HEIGHT_LARGE]; string rect0_string = ""; while (getline(file0, rect0_string)) { const int i_max = rect0_string.length(); for (int i = 0; i < i_max; ++i) { string str_temp = ""; str_temp += rect0_string[i]; if (str_temp != " " && str_temp != "," && str_temp != ";") is_number_new = true; else is_number_new = false; if (is_number_new) { if (!is_number_old) str_num_temp = str_temp; else str_num_temp += str_temp; } else if (is_number_old) { block[block_count] = stoi(str_num_temp); ++block_count; } if (block_count == 4) { if (!flipped) rect_mat0[block[0]][block[1]] = Point(block[2], block[3]); else rect_mat0[block[0]][HEIGHT_LARGE_MINUS - block[1]] = Point(block[2], block[3]); block_count = 0; } is_number_old = is_number_new; } } ifstream file1(data_path_current_module + "\\rect1.txt"); is_number_new = false; is_number_old = false; block_count = 0; rect_mat1 = new Point*[WIDTH_LARGE]; for (int i = 0; i < WIDTH_LARGE; ++i) rect_mat1[i] = new Point[HEIGHT_LARGE]; string rect1_string = ""; while (getline(file1, rect1_string)) { const int i_max = rect1_string.length(); for (int i = 0; i < i_max; ++i) { string str_temp = ""; str_temp += rect1_string[i]; if (str_temp != " " && str_temp != "," && str_temp != ";") is_number_new = true; else is_number_new = false; if (is_number_new) { if (!is_number_old) str_num_temp = str_temp; else str_num_temp += str_temp; } else if (is_number_old) { block[block_count] = stoi(str_num_temp); ++block_count; } if (block_count == 4) { if (!flipped) rect_mat1[block[0]][block[1]] = Point(block[2], block[3]); else rect_mat1[block[0]][HEIGHT_LARGE_MINUS - block[1]] = Point(block[2], block[3]); block_count = 0; } is_number_old = is_number_new; } } }
void mess_up_inline_inode(ocfs2_filesys *fs, enum fsck_type type, uint64_t blkno) { int i; errcode_t ret; char *buf = NULL, file_type[20]; uint64_t inline_blkno; struct ocfs2_dinode *di; uint16_t max_inline_sz; struct ocfs2_super_block *osb; osb = OCFS2_RAW_SB(fs->fs_super); if (!ocfs2_support_inline_data(osb)) FSWRK_FATAL("Should specify a inline-data supported " "volume to do this corruption\n"); ret = ocfs2_malloc_block(fs->fs_io, &buf); if (ret) FSWRK_COM_FATAL(progname, ret); for (i = 0; i < 2; i++) { if (i == 0) { create_file(fs, blkno, &inline_blkno); snprintf(file_type, 20, "%s", "Regular file"); } else { create_directory(fs, blkno, &inline_blkno); snprintf(file_type, 20, "%s", "Diectroy"); } ret = ocfs2_read_inode(fs, inline_blkno, buf); if (ret) FSWRK_COM_FATAL(progname, ret); di = (struct ocfs2_dinode *)buf; max_inline_sz = ocfs2_max_inline_data_with_xattr(fs->fs_blocksize, di); if (!(di->i_dyn_features & OCFS2_INLINE_DATA_FL)) di->i_dyn_features |= OCFS2_INLINE_DATA_FL; switch (type) { case INLINE_DATA_COUNT_INVALID: di->id2.i_data.id_count = 0; fprintf(stdout, "INLINE_DATA_COUNT_INVALID: " "Create an inlined inode#%"PRIu64"(%s)," "whose id_count has been messed up.\n", inline_blkno, file_type); break; case INODE_INLINE_SIZE: di->i_size = max_inline_sz + 1; fprintf(stdout, "INODE_INLINE_SIZE: " "Create an inlined inode#%"PRIu64"(%s)," "whose i_size has been messed up.\n", inline_blkno, file_type); break; case INODE_INLINE_CLUSTERS: di->i_clusters = 1; fprintf(stdout, "INODE_INLINE_CLUSTERS: " "Create an inlined inode#%"PRIu64"(%s)," "whose i_clusters has been messed up.\n", inline_blkno, file_type); break; default: FSWRK_FATAL("Invalid type[%d]\n", type); } ret = ocfs2_write_inode(fs, inline_blkno, buf); if (ret) FSWRK_COM_FATAL(progname, ret); } if (buf) ocfs2_free(&buf); return; }
Configuration::Configuration(const std::string& directory, const std::string& filename) throw (ConfigurationException) { if (!filename.length()) { throw ConfigurationException("No valid configuration filename."); } std::string base_dir = get_home_directory(); create_directory(directory, base_dir); this->filename = base_dir + "/" + directory + "/" + filename; /* load file */ if (file_exists(this->filename)) { settings.read(this->filename); } /* load defaults */ cdef("player_name", generate_name()); cdef("show_player_name", "1"); cdef("player_skin", "goat"); cdef("fullscreen", "0"); cdef("show_scanlines", "1"); cdef("scanlines_intensity", "50"); cdef("music_volume", "128"); cdef("sfx_volume", "128"); cdef("master_server", "master.goatattack.net"); cdef("master_port", "25113"); cdef("server_name", "frederic's temple"); cdef("server_port", "25111"); cdef("game_mode", "0"); cdef("max_players", "12"); cdef("duration", "20"); cdef("warmup", "0"); cdef("bind_escape_device", "0"); cdef("bind_escape_param", "27"); cdef("bind_chat_device", "0"); cdef("bind_chat_param", "116"); cdef("bind_down_device", "0"); cdef("bind_down_param", "1073741905"); cdef("bind_drop1_device", "0"); cdef("bind_drop1_param", "115"); cdef("bind_drop2_device", "0"); cdef("bind_drop2_param", "100"); cdef("bind_drop3_device", "0"); cdef("bind_drop3_param", "102"); cdef("bind_fire_device", "0"); cdef("bind_fire_param", "97"); cdef("bind_jump_device", "0"); cdef("bind_jump_param", "32"); cdef("bind_left_device", "0"); cdef("bind_left_param", "1073741904"); cdef("bind_right_device", "0"); cdef("bind_right_param", "1073741903"); cdef("bind_stats_device", "0"); cdef("bind_stats_param", "9"); cdef("bind_up_device", "0"); cdef("bind_up_param", "1073741906"); cdef("deadzone_horizontal", "3200"); cdef("deadzone_vertical", "3200"); /* extract -> ready for quick access */ extract(); }
KeymapWindow::KeymapWindow() : BWindow(BRect(80, 50, 880, 380), B_TRANSLATE("Keymap"), B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS | B_AUTO_UPDATE_SIZE_LIMITS) { SetLayout(new BGroupLayout(B_VERTICAL)); fKeyboardLayoutView = new KeyboardLayoutView("layout"); fKeyboardLayoutView->SetKeymap(&fCurrentMap); fTextControl = new BTextControl(B_TRANSLATE("Sample and clipboard:"), "", NULL); fSwitchShortcutsButton = new BButton("switch", "", new BMessage(kMsgSwitchShortcuts)); fRevertButton = new BButton("revertButton", B_TRANSLATE("Revert"), new BMessage(kMsgRevertKeymap)); // controls pane AddChild(BGroupLayoutBuilder(B_VERTICAL) .Add(_CreateMenu()) .Add(BGroupLayoutBuilder(B_HORIZONTAL, 10) .Add(_CreateMapLists(), 0.25) .Add(BGroupLayoutBuilder(B_VERTICAL, 10) .Add(fKeyboardLayoutView) //.Add(new BStringView("text label", "Sample and clipboard:")) .Add(BGroupLayoutBuilder(B_HORIZONTAL, 10) .Add(_CreateDeadKeyMenuField(), 0.0) .AddGlue() .Add(fSwitchShortcutsButton)) .Add(fTextControl) .AddGlue(0.0) .Add(BGroupLayoutBuilder(B_HORIZONTAL, 10) .AddGlue(0.0) .Add(fRevertButton))) .SetInsets(10, 10, 10, 10))); fKeyboardLayoutView->SetTarget(fTextControl->TextView()); fTextControl->MakeFocus(); // Make sure the user keymap directory exists BPath path; find_directory(B_USER_SETTINGS_DIRECTORY, &path); path.Append("Keymap"); entry_ref ref; get_ref_for_path(path.Path(), &ref); BDirectory userKeymapsDir(&ref); if (userKeymapsDir.InitCheck() != B_OK) create_directory(path.Path(), S_IRWXU | S_IRWXG | S_IRWXO); BMessenger messenger(this); fOpenPanel = new BFilePanel(B_OPEN_PANEL, &messenger, &ref, B_FILE_NODE, false, NULL); fSavePanel = new BFilePanel(B_SAVE_PANEL, &messenger, &ref, B_FILE_NODE, false, NULL); BRect windowFrame; BString keyboardLayout; _LoadSettings(windowFrame, keyboardLayout); _SetKeyboardLayout(keyboardLayout.String()); ResizeTo(windowFrame.Width(), windowFrame.Height()); MoveTo(windowFrame.LeftTop()); // TODO: this might be a bug in the interface kit, but scrolling to // selection does not correctly work unless the window is shown. Show(); Lock(); // Try and find the current map name in the two list views (if the name // was read at all) _SelectCurrentMap(); KeymapListItem* current = static_cast<KeymapListItem*>(fUserListView->FirstItem()); fCurrentMap.Load(current->EntryRef()); fPreviousMap = fCurrentMap; fAppliedMap = fCurrentMap; fCurrentMap.SetTarget(this, new BMessage(kMsgKeymapUpdated)); _UpdateButtons(); _UpdateDeadKeyMenu(); _UpdateSwitchShortcutButton(); Unlock(); }
int run_stats(shared_options_data_t *shared_options_data, stats_options_data_t *options_data) { file_stats_t *file_stats = file_stats_new(); sample_stats_t **sample_stats; // List that stores the batches of records filtered by each thread list_t *output_list[shared_options_data->num_threads]; // List that stores which thread filtered the next batch to save list_t *next_token_list = malloc(sizeof(list_t)); int ret_code; double start, stop, total; vcf_file_t *vcf_file = vcf_open(shared_options_data->vcf_filename, shared_options_data->max_batches); if (!vcf_file) { LOG_FATAL("VCF file does not exist!\n"); } ped_file_t *ped_file = NULL; if (shared_options_data->ped_filename) { ped_file = ped_open(shared_options_data->ped_filename); if (!ped_file) { LOG_FATAL("PED file does not exist!\n"); } if(options_data->variable) { set_variable_field(options_data->variable, 0, ped_file); } else { set_variable_field("PHENO", 6, ped_file); } if(options_data->variable_groups) { int n, m; char *variable_groups = strdup(options_data->variable_groups); char **groups; char **phenos_in_group; groups = split(variable_groups, ":", &n); for(int i = 0; i < n; i++){ phenos_in_group = split(groups[i], ",", &m); if(set_phenotype_group(phenos_in_group, m, ped_file) < 0) { LOG_ERROR("Variable can't appear in two groups\n"); return DUPLICATED_VARIABLE; } free(phenos_in_group); } ped_file->accept_new_values = 0; free(variable_groups); free(groups); } else { ped_file->accept_new_values = 1; } if(options_data->phenotype) { int n; char* phenotypes = strdup(options_data->phenotype); char** pheno_values = split(phenotypes, ",", &n); if(n != 2) { LOG_ERROR("To handle case-control test, only two phenotypes are supported\n"); return MORE_THAN_TWO_PHENOTYPES; } else { set_unaffected_phenotype(pheno_values[0],ped_file); set_affected_phenotype(pheno_values[1],ped_file); } } else { set_unaffected_phenotype("1", ped_file); set_affected_phenotype("2", ped_file); } LOG_INFO("About to read PED file...\n"); // Read PED file before doing any processing ret_code = ped_read(ped_file); if (ret_code != 0) { LOG_FATAL_F("Can't read PED file: %s\n", ped_file->filename); } if(!ped_file->num_field) { LOG_ERROR_F("Can't find the specified field \"%s\" in file: %s \n", options_data->variable, ped_file->filename); return VARIABLE_FIELD_NOT_FOUND; } } ret_code = create_directory(shared_options_data->output_directory); if (ret_code != 0 && errno != EEXIST) { LOG_FATAL_F("Can't create output directory: %s\n", shared_options_data->output_directory); } // Initialize variables related to the different threads for (int i = 0; i < shared_options_data->num_threads; i++) { output_list[i] = (list_t*) malloc(sizeof(list_t)); list_init("input", 1, shared_options_data->num_threads * shared_options_data->batch_lines, output_list[i]); } list_init("next_token", shared_options_data->num_threads, INT_MAX, next_token_list); LOG_INFO("About to retrieve statistics from VCF file...\n"); #pragma omp parallel sections private(start, stop, total) { #pragma omp section { LOG_DEBUG_F("Thread %d reads the VCF file\n", omp_get_thread_num()); // Reading start = omp_get_wtime(); if (shared_options_data->batch_bytes > 0) { ret_code = vcf_parse_batches_in_bytes(shared_options_data->batch_bytes, vcf_file); } else if (shared_options_data->batch_lines > 0) { ret_code = vcf_parse_batches(shared_options_data->batch_lines, vcf_file); } stop = omp_get_wtime(); total = stop - start; if (ret_code) { LOG_FATAL_F("[%dR] Error code = %d\n", omp_get_thread_num(), ret_code); } LOG_INFO_F("[%dR] Time elapsed = %f s\n", omp_get_thread_num(), total); LOG_INFO_F("[%dR] Time elapsed = %e ms\n", omp_get_thread_num(), total*1000); notify_end_parsing(vcf_file); } #pragma omp section { // Enable nested parallelism and set the number of threads the user has chosen omp_set_nested(1); LOG_DEBUG_F("Thread %d processes data\n", omp_get_thread_num()); individual_t **individuals = NULL; khash_t(ids) *sample_ids = NULL; khash_t(str) *phenotype_ids = NULL; int num_phenotypes; start = omp_get_wtime(); int i = 0; vcf_batch_t *batch = NULL; while ((batch = fetch_vcf_batch(vcf_file)) != NULL) { if (i == 0) { sample_stats = malloc (get_num_vcf_samples(vcf_file) * sizeof(sample_stats_t*)); for (int j = 0; j < get_num_vcf_samples(vcf_file); j++) { sample_stats[j] = sample_stats_new(array_list_get(j, vcf_file->samples_names)); } if (ped_file) { // Create map to associate the position of individuals in the list of samples defined in the VCF file sample_ids = associate_samples_and_positions(vcf_file); // Sort individuals in PED as defined in the VCF file individuals = sort_individuals(vcf_file, ped_file); // Get the khash of the phenotypes in PED file phenotype_ids = get_phenotypes(ped_file); num_phenotypes = get_num_variables(ped_file); } } if (i % 50 == 0) { LOG_INFO_F("Batch %d reached by thread %d - %zu/%zu records \n", i, omp_get_thread_num(), batch->records->size, batch->records->capacity); } // Divide the list of passed records in ranges of size defined in config file int num_chunks; int *chunk_sizes = NULL; array_list_t *input_records = batch->records; int *chunk_starts = create_chunks(input_records->size, ceil((float) shared_options_data->batch_lines / shared_options_data->num_threads), &num_chunks, &chunk_sizes); // OpenMP: Launch a thread for each range #pragma omp parallel for num_threads(shared_options_data->num_threads) for (int j = 0; j < num_chunks; j++) { LOG_DEBUG_F("[%d] Stats invocation\n", omp_get_thread_num()); // Invoke variant stats and/or sample stats when applies if (options_data->variant_stats) { int index = omp_get_thread_num() % shared_options_data->num_threads; ret_code = get_variants_stats((vcf_record_t**) (input_records->items + chunk_starts[j]), chunk_sizes[j], individuals, sample_ids,num_phenotypes, output_list[index], file_stats); } if (options_data->sample_stats) { ret_code |= get_sample_stats((vcf_record_t**) (input_records->items + chunk_starts[j]), chunk_sizes[j], individuals, sample_ids, sample_stats, file_stats); } } if (options_data->variant_stats) { // Insert as many tokens as elements correspond to each thread for (int t = 0; t < num_chunks; t++) { for (int s = 0; s < chunk_sizes[t]; s++) { list_item_t *token_item = list_item_new(t, 0, NULL); list_insert_item(token_item, next_token_list); } } } free(chunk_starts); free(chunk_sizes); vcf_batch_free(batch); i++; } stop = omp_get_wtime(); total = stop - start; LOG_INFO_F("[%d] Time elapsed = %f s\n", omp_get_thread_num(), total); LOG_INFO_F("[%d] Time elapsed = %e ms\n", omp_get_thread_num(), total*1000); // Decrease list writers count for (i = 0; i < shared_options_data->num_threads; i++) { list_decr_writers(next_token_list); list_decr_writers(output_list[i]); } if (sample_ids) { kh_destroy(ids, sample_ids); } if (individuals) { free(individuals); } } #pragma omp section { LOG_DEBUG_F("Thread %d writes the output\n", omp_get_thread_num()); char *stats_prefix = get_vcf_stats_filename_prefix(shared_options_data->vcf_filename, shared_options_data->output_filename, shared_options_data->output_directory); // File names and descriptors for output to plain text files char *stats_filename, *summary_filename, *phenotype_filename; FILE *stats_fd, *summary_fd, **phenotype_fd; char *stats_db_name; sqlite3 *db = NULL; khash_t(stats_chunks) *hash; khash_t(str) *phenotype_ids; int num_phenotypes; if(ped_file){ phenotype_ids = get_phenotypes(ped_file); num_phenotypes = get_num_variables(ped_file); } if (options_data->save_db) { delete_files_by_extension(shared_options_data->output_directory, "db"); stats_db_name = calloc(strlen(stats_prefix) + strlen(".db") + 2, sizeof(char)); sprintf(stats_db_name, "%s.db", stats_prefix); create_stats_db(stats_db_name, VCF_CHUNKSIZE, create_vcf_query_fields, &db); hash = kh_init(stats_chunks); } // Write variant (and global) statistics if (options_data->variant_stats) { stats_filename = get_variant_stats_output_filename(stats_prefix); if (!(stats_fd = fopen(stats_filename, "w"))) { LOG_FATAL_F("Can't open file for writing statistics of variants: %s\n", stats_filename); } //Open one file for each phenotype if(ped_file){ phenotype_fd = malloc(sizeof(FILE*)*num_phenotypes); if(options_data->variable_groups){ int n; char *variable_groups = strdup(options_data->variable_groups); char ** names = split(variable_groups, ":", &n); for(int i = 0; i < n; i++) { phenotype_filename = get_variant_phenotype_stats_output_filename(stats_prefix, names[i]); if(!(phenotype_fd[i] = fopen(phenotype_filename, "w"))) { LOG_FATAL_F("Can't open file for writing statistics of variants per phenotype: %s\n", stats_filename); } free(phenotype_filename); } free(names); free(variable_groups); } else { for (khint_t i = kh_begin(phenotype_ids); i != kh_end(phenotype_ids); ++i) { if (!kh_exist(phenotype_ids,i)) continue; phenotype_filename = get_variant_phenotype_stats_output_filename(stats_prefix, kh_key(phenotype_ids,i)); if(!(phenotype_fd[kh_val(phenotype_ids,i)] = fopen(phenotype_filename, "w"))) { LOG_FATAL_F("Can't open file for writing statistics of variants per phenotype: %s\n", stats_filename); } free(phenotype_filename); } } } // Write header report_vcf_variant_stats_header(stats_fd); if(ped_file){ for(int i = 0; i < num_phenotypes; i++) report_vcf_variant_phenotype_stats_header(phenotype_fd[i]); } // For each variant, generate a new line int avail_stats = 0; variant_stats_t *var_stats_batch[VCF_CHUNKSIZE]; list_item_t *token_item = NULL, *output_item = NULL; while ( token_item = list_remove_item(next_token_list) ) { output_item = list_remove_item(output_list[token_item->id]); assert(output_item); var_stats_batch[avail_stats] = output_item->data_p; avail_stats++; // Run only when certain amount of stats is available if (avail_stats >= VCF_CHUNKSIZE) { report_vcf_variant_stats(stats_fd, db, hash, avail_stats, var_stats_batch); if(ped_file) for(int i = 0; i < num_phenotypes; i++) report_vcf_variant_phenotype_stats(phenotype_fd[i], avail_stats, var_stats_batch, i); // Free all stats from the "batch" for (int i = 0; i < avail_stats; i++) { variant_stats_free(var_stats_batch[i]); } avail_stats = 0; } // Free resources list_item_free(output_item); list_item_free(token_item); } if (avail_stats > 0) { report_vcf_variant_stats(stats_fd, db, hash, avail_stats, var_stats_batch); if(ped_file) for(int i = 0; i < num_phenotypes; i++) report_vcf_variant_phenotype_stats(phenotype_fd[i], avail_stats, var_stats_batch, i); // Free all stats from the "batch" for (int i = 0; i < avail_stats; i++) { variant_stats_free(var_stats_batch[i]); } avail_stats = 0; } // Write whole file stats (data only got when launching variant stats) summary_filename = get_vcf_file_stats_output_filename(stats_prefix); if (!(summary_fd = fopen(summary_filename, "w"))) { LOG_FATAL_F("Can't open file for writing statistics summary: %s\n", summary_filename); } report_vcf_summary_stats(summary_fd, db, file_stats); free(stats_filename); free(summary_filename); // Close variant stats file if (stats_fd) { fclose(stats_fd); } if (summary_fd) { fclose(summary_fd); } if(ped_file){ for(int i = 0; i < num_phenotypes; i++) if(phenotype_fd[i]) fclose(phenotype_fd[i]); free(phenotype_fd); } } // Write sample statistics if (options_data->sample_stats) { stats_filename = get_sample_stats_output_filename(stats_prefix); if (!(stats_fd = fopen(stats_filename, "w"))) { LOG_FATAL_F("Can't open file for writing statistics of samples: %s\n", stats_filename); } report_vcf_sample_stats_header(stats_fd); report_vcf_sample_stats(stats_fd, NULL, vcf_file->samples_names->size, sample_stats); // Close sample stats file free(stats_filename); if (stats_fd) { fclose(stats_fd); } } free(stats_prefix); if (db) { insert_chunk_hash(VCF_CHUNKSIZE, hash, db); create_stats_index(create_vcf_index, db); close_stats_db(db, hash); } } } for (int i = 0; i < get_num_vcf_samples(vcf_file); i++) { sample_stats_free(sample_stats[i]); } free(sample_stats); free(file_stats); free(next_token_list); for (int i = 0; i < shared_options_data->num_threads; i++) { free(output_list[i]); } vcf_close(vcf_file); if (ped_file) { ped_close(ped_file, 1,1); } return 0; }
int main(int argc, const char ** argv) { std::string path = "./protocol"; if (argc > 1) path = argv[1]; fs::path protocol; std::istringstream ss { path }; ss >> protocol; if (!fs::is_directory(protocol)) throw std::runtime_error { path + " is not a directory or doesn't exist" }; auto local = fs::current_path() / "cpp" ; if (fs::exists(local) && fs::is_directory(local)) fs::remove_all(local); fs::create_directory(local); fs::create_directory(local / "enums"); fs::create_directory(local / "messages"); fs::create_directory(local / "types"); fs::create_directory(local / "datacenter"); /* enums */ std::cout << "Translating enums..." << std::endl; for (fs::recursive_directory_iterator it { protocol / "enums" }; it != fs::recursive_directory_iterator { }; ++it) { if (!fs::is_regular_file(it->path()) || it->path().extension() != ".as") { if (fs::is_directory(it->path())) create_directory(compute(local, protocol, it->path())); continue; } enum_file f { std::ifstream { it->path().string() } }; f.parse(); make_cpp_file(f.cpp_output(), compute(local, protocol, it->path().parent_path()) / (to_cpp_case(f.class_name()) + ".hpp")); } /* messages / types */ std::cout << "Translating messages and types..." << std::endl; std::unordered_map<std::string, network_file> network; std::unordered_map<std::string, fs::path> paths; retrieve(network, paths, local, protocol, "messages"); retrieve(network, paths, local, protocol, "types"); for (auto && it : paths) { auto && f = network.at(it.first); make_cpp_file(f.cpp_output(network), it.second); } /* datacenter */ std::cout << "Translating datacenter..." << std::endl; std::unordered_map<std::string, datacenter_file> datacenter; paths.clear(); retrieve(datacenter, paths, local, protocol, "datacenter"); for (auto && it : paths) { auto && f = datacenter.at(it.first); make_cpp_file(f.cpp_output(datacenter), it.second); } return 0; }
EXPORT void geomview_amr_fronts_plot2d( const char *dname, Front **frs, int num_patches, Wv_on_pc **redistr_table, int max_n_patch) { FILE *fp; int i; char fmt[256]; static const char *indent = " "; static char *fname = NULL, *ppfname = NULL; static size_t fname_len = 0, ppfname_len = 0; INTERFACE *intfc = frs[0]->interf; INTERFACE *tmpintfc; CURVE **c; INTERFACE *sav_intfc; bool sav_copy; float **clrmap = NULL; float ccolor[4] = {0.0, 0.0, 0.0, 1.0}; int myid, numnodes; const char *nstep; char outname[256],outdir[256]; myid = pp_mynode(); numnodes = pp_numnodes(); sprintf(outdir,"%s/%s",dname,"geomintfc"); ppfname = set_ppfname(ppfname,"intfc",&ppfname_len); nstep = right_flush(frs[0]->step,7); sprintf(outname,"%s.ts%s",ppfname,nstep); if (create_directory(dname,YES) == FUNCTION_FAILED) { (void) printf("WARNING in geomview_intfc_plot2d(), directory " "%s doesn't exist and can't be created\n",dname); return; } if (create_directory(outdir,YES) == FUNCTION_FAILED) { (void) printf("WARNING in geomview_intfc_plot2d(), directory " "%s doesn't exist and can't be created\n",outdir); return; } sav_intfc = current_interface(); sav_copy = copy_intfc_states(); set_size_of_intfc_state(size_of_state(intfc)); set_copy_intfc_states(YES); tmpintfc = copy_interface(intfc); /* clip_to_interior_region(tmpintfc, frs[0]->rect_grid->lbuf,frs[0]->rect_grid->ubuf); */ uni_array(&clrmap,6,sizeof(float*)); for(i = 0; i < 6; i++) uni_array(&clrmap[i],4,sizeof(float)); i = 0; clrmap[i][0] = 0.098; clrmap[i][1] = 0.647; clrmap[i][2] = 0.400; clrmap[i][3] = 1.000; i++; clrmap[i][0] = 0.898; clrmap[i][1] = 0.400; clrmap[i][2] = 0.000; clrmap[i][3] = 1.000; i++; clrmap[i][0] = 0.500; clrmap[i][1] = 1.000; clrmap[i][2] = 0.500; clrmap[i][3] = 1.000; i++; clrmap[i][0] = 1.000; clrmap[i][1] = 0.000; clrmap[i][2] = 1.000; clrmap[i][3] = 1.000; i++; clrmap[i][0] = 0.000; clrmap[i][1] = 0.800; clrmap[i][2] = 1.000; clrmap[i][3] = 1.000; i++; clrmap[i][0] = 0.250; clrmap[i][1] = 0.808; clrmap[i][2] = 0.098; clrmap[i][3] = 1.000; i++; fname = get_list_file_name(fname,outdir,outname,&fname_len); if ((fp = fopen(fname,"w")) == NULL) { (void) printf("WARNING in gview_plot_intfc2d(), " "can't open %s\n",fname); delete_interface(tmpintfc); set_current_interface(sav_intfc); set_copy_intfc_states(sav_copy); return; } fprintf(fp,"{ LIST \n"); /* beginning of writting Vect to file */ for(c = tmpintfc->curves; c and *c; c++) gview_plot_curve2d(fp,*c,ccolor); for(int i = 0; i < num_patches; i++) { int use_clr; /* gview_plot_box2d(fp, frs[i]->rect_grid->L, frs[i]->rect_grid->U,clrmap[i%3]); */ if(NULL != redistr_table) use_clr = redistr_table[myid][i].pc_id % 6; else use_clr = 1; gview_plot_grid2d(fp,frs[i]->rect_grid,clrmap[use_clr]); } /* end of LIST OBJ */ fprintf(fp,"}\n"); fclose(fp); set_current_interface(sav_intfc); set_copy_intfc_states(sav_copy); delete_interface(tmpintfc); for(int i = 0; i < 6; i++) free(clrmap[i]); free(clrmap); }
int run_merge(shared_options_data_t *shared_options_data, merge_options_data_t *options_data) { if (options_data->num_files == 1) { LOG_INFO("Just one VCF file specified, no need to merge"); return 0; } list_t *read_list[options_data->num_files]; memset(read_list, 0, options_data->num_files * sizeof(list_t*)); list_t *output_header_list = (list_t*) malloc (sizeof(list_t)); list_init("headers", shared_options_data->num_threads, INT_MAX, output_header_list); list_t *output_list = (list_t*) malloc (sizeof(list_t)); list_init("output", shared_options_data->num_threads, shared_options_data->max_batches * shared_options_data->batch_lines, output_list); list_t *merge_tokens = (list_t*) malloc (sizeof(list_t)); list_init("tokens", 1, INT_MAX, merge_tokens); int ret_code = 0; double start, stop, total; vcf_file_t *files[options_data->num_files]; memset(files, 0, options_data->num_files * sizeof(vcf_file_t*)); // Initialize variables related to the different files for (int i = 0; i < options_data->num_files; i++) { files[i] = vcf_open(options_data->input_files[i], shared_options_data->max_batches); if (!files[i]) { LOG_FATAL_F("VCF file %s does not exist!\n", options_data->input_files[i]); } read_list[i] = (list_t*) malloc(sizeof(list_t)); list_init("text", 1, shared_options_data->max_batches, read_list[i]); } ret_code = create_directory(shared_options_data->output_directory); if (ret_code != 0 && errno != EEXIST) { LOG_FATAL_F("Can't create output directory: %s\n", shared_options_data->output_directory); } chromosome_order = get_chromosome_order(shared_options_data->host_url, shared_options_data->species, shared_options_data->version, &num_chromosomes); printf("Number of threads = %d\n", shared_options_data->num_threads); #pragma omp parallel sections private(start, stop, total) { #pragma omp section { LOG_DEBUG_F("Thread %d reads the VCF file\n", omp_get_thread_num()); // Reading start = omp_get_wtime(); ret_code = vcf_multiread_batches(read_list, shared_options_data->batch_lines, files, options_data->num_files); stop = omp_get_wtime(); total = stop - start; if (ret_code) { LOG_ERROR_F("Error %d while reading VCF files\n", ret_code); } LOG_INFO_F("[%dR] Time elapsed = %f s\n", omp_get_thread_num(), total); LOG_INFO_F("[%dR] Time elapsed = %e ms\n", omp_get_thread_num(), total*1000); } #pragma omp section { // Enable nested parallelism omp_set_nested(1); LOG_DEBUG_F("Thread %d processes data\n", omp_get_thread_num()); int num_eof_found = 0; int eof_found[options_data->num_files]; memset(eof_found, 0, options_data->num_files * sizeof(int)); list_item_t *items[options_data->num_files]; memset(items, 0, options_data->num_files * sizeof(list_item_t*)); char *texts[options_data->num_files]; memset(texts, 0, options_data->num_files * sizeof(char*)); khash_t(pos) *positions_read = kh_init(pos); long max_position_merged = LONG_MAX; char *max_chromosome_merged = NULL; int header_merged = 0; int token = 0; double start_parsing, start_insertion, total_parsing = 0, total_insertion = 0; start = omp_get_wtime(); while (num_eof_found < options_data->num_files) { /* Process: * - N threads getting batches of VCF records and inserting them in a data structure. The common minimum * position of each group of batches will also be stored. * - If the data structure reaches certain size or the end of a chromosome, merge positions prior to the * last minimum registered. */ // Getting text elements in a critical region guarantees that each thread gets variants in positions in the same range for (int i = 0; i < options_data->num_files; i++) { if (eof_found[i]) { continue; } items[i] = list_remove_item(read_list[i]); if (items[i] == NULL || !strcmp(items[i]->data_p, "")) { LOG_INFO_F("[%d] EOF found in file %s\n", omp_get_thread_num(), options_data->input_files[i]); eof_found[i] = 1; num_eof_found++; if(items[i] != NULL && !strcmp(items[i]->data_p, "")) { free(items[i]->data_p); list_item_free(items[i]); LOG_DEBUG_F("[%d] Text batch freed\n", omp_get_thread_num()); } else { LOG_DEBUG_F("[%d] No need to free text batch\n", omp_get_thread_num()); } continue; } assert(items[i]->data_p != NULL); texts[i] = items[i]->data_p; // printf("[%d] text batch from file %d\tcontents = '%s'\n", omp_get_thread_num(), i, texts[i]); } for (int i = 0; i < options_data->num_files; i++) { if (eof_found[i]) { continue; } start_parsing = omp_get_wtime(); char *text_begin = texts[i]; char *text_end = text_begin + strlen(text_begin); assert(text_end != NULL); // printf("batch = '%.*s'\n", text_end - text_begin, text_begin); // Get VCF batches from text batches vcf_reader_status *status = vcf_reader_status_new(shared_options_data->batch_lines, 0); ret_code = run_vcf_parser(text_begin, text_end, shared_options_data->batch_lines, files[i], status); if (ret_code) { // TODO stop? LOG_ERROR_F("Error %d while reading the file %s\n", ret_code, files[i]->filename); continue; } // printf("batches = %d\n", files[i]->record_batches->length); vcf_batch_t *batch = fetch_vcf_batch_non_blocking(files[i]); if (!batch) { continue; } total_parsing += omp_get_wtime() - start_parsing; start_insertion = omp_get_wtime(); // Insert records into hashtable for (int j = 0; j < batch->records->size; j++) { vcf_record_t *record = vcf_record_copy(array_list_get(j, batch->records)); vcf_record_file_link *link = vcf_record_file_link_new(record, files[i]); char key[64]; compose_key_value(record->chromosome, record->position, key); int ret = insert_position_read(key, link, positions_read); assert(ret); } total_insertion += omp_get_wtime() - start_insertion; // Update minimum position being a maximum of these batches vcf_record_t *current_record = (vcf_record_t*) array_list_get(batch->records->size - 1, batch->records); calculate_merge_interval(current_record, &max_chromosome_merged, &max_position_merged, chromosome_order, num_chromosomes); // Free batch and its contents vcf_reader_status_free(status); vcf_batch_free(batch); list_item_free(items[i]); } if (num_eof_found == options_data->num_files) { max_chromosome_merged = chromosome_order[num_chromosomes-1]; max_position_merged = LONG_MAX; } // Merge headers, if not previously done if (!header_merged) { merge_vcf_headers(files, options_data->num_files, options_data, output_header_list); header_merged = 1; // Decrease list writers count for (int i = 0; i < shared_options_data->num_threads; i++) { list_decr_writers(output_header_list); } } // If the data structure reaches certain size or the end of a chromosome, // merge positions prior to the last minimum registered if (num_eof_found < options_data->num_files && kh_size(positions_read) > TREE_LIMIT) { LOG_INFO_F("Merging until position %s:%ld\n", max_chromosome_merged, max_position_merged); token = merge_interval(positions_read, max_chromosome_merged, max_position_merged, chromosome_order, num_chromosomes, files, shared_options_data, options_data, output_list); } // When reaching EOF for all files, merge the remaining entries else if (num_eof_found == options_data->num_files && kh_size(positions_read) > 0) { LOG_INFO_F("Merging remaining positions (last = %s:%ld)\n", chromosome_order[num_chromosomes - 1], LONG_MAX); token = merge_remaining_interval(positions_read, files, shared_options_data, options_data, output_list); } if (token) { int *token_ptr = malloc (sizeof(int)); *token_ptr = token; list_item_t *item = list_item_new(1, 0, token_ptr); list_insert_item(item, merge_tokens); } // Set variables ready for next iteration of the algorithm if (max_chromosome_merged) { free(max_chromosome_merged); } token = 0; max_chromosome_merged = NULL; max_position_merged = LONG_MAX; } kh_destroy(pos, positions_read); stop = omp_get_wtime(); total = stop - start; LOG_INFO_F("[%d] Time elapsed = %f s\n", omp_get_thread_num(), total); LOG_INFO_F("[%d] Time elapsed = %e ms\n", omp_get_thread_num(), total*1000); LOG_DEBUG_F("** Time in parsing = %f s\n", total_parsing); LOG_DEBUG_F("** Time in insertion = %f s\n", total_insertion); // for (int i = 0; i < shared_options_data->num_threads; i++) { // printf("[%d] Time in searching = %f s\n", i, total_search[i]); // printf("[%d] Time in merging = %f s\n", i, total_merge[i]); // } // Decrease list writers count for (int i = 0; i < shared_options_data->num_threads; i++) { list_decr_writers(output_list); } list_decr_writers(merge_tokens); } #pragma omp section { LOG_DEBUG_F("Thread %d writes the output\n", omp_get_thread_num()); start = omp_get_wtime(); // Create file streams for results char aux_filename[32]; memset(aux_filename, 0, 32 * sizeof(char)); sprintf(aux_filename, "merge_from_%d_files.vcf", options_data->num_files); char *merge_filename; FILE *merge_fd = get_output_file(shared_options_data, aux_filename, &merge_filename); LOG_INFO_F("Output filename = %s\n", merge_filename); free(merge_filename); list_item_t *item1 = NULL, *item2 = NULL; vcf_header_entry_t *entry; vcf_record_t *record; int *num_records; // Write headers while ((item1 = list_remove_item(output_header_list)) != NULL) { entry = item1->data_p; write_vcf_header_entry(entry, merge_fd); } // Write delimiter array_list_t *sample_names = merge_vcf_sample_names(files, options_data->num_files); write_vcf_delimiter_from_samples((char**) sample_names->items, sample_names->size, merge_fd); // Write records // When a token is present, it means a set of batches has been merged. The token contains the number of records merged. // In this case, the records must be sorted by chromosome and position, and written afterwards. while ((item1 = list_remove_item(merge_tokens)) != NULL) { num_records = item1->data_p; vcf_record_t *records[*num_records]; for (int i = 0; i < *num_records; i++) { item2 = list_remove_item(output_list); if (!item2) { break; } records[i] = item2->data_p; list_item_free(item2); } // Sort records qsort(records, *num_records, sizeof(vcf_record_t*), record_cmp); // Write and free sorted records for (int i = 0; i < *num_records; i++) { record = records[i]; write_vcf_record(record, merge_fd); vcf_record_free_deep(record); } free(num_records); list_item_free(item1); } // Close file if (merge_fd != NULL) { fclose(merge_fd); } stop = omp_get_wtime(); total = stop - start; LOG_INFO_F("[%dW] Time elapsed = %f s\n", omp_get_thread_num(), total); LOG_INFO_F("[%dW] Time elapsed = %e ms\n", omp_get_thread_num(), total*1000); } } // Free variables related to the different files for (int i = 0; i < options_data->num_files; i++) { if(files[i]) { vcf_close(files[i]); } if(read_list[i]) { free(read_list[i]); } } free(output_list); return ret_code; }
Project * App::CreateNewProject(const BMessage &settings) { Project *proj = NULL; BString projectName, targetName, projectPath, templateName, pldName; int32 projectType, scmType; bool createFolder, populateProject = true; settings.FindString("name",&projectName); settings.FindString("target",&targetName); settings.FindInt32("type",&projectType); settings.FindString("path",&projectPath); settings.FindInt32("scmtype", &scmType); settings.FindBool("createfolder",&createFolder); settings.FindString("template", &templateName); settings.FindString("pldfile", &pldName); if (templateName.CountChars() > 0) { // Templates are now a directory with a TEMPLATEINFO file. All files in the // directory are copies, allowing for much greater flexibility than before. BString projectFileName(projectName); projectFileName << ".pld"; DPath templatePath(gAppPath.GetFolder()); templatePath << "Templates" << templateName; // Copy the contents of the chosen template folder to the project path DPath sourcePath(templatePath); DPath destPath(gProjectPath); if (createFolder) { destPath << projectName; create_directory(destPath.GetFullPath(), 0777); } BString wildcard("'"); wildcard << sourcePath.GetFullPath() << "'/*"; ShellHelper shell("cp -a "); shell << wildcard; shell.AddQuotedArg(destPath.GetFullPath()); shell.Run(); // The copy command copies *everything*, so we have to delete the // TEMPLATEINFO file. DPath templateInfo(destPath); templateInfo << "TEMPLATEINFO"; BEntry infoEntry(templateInfo.GetFullPath()); infoEntry.Remove(); infoEntry.Unset(); DPath finalPath; // Load project and set info or create one, if needed. // If the settings contain the name of a .pld project file, we'll search // for that first. Assuming that it exists, we'll rename that file to the // project name specified. If it doesn't exist or the .pld name is empty, // we'll create a new project with the appropriate name. // The pldname field comes from the TEMPLATEINFO file, which can designate // the main project file in a template. This allows a template to have // multiple project files, such as for the Tracker Add-on development framework // which has both a project file for generating the actual addon and another // one which is the testing framework. bool createProjFile = true; if (pldName.CountChars() > 0) { // If a .pld project file was specified in TEMPLATEINFO, check to see if // the file exists and rename it. If it doesn't exist, we'll create a new // file, and if a .pld file already exists with the intended name, we won't // do anything except tell the user what's happened. DPath oldPldNamePath(destPath); oldPldNamePath << pldName; BEntry oldPldNameEntry(oldPldNamePath.GetFullPath()); DPath newPldNamePath(destPath); newPldNamePath << projectFileName; BEntry newPldNameEntry(newPldNamePath.GetFullPath()); if (newPldNameEntry.Exists()) { // createProjFile is false here only if there is a .pld file with the // user's chosen project name. If that is the case, we keep both files and // let the user sort it out. BString errMsg; errMsg << "Project file '" << projectName << ".pld' already exists. The " "original file for this template is '" << pldName << "'. You'll need " "to open the project folder and figure out which one you wish to keep."; ShowAlert(errMsg); populateProject = createProjFile = false; finalPath = newPldNamePath; } else if (oldPldNameEntry.Exists()) { oldPldNameEntry.Rename(projectFileName.String()); populateProject = createProjFile = false; finalPath = newPldNamePath; } } if (createProjFile) { proj = Project::CreateProject(projectName.String(), targetName.String(), projectType, projectPath.String(), createFolder); if (proj) finalPath = proj->GetPath(); } else { proj = new Project(); if (proj->Load(finalPath.GetFullPath()) != B_OK) { delete proj; return NULL; } } } else { // This case is for stuff like the Quick Import feature proj = Project::CreateProject(projectName.String(), targetName.String(), projectType, projectPath.String(), createFolder); } if (!proj) return NULL; scm_t detectedSCM = DetectSCM(projectPath); proj->SetSourceControl(detectedSCM == SCM_NONE ? (scm_t)scmType : detectedSCM); gCurrentProject = proj; gProjectList->Lock(); gProjectList->AddItem(proj); gProjectList->Unlock(); BRect r(0,0,200,300); r.OffsetTo(gProjectWindowPoint); gProjectWindowPoint.x += 25; gProjectWindowPoint.y += 25; ProjectWindow *projwin = new ProjectWindow(r,gCurrentProject); projwin->Show(); BEntry entry(gCurrentProject->GetPath().GetFullPath()); if (entry.InitCheck() == B_OK) { entry_ref newprojref; entry.GetRef(&newprojref); UpdateRecentItems(newprojref); } if (populateProject) { entry_ref addRef; int32 i = 0; while (settings.FindRef("libs",i++,&addRef) == B_OK) { if (BEntry(&addRef).Exists()) proj->AddLibrary(DPath(addRef).GetFullPath()); } i = 0; BMessage addMsg(M_IMPORT_REFS); while (settings.FindRef("refs",i++,&addRef) == B_OK) addMsg.AddRef("refs",&addRef); PostToProjectWindow(&addMsg,NULL); } return proj; }
STAGE_RC patcher::apply_update(const release_manifest& release) { auto file_manager = config_.file_manager; const path_t staging_path(config_.cache_path / release.id); const auto rollback = [&](STAGE_RC rc) -> STAGE_RC { // rollback any changes if the staging failed info() << "Rolling back all changes."; for (auto op = release.operations.rbegin(); op != release.operations.rend(); ++op) { (*op)->rollback(); } file_manager->remove_directory(staging_path); return rc; }; info() << "Applying update: <<" << release.tag << ">>"; info() << "Update has " << release.operations.size() << " operations to be applied."; info() << "Staging..."; // create the cache directory for this release file_manager->create_directory(staging_path); for (auto op : release.operations) { STAGE_RC rc = op->stage(); if (rc != STAGE_OK) { error() << "An operation failed to stage, patch will not be applied."; error() << op->tostring(); debug() << "STAGE_RC: " << rc; // TODO: an option to keep the data that has been downloaded would be nice return rollback(rc); } } // Commit the patch info() << "Deploying..."; for (auto op : release.operations) { STAGE_RC rc = op->deploy(); if (rc != STAGE_OK) { error() << "An operation failed to deploy, patch will not be applied."; error() << op->tostring(); debug() << "STAGE_RC: " << rc; return rollback(rc); } } info() << "All operations have been applied, now to clean artifacts..."; for (auto op : release.operations) { op->commit(); } file_manager->remove_directory(staging_path); info() << "Patch applied successfully."; return STAGE_OK; }
/* read (extract) 'zipfile' in current directory */ int zread(const char *zipfile, unsigned long long maxsize) { struct filedesc *fdesc; int err, fd; size_t len; struct zip *zip; zip_int64_t z64; zip_uint64_t uz64; unsigned int count, index; struct zip_file *zfile; struct zip_stat zstat; char buffer[32768]; ssize_t sizr, sizw; size_t esize; /* open the zip file */ zip = zip_open(zipfile, ZIP_CHECKCONS, &err); if (!zip) { ERROR("Can't connect to file %s", zipfile); return -1; } z64 = zip_get_num_entries(zip, 0); if (z64 < 0 || z64 > UINT_MAX) { ERROR("too many entries in %s", zipfile); goto error; } count = (unsigned int)z64; /* records the files */ file_reset(); esize = 0; for (index = 0 ; index < count ; index++) { err = zip_stat_index(zip, index, ZIP_FL_ENC_GUESS, &zstat); /* check the file name */ if (!is_valid_filename(zstat.name)) { ERROR("invalid entry %s found in %s", zstat.name, zipfile); goto error; } if (zstat.name[0] == '/') { ERROR("absolute entry %s found in %s", zstat.name, zipfile); goto error; } len = strlen(zstat.name); if (len == 0) { ERROR("empty entry found in %s", zipfile); goto error; } if (zstat.name[len - 1] == '/') /* record */ fdesc = file_add_directory(zstat.name); else { /* get the size */ esize += zstat.size; /* record */ fdesc = file_add_file(zstat.name); } if (!fdesc) goto error; fdesc->zindex = index; } /* check the size */ if (maxsize && esize > maxsize) { ERROR("extracted size %zu greater than allowed size %llu", esize, maxsize); goto error; } /* unpack the recorded files */ assert(count == file_count()); for (index = 0 ; index < count ; index++) { fdesc = file_of_index(index); assert(fdesc != NULL); err = zip_stat_index(zip, fdesc->zindex, ZIP_FL_ENC_GUESS, &zstat); assert(zstat.name[0] != '/'); len = strlen(zstat.name); assert(len > 0); if (zstat.name[len - 1] == '/') { /* directory name */ err = create_directory((char*)zstat.name, MODE_OF_DIRECTORY_CREATION); if (err && errno != EEXIST) goto error; } else { /* file name */ zfile = zip_fopen_index(zip, fdesc->zindex, 0); if (!zfile) { ERROR("Can't open %s in %s", zstat.name, zipfile); goto error; } fd = create_file((char*)zstat.name, MODE_OF_FILE_CREATION, MODE_OF_DIRECTORY_CREATION); if (fd < 0) goto errorz; /* extract */ uz64 = zstat.size; while (uz64) { sizr = zip_fread(zfile, buffer, sizeof buffer); if (sizr < 0) { ERROR("error while reading %s in %s", zstat.name, zipfile); goto errorzf; } sizw = write(fd, buffer, (size_t)sizr); if (sizw < 0) { ERROR("error while writing %s", zstat.name); goto errorzf; } uz64 -= (size_t)sizw; } close(fd); zip_fclose(zfile); } } zip_close(zip); return 0; errorzf: close(fd); errorz: zip_fclose(zfile); error: zip_close(zip); return -1; }
void GenesisCopyWindow::CopyDirectory(const char *dirname, const char *destination, const char *destdirname) //////////////////////////////////////////////////////////////////////// { BEntry srcentry(dirname); BEntry dstentry; char name[B_FILE_NAME_LENGTH]; BString fulldestdir; if (srcentry.InitCheck()!=B_OK) return; if (!srcentry.Exists()) return; srcentry.GetName(name); fulldestdir.SetTo(destination); if (destdirname) fulldestdir << "/" << destdirname; else fulldestdir << "/" << name; dstentry.SetTo(fulldestdir.String()); if (dstentry.InitCheck()!=B_OK) return; if (!dstentry.Exists()) { if (create_directory(fulldestdir.String(), 0777)!=B_OK) // TODO: jo a 0777? return; } BDirectory dir; dir.SetTo(dirname); if (dir.InitCheck()==B_OK) { BEntry entry; if (dir.GetEntry(&entry)==B_OK) { while (dir.GetNextEntry(&entry)==B_OK) { entry.GetName(name); if (entry.IsDirectory()) { BString fullname; fullname.SetTo(dirname); fullname << "/" << name; CopyDirectory(fullname.String(), fulldestdir.String()); } else if (entry.IsSymLink()) { BString fullname; fullname.SetTo(dirname); fullname << "/" << name; CopyLink(fullname.String(), fulldestdir.String()); } else { BString fullname; fullname.SetTo(dirname); fullname << "/" << name; CopyFile(fullname.String(), fulldestdir.String()); } } } } // Copy attributes... CopyAttr(dirname, fulldestdir.String()); }