9e67fe66a879dfaebb89599766cdc6481f46f996
[sdk] / ide / src / project / Workspace.ec
1 import "ide"
2
3 /*static void ParseListValue(List<String> list, char * equal)
4 {
5    char * start, * comma;
6    char * string;
7    string = CopyString(equal);
8    start = string;
9    while(start)
10    {
11       comma = strstr(start, ",");
12       if(comma)
13          comma[0] = '\0';
14       list.Add(CopyString(start));
15       if(comma)
16          comma++;
17       if(comma)
18          comma++;
19       start = comma;
20    }
21    delete string;
22 }*/
23
24 enum OpenedFileState { unknown, opened, closed };
25 enum ValgrindLeakCheck
26 {
27    no, summary, yes, full;
28
29    property char *
30    {
31       get { return OnGetString(null, null, null); }
32    }
33
34    char * OnGetString(char * tempString, void * fieldData, bool * needClass)
35    {
36       if(this >= no && this <= full)
37       {
38          if(tempString)
39             strcpy(tempString, valgrindLeakCheckNames[this]);
40          return valgrindLeakCheckNames[this];
41       }
42       if(tempString && tempString[0])
43          tempString[0] = '\0';
44       return null;
45    }
46 };
47 static const char * valgrindLeakCheckNames[ValgrindLeakCheck] = { "no", "summary", "yes", "full" };
48
49 class OpenedFileInfo
50 {
51    class_no_expansion;
52 //public:
53 //   class_fixed
54    char * path;
55    OpenedFileState state;
56    int lineNumber, position;
57    Point scroll;
58    bool holdTracking;
59
60    property bool trackingAllowed
61    {
62       get { return !holdTracking && ide && ide.workspace && !ide.workspace.holdTracking; }
63    }
64
65    void CaretMove(int line, int charPos)
66    {
67       if(trackingAllowed && (line != lineNumber || position != charPos))
68       {
69          lineNumber = line;
70          position = charPos;
71          ide.workspace.modified = true;
72       }
73    }
74
75    void ScrollChange(Point scroll)
76    {
77       if(trackingAllowed)
78       {
79          this.scroll.x = scroll.x;
80          this.scroll.y = scroll.y;
81          ide.workspace.modified = true;
82       }
83    }
84    
85    void Activate()
86    {
87       if(trackingAllowed)
88       {
89          List<OpenedFileInfo> files = ide.workspace.openedFiles;
90          Iterator<OpenedFileInfo> it { files };
91          if(it.Find(this) && it.pointer != files.GetLast())
92          {
93             files.Move(it.pointer, files.GetPrev(files.GetLast()));
94             ide.workspace.modified = true;
95          } 
96       }
97    }
98    ~OpenedFileInfo()
99    {
100       delete path;
101    }
102 };
103
104 class Workspace
105 {
106 public:
107    char * name;
108    char * workspaceFile;
109    char * workspaceDir;
110    char * commandLineArgs;
111    property char * commandLineArgs { set { delete commandLineArgs; if(value) commandLineArgs = CopyString(value); } }
112    char * debugDir;
113    property char * debugDir { set { delete debugDir; if(value) debugDir = CopyString(value); } }
114
115    int bpCount;
116
117    property char * compiler
118    {
119       set { delete compiler; if(value && value[0]) compiler = CopyString(value); }
120       get { return compiler && compiler[0] ? compiler : null; }
121    }
122
123    List<String> sourceDirs { };
124    Array<NamedString> environmentVars { };
125    List<Breakpoint> breakpoints { };
126    List<Watch> watches { };
127    List<OpenedFileInfo> openedFiles { };
128    List<Project> projects { };
129
130    //Project project;
131
132    bool modified;
133    bool holdTracking;
134
135    Timer timer
136    {
137       userData = this, delay = 2.5;
138       bool DelayExpired()
139       {
140          if(modified)
141             Save();
142          return true;
143       }
144    };
145
146    property char * workspaceFile
147    {
148       set
149       {
150          char dir[MAX_DIRECTORY];
151          if(workspaceFile) delete workspaceFile;
152          if(workspaceDir) delete workspaceDir;
153          workspaceFile = CopyString(value);
154          StripLastDirectory(workspaceFile, dir);
155          workspaceDir = CopyUnixPath(dir);
156       }
157       get { return workspaceFile; }
158    }
159
160    property char * projectDir
161    {
162       get
163       {
164          if(projects.first)
165          {
166             Project prj = projects.firstIterator.data;
167             return prj.topNode ? prj.topNode.path : null;
168          }
169          else
170             return workspaceDir;
171       }
172    }
173
174    /*property Project project
175    {
176       set
177       {
178          if(project)
179          {
180          }
181          project = value;
182          if(project)
183          {
184             projectDir = CopyString(project.topNode.path);
185             
186             if(!project.config && activeConfig && activeConfig[0])
187             {
188                ProjectConfig cfg;
189                for(cfg = project.configurations.first; cfg; cfg = cfg.next)
190                   if(!strcmp(cfg.name, activeConfig))
191                      break;
192                project.config = cfg;
193             }
194             if(!project.config)
195                project.config = project.configurations.first;
196          }
197       }
198       get { return project; }
199    }*/
200
201 private:
202    String compiler;
203    int bitDepth;
204    // TODO: save these new settings when json format is ready
205    bool useValgrind;
206    ValgrindLeakCheck vgLeakCheck;
207    bool vgTrackOrigins;
208    int vgRedzoneSize;
209
210    vgRedzoneSize = -1;
211    vgLeakCheck = summary;
212
213 public:
214    void Save()
215    {
216       bool bkpts = false;
217       File file;
218       
219       file = FileOpen(workspaceFile, write);
220       if(file)
221       {
222          /*
223          for(bp : breakpoints)
224          {
225             if(bp.type == user)
226             {
227                if(bp.enabled)
228                   file.Printf("Breakpoint=1,%d,%s,%s\n", bp.line, bp.absoluteFilePath, bp.relativeFilePath);
229                else
230                   file.Printf("Breakpoint=0,%d,%s,%s\n", bp.line, bp.absoluteFilePath, bp.relativeFilePath);
231             }
232          }
233          
234          for(wh : watches)
235             file.Printf("Watch=%s\n", wh.expression);
236          
237          for(dir : sourceDirs)
238             file.Printf("SourceDir=%s\n", dir);
239
240          if(debugDir && debugDir[0])
241             file.Printf("DebugDir=%s\n", debugDir);
242          
243          if(commandLineArgs && commandLineArgs[0])
244             file.Printf("CommandLineArgs=%s\n", commandLineArgs);
245          */
246
247          /*
248          char indentation[128*3];
249          char path[MAX_LOCATION];
250          */
251          file.Printf("\nECERE Workspace File\n");
252          file.Printf("\nVersion 0.02\n");
253          file.Printf("\nWorkspace\n");
254          file.Printf("\n   Active Compiler = %s\n", compiler ? compiler : defaultCompilerName);
255          file.Printf("\n   Active Bit Depth = %d\n", bitDepth);
256          
257          if(projects.first)
258          {
259             file.Printf("\n   Projects\n\n");
260             for(prj : projects)
261             {
262                char location[MAX_LOCATION];
263                MakePathRelative(prj.topNode.path, workspaceDir, location);
264                MakeSlashPath(location);
265                PathCatSlash(location, prj.topNode.name);
266                //strcat(location, ".epj");
267
268                file.Printf("    %s %s\n", "-", location);
269                
270                if(prj.config)
271                   file.Printf("         Active Configuration = %s\n", prj.config.name);
272                for(cfg : prj.configurations)
273                {
274                   if(cfg.compilingModified)
275                      file.Printf("         Modified Compiler Config = %s\n", cfg.name);
276                   else if(cfg.linkingModified)
277                      file.Printf("         Modified Linker Config = %s\n", cfg.name);
278                }
279             }
280          }
281          
282          file.Printf("\n   Execution Data\n");
283          if(commandLineArgs && commandLineArgs[0])
284          {
285             file.Printf("\n      Command Line Arguments = ");
286             file.Puts(commandLineArgs);
287             file.Printf("\n");
288          }
289
290          if(environmentVars.count)
291          {
292             file.Printf("\n      Environment Variables\n\n");
293             for(v : environmentVars)
294             {
295                file.Printf("       ~ ");
296                file.Puts(v.name);
297                file.Printf(" = ");
298                file.Puts(v.string);
299                file.Printf("\n");
300             }
301          }
302
303          file.Printf("\n   Debugger Data\n");
304          // This really belonged in Execution Data...
305          if(debugDir && debugDir[0])
306             file.Printf("\n      Debug Working Directory = %s\n", debugDir);
307          if(sourceDirs.count)
308          {
309             file.Printf("\n      Source Directories\n");
310             for(dir : sourceDirs)
311                file.Printf("       = %s\n", dir);
312          }
313
314          for(bp : breakpoints)
315          {
316             if(bp.type == user)
317             {
318                if(!bkpts)
319                {
320                   bkpts = true;
321                   file.Printf("\n   Breakpoints\n\n");
322                }
323                bp.Save(file);
324             }
325          }
326
327          if(watches.count)
328          {
329             file.Printf("\n   Watches\n\n");
330             for(wh : watches)
331                wh.Save(file);
332          }
333
334          if(openedFiles.count)
335          {
336             file.Printf("\n   Opened Files\n\n");
337             for(ofi : openedFiles)
338             {
339                char * location;
340                char chr[2];
341                char relativePath[MAX_LOCATION];
342                if(IsPathInsideOf(ofi.path, workspaceDir))
343                {
344                   MakePathRelative(ofi.path, workspaceDir, relativePath);
345                   MakeSlashPath(relativePath);
346                   location = relativePath;
347                }
348                else
349                   location = ofi.path;
350                strcpy(chr, "=");
351
352                file.Printf("    %s %s:%d:%d:%d:%d:%s\n", chr, ofi.state == closed ? "C" : "O", ofi.lineNumber, ofi.position, ofi.scroll.x, ofi.scroll.y, location);
353             }
354          }
355
356          modified = false;
357          delete file;
358       }
359    }
360
361    char * GetAbsolutePathFromRelative(char * relative)
362    {
363       char name[MAX_LOCATION];
364       char absolute[MAX_LOCATION];
365       Project prj = null;
366       ProjectNode node = null;
367
368       GetLastDirectory(relative, name);
369       for(p : projects)
370       {
371          if(node = p.topNode.Find(name, false))
372          {
373             prj = p;
374             break;
375          }
376       }
377       if(prj)
378       {
379          node.GetFullFilePath(absolute);
380          return CopyString(absolute);
381       }
382
383       prj = null;
384       for(p : projects)
385       {
386          strcpy(absolute, p.topNode.path);
387          PathCatSlash(absolute, relative);
388          if(FileExists(absolute))
389          {
390             prj = p;
391             break;
392          }
393       }
394       if(prj)
395          return CopyString(absolute);
396
397       strcpy(absolute, workspaceDir); //projectDir // CHECK?
398       PathCatSlash(absolute, relative);
399       if(FileExists(absolute))
400          return CopyString(absolute);
401
402       {
403          for(dir : sourceDirs)
404          {
405             strcpy(absolute, dir);
406             PathCatSlash(absolute, relative);
407             if(FileExists(absolute))
408                return CopyString(absolute);
409          }
410       }
411       
412       return null;
413    }
414
415    char * GetPathWorkspaceRelativeOrAbsolute(char * path)
416    {
417       if(IsPathInsideOf(path, workspaceDir))
418       {
419          char relativePath[MAX_LOCATION];
420          MakePathRelative(path, workspaceDir, relativePath);
421          return CopyUnixPath(relativePath);
422       }
423       else
424          return CopyUnixPath(path);
425    }
426
427    OpenedFileInfo UpdateOpenedFileInfo(char * fileName, OpenedFileState state)
428    {
429       char filePath[MAX_LOCATION];
430       OpenedFileInfo ofi = null;
431       GetSlashPathBuffer(filePath, fileName);
432       for(item : openedFiles)
433       {
434          if(!fstrcmp(item.path, filePath))
435          {
436             ofi = item;
437             break;
438          }
439       }
440       if(state)
441       {
442          if(!ofi)
443          {
444             ofi = OpenedFileInfo { path = CopyString(filePath) };
445             openedFiles.Add(ofi);
446          }
447          ofi.state = state;
448          if(!holdTracking)
449             modified = true;
450       }
451       else if(ofi)
452       {
453          Iterator<OpenedFileInfo> it { openedFiles };
454          if(it.Find(ofi))
455             openedFiles.Delete(it.pointer);
456          if(!holdTracking)
457             modified = true;
458       }
459       return ofi;
460    }
461
462    void UpdateSourceDirsArray(Array<String> dirs)
463    {
464       byte * tokens[256];
465       int c, numTokens;
466
467       sourceDirs.Free();
468       
469       for(s : dirs)
470          sourceDirs.Add(CopyString(s));
471       
472       DropInvalidBreakpoints();
473
474       delete dirs;
475    }
476    
477    void RemoveProject(Project project)
478    {
479       Iterator<Project> it { projects };
480       if(it.Find(project))
481          it.Remove();
482
483       DropInvalidBreakpoints();
484       modified = true;
485       ide.findInFilesDialog.RemoveProjectItem(project);
486       ide.UpdateToolBarActiveConfigs(false);
487       Save();
488
489       delete project;
490    }
491
492    void SelectActiveConfig(char * configName)
493    {
494       bool change = false;
495       for(prj : ide.workspace.projects)
496       {
497          for(cfg : prj.configurations)
498          {
499             if(cfg.name && !strcmp(cfg.name, configName))
500             {
501                prj.config = cfg;
502                change = true;
503                break;
504             }
505          }
506       }
507       if(change)
508       {
509          modified = true;
510          ide.UpdateToolBarActiveConfigs(true);
511          ide.projectView.Update(null);
512          Save();
513       }
514    }
515
516    bool FindPath(ProjectNode node, char * path)
517    {
518       if(node.type == file)
519       {
520          // TODO: Should this code be moved into a ProjectNode::absolutePath property? Taken from NodeProperties.ec
521          char filePath[MAX_LOCATION];
522          GetSlashPathBuffer(filePath, node.project.topNode.path);
523          PathCatSlash(filePath, node.path);
524          PathCatSlash(filePath, node.name);
525
526          if(!fstrcmp(filePath, path))
527             return true;
528       }
529       if(node.files)
530       {
531          for(n : node.files)
532          {
533             if(FindPath(n, path))
534                return true;
535          }
536       }
537       return false;
538    }
539
540    void ChangeBreakpoint(DataRow row, char * location)
541    {
542       Breakpoint bp = (Breakpoint)row.tag;
543       if(bp)
544       {
545          char * currentLoc = bp.CopyUserLocationString();
546          if(strcmp(location, currentLoc))
547          {
548             // todo, parse location
549             //  if good, make changes to breakpoint, according to execution state delete and place breakpoint
550             ide.breakpointsView.UpdateBreakpoint(row); // this is the else
551             //Save();
552          }
553          delete currentLoc;
554       }
555       else if(location)
556       {
557          // adding a breakpoint by typing it in the breakpoints view
558          // todo, parse location
559          //  if good, make add breakpoint, make sure possibly previously entered ignore and level are reflected in breakpoint
560          //  else
561          //     ...
562          //bp = Breakpoint { };
563          //row.tag = (int64)bp;
564          //breakpoints.Add(bp);
565          //bp.row = row;
566          Save();
567       }
568    }
569
570    void ChangeBreakpointIgnore(DataRow row, int ignore)
571    {
572       Breakpoint bp = (Breakpoint)row.tag;
573       if(bp)
574       {
575          bp.ignore = ignore;
576          Save();
577       }
578    }
579
580    void ChangeBreakpointLevel(DataRow row, int level)
581    {
582       Breakpoint bp = (Breakpoint)row.tag;
583       if(bp)
584       {
585          bp.level = level;
586          Save();
587       }
588    }
589
590    void ChangeBreakpointCondition(DataRow row, char * condition)
591    {
592       Breakpoint bp = (Breakpoint)row.tag;
593       if(bp && !(!bp.condition && !(condition && condition[0])))
594       {
595          if(!bp.condition)
596          {
597             bp.condition = Watch { };
598             bp.condition.expression = CopyString(condition);
599             Save();
600          }
601          else if(!(condition && condition[0]))
602          {
603             bp.condition.Free();
604             bp.condition = null;
605             Save();
606          }
607          else if(strcmp(condition, bp.condition.expression))
608          {
609             bp.condition.Free();
610             bp.condition = Watch { };
611             bp.condition.expression = CopyString(condition);
612             Save();
613          }
614       }
615    }
616
617    void RemoveBreakpoint(Breakpoint bp)
618    {
619       bpCount--;
620       ide.breakpointsView.RemoveBreakpoint(bp);
621       ide.debugger.UpdateRemovedBreakpoint(bp);
622       {
623          Iterator<Breakpoint> it { breakpoints };
624          if(it.Find(bp))
625             breakpoints.Remove(it.pointer);
626       }
627       {
628          Window document;
629          for(document = ide.firstChild; document; document = document.next)
630          {
631             char * fileName = document.fileName;
632             if(document.isDocument && fileName && document.created)
633             {
634                char winFilePath[MAX_LOCATION];
635                char * slashPath = GetSlashPathBuffer(winFilePath, fileName);
636
637                if(!fstrcmp(slashPath, bp.absoluteFilePath))
638                {
639                   CodeEditor codeEditor = (CodeEditor)document;
640                   int boxH = codeEditor.editBox.clientSize.h;
641                   Box box { 0, 0, 19, boxH - 1 };
642                   document.Update(box);
643                   break;
644                }
645             }
646          }
647       }
648       Save();
649       delete bp;
650    }
651
652    void DropInvalidBreakpoints()
653    {
654       Link bpLink, next;
655       for(bpLink = breakpoints.first; bpLink; bpLink = next)
656       {
657          Breakpoint bp = (Breakpoint)bpLink.data;
658          next = bpLink.next;
659
660          if(bp.type == user)
661          {
662             Project project = null;
663             for(p : projects)
664             {
665                if(FindPath(p.topNode, bp.absoluteFilePath))
666                {
667                   project = p;
668                   break;
669                }
670                // Handle symbol loader modules:
671                {
672                   char moduleName[MAX_FILENAME];
673                   char * sl;
674                   GetLastDirectory(bp.absoluteFilePath, moduleName);
675                   // Tweak for automatically resolving symbol loader modules
676                   sl = strstr(moduleName, ".main.ec");
677                   if(sl && (*sl = 0, !strcmpi(moduleName, p.name)))
678                   {
679                      project = p;
680                      break;
681                   }
682                }
683             }
684             if(!project)
685             {
686                bool found = false;
687                for(dir : sourceDirs)
688                {
689                   if(IsPathInsideOf(bp.absoluteFilePath, dir))
690                   {
691                      found = true;
692                      break;
693                   }
694                }
695                if(!found)
696                {
697                   ide.breakpointsView.RemoveBreakpoint(bp);
698                   RemoveBreakpoint(bp);
699                }
700             }
701          }
702       }
703       ide.breakpointsView.Update(null);
704    }
705
706    void Free()
707    {
708       delete workspaceFile;
709       delete workspaceDir;
710       delete commandLineArgs;
711       delete debugDir;
712       
713       //project = null;
714
715       projects.Free();
716       breakpoints.Free();
717       watches.Free();
718    }
719
720    Workspace()
721    {
722       ide.outputView.buildBox.Clear();
723       ide.outputView.debugBox.Clear();
724       ide.callStackView.Clear();
725       ide.watchesView.Clear();
726       ide.threadsView.Clear();
727       ide.breakpointsView.Clear();
728
729       property::debugDir = "";
730       
731       SetSourceDirs(sourceDirs);
732    }
733
734    ~Workspace()
735    {
736       Save();
737       timer.Stop();
738
739       sourceDirs.Free();
740       environmentVars.Free();
741       SetSourceDirs(null);
742       Free();
743       openedFiles.Free();
744       delete compiler;
745    }
746
747 }
748
749 Workspace LoadWorkspace(char * filePath, char * fromProjectFile)
750 {
751    File file;
752    Workspace workspace = null;
753
754    file = FileOpen(filePath, read);
755    if(file)
756    {
757       OldList openedFilesNotFound { };
758       double version = 0;
759       char section[128];
760       char subSection[128];
761
762       workspace = Workspace { compiler = ideSettings.defaultCompiler, workspaceFile = filePath };
763
764       file.Seek(0, start);
765       while(!file.Eof())
766       {
767          char buffer[65536];
768          char * equal;
769          int len;
770          
771          Watch wh;
772          Breakpoint bp;
773          
774          file.GetLine(buffer, 65536 - 1);
775          TrimLSpaces(buffer, buffer);
776          TrimRSpaces(buffer, buffer);
777          if(strlen(buffer))
778          {
779             if(buffer[0] == '~')
780             {
781                equal = &buffer[0];
782                equal[0] = ' ';
783                TrimLSpaces(equal, equal);
784                if(!strcmpi(section, "Debugger Data") && !strcmpi(subSection, "Watches"))
785                {
786                   wh = Watch { };
787                   workspace.watches.Add(wh);
788                   wh.expression = CopyString(equal);
789                }
790                else if(!strcmpi(section, "Debugger Data") && !strcmpi(subSection, "Breakpoints"))
791                {
792                   if(bp)
793                   {
794                      wh = Watch { };
795                      wh.expression = CopyString(equal);
796                      bp.condition = wh;
797                   }
798                }
799                else if(!strcmpi(section, "Execution Data") && !strcmpi(subSection, "Environment Variables"))
800                {
801                   String value = strchr(equal, '=');
802                   if(value)
803                   {
804                      *value = 0;
805                      value++;
806                      TrimRSpaces(equal, equal);
807                      TrimLSpaces(value, value);
808                      workspace.environmentVars.Add({ equal, value });
809                   }
810                }
811             }
812             else if(buffer[0] == '*')
813             {
814                equal = &buffer[0];
815                equal[0] = ' ';
816                TrimLSpaces(equal, equal);
817                if(!strcmpi(section, "Debugger Data") && !strcmpi(subSection, "Breakpoints"))
818                {
819                   char * strEnabled = null;
820                   char * strIgnore = null;
821                   char * strLevel = null;
822                   char * strLine = null;
823                   char * strFile = null;
824                   
825                   strEnabled = equal;
826                   if(strEnabled && strEnabled[0])
827                   {
828                      strIgnore = strstr(strEnabled, ",");
829                      strIgnore[0] = '\0';
830                      strIgnore++;
831                   }
832                   if(strIgnore && strIgnore[0])
833                   {
834                      strLevel = strstr(strIgnore, ",");
835                      strLevel[0] = '\0';
836                      strLevel++;
837                   }
838                   if(strLevel && strLevel[0])
839                   {
840                      strLine = strstr(strLevel, ",");
841                      strLine[0] = '\0';
842                      strLine++;
843                   }
844                   if(strLine && strLine[0])
845                   {
846                      strFile = strstr(strLine, ",");
847                      strFile[0] = '\0';
848                      strFile++;
849                   }
850                   if(strEnabled && strEnabled[0] && strIgnore && strIgnore[0] && 
851                         strLevel && strLevel[0] && strLine && strLine[0] && strFile && strFile[0])
852                   {
853                      bool enabled;
854                      int ignore;
855                      int level;
856                      int line;
857
858                      TrimLSpaces(strEnabled, strEnabled);
859                      TrimRSpaces(strEnabled, strEnabled);
860                      TrimLSpaces(strIgnore, strIgnore);
861                      TrimRSpaces(strIgnore, strIgnore);
862                      TrimLSpaces(strLevel, strLevel);
863                      TrimRSpaces(strLevel, strLevel);
864                      TrimLSpaces(strLevel, strLevel);
865                      TrimRSpaces(strLevel, strLevel);
866                      TrimLSpaces(strFile, strFile);
867                      TrimRSpaces(strFile, strFile);
868
869                      enabled = (strEnabled[0] == '1');
870                      ignore = atoi(strIgnore);
871                      level = atoi(strLevel);
872                      line = atoi(strLine);
873
874                      bp = { type = user, enabled = enabled, ignore = ignore, level = level, line = line };
875                      workspace.breakpoints.Add(bp);
876                      bp.relativeFilePath = strFile;
877                      bp.absoluteFilePath = workspace.GetAbsolutePathFromRelative(strFile);
878                      if(!bp.absoluteFilePath)
879                         bp.absoluteFilePath = CopyString("");
880                   }
881                }
882             }
883             else if(buffer[0] == '=' || buffer[0] == '-')
884             {
885                equal = &buffer[0];
886                equal[0] = ' ';
887                TrimLSpaces(equal, equal);
888                if(!strcmpi(section, "Debugger Data") && !strcmpi(subSection, "Source Directories"))
889                   workspace.sourceDirs.Add(CopyString(equal));
890                else if(!strcmpi(section, "Opened Files"))
891                {
892                   OpenedFileState state = opened;
893                   int lineNumber = 0;
894                   int position = 0;
895                   Point scroll { };
896                   char absolutePath[MAX_LOCATION];
897                   strcpy(absolutePath, workspace.workspaceDir);
898                   if(version == 0.01)
899                   {
900                      char * comma = strchr(equal, ',');
901                      if(comma)
902                      {
903                         comma[0] = '\0';
904                         lineNumber = atoi(equal);
905                         equal = comma + 1;
906                      }
907                   }
908                   else if(version >= 0.02)
909                   {
910                      char * column = strchr(equal, ':');
911                      if(column)
912                      {
913                         column[0] = '\0';
914                         if(strcmpi(equal, "O"))
915                            state = closed;
916                         column++;
917                         equal = column;
918                         column = strchr(equal, ':');
919                         if(column)
920                         {
921                            column[0] = '\0';
922                            lineNumber = atoi(equal);
923                            column++;
924                            equal = column;
925                            column = strchr(equal, ':');
926                            if(column)
927                            {
928                               column[0] = '\0';
929                               position = atoi(equal);
930                               column++;
931                               equal = column;
932                               column = strchr(equal, ':');
933                               if(column)
934                               {
935                                  column[0] = '\0';
936                                  scroll.x = atoi(equal);
937                                  column++;
938                                  equal = column;
939                                  column = strchr(equal, ':');
940                                  if(column)
941                                  {
942                                     column[0] = '\0';
943                                     scroll.y = atoi(equal);
944                                     column++;
945                                     equal = column;
946                                  }
947                               }
948                            }
949                         }
950                      }
951                   }
952                   PathCatSlash(absolutePath, equal);
953                      
954                   if(state == closed || FileExists(absolutePath))
955                      workspace.openedFiles.Add(OpenedFileInfo { path = CopyString(absolutePath), state = state, lineNumber = lineNumber, position = position, scroll = scroll });
956                   else
957                      openedFilesNotFound.Add(NamedItem { name = CopyString(equal) });
958                }
959                else if(!strcmpi(section, "Projects"))
960                {
961                   char projectFilePath[MAX_LOCATION];
962                   Project newProject;
963                   strcpy(projectFilePath, workspace.workspaceDir);
964                   PathCatSlash(projectFilePath, equal);
965                   newProject = LoadProject(projectFilePath, null);
966                   if(newProject)
967                   {
968                      workspace.projects.Add(newProject);
969                      newProject.StartMonitoring();
970                   }
971                   else if(workspace.projects.count == 0)
972                   {
973                      delete workspace;
974                      break;
975                   }
976                   else
977                   {
978                      // TODO: show message or something when added project fails to load
979                      // http://ecere.com/mantis/view.php?id=524
980                   }
981                }
982             }
983             else if(!strcmpi(buffer, "ECERE Workspace File"));
984             else if(!strcmpi(buffer, "Version 0a"))
985                version = 0;
986             else if(!strncmp(buffer, "Version ", 8))
987                version = atof(&buffer[8]);
988             else if(!strcmpi(buffer, "Workspace"))
989                strcpy(section, buffer);
990             else if(!strcmpi(buffer, "Projects"))
991                strcpy(section, buffer);
992             else if(!strcmpi(buffer, "Execution Data"))
993                strcpy(section, buffer);
994             else if(!strcmpi(buffer, "Debugger Data"))
995                strcpy(section, buffer);
996             else if(!strcmpi(buffer, "Source Directories"))
997                strcpy(subSection, buffer);
998             else if(!strcmpi(buffer, "Breakpoints"))
999                strcpy(subSection, buffer);
1000             else if(!strcmpi(buffer, "Watches"))
1001                strcpy(subSection, buffer);
1002             else if(!strcmpi(buffer, "Environment Variables"))
1003                strcpy(subSection, buffer);
1004             else if(!strcmpi(buffer, "Opened Files"))
1005                strcpy(section, buffer);
1006             else if(!strcmpi(buffer, ""))      // | These two lines were commented out
1007                strcpy(subSection, buffer);     // | Do they serve a purpose? They were there for copy paste when adding a new subsection
1008             else
1009             {
1010                equal = strstr(buffer, "=");
1011                if(equal)
1012                {
1013                   if(!strcmpi(section, "Workspace"))
1014                   {
1015                      equal[0] = '\0';
1016                      TrimRSpaces(buffer, buffer);
1017                      equal++;
1018                      TrimLSpaces(equal, equal);
1019                      if(!strcmpi(buffer, "Active Compiler"))
1020                      {
1021                         CompilerConfig compiler = ideSettings.GetCompilerConfig(equal);
1022                         if(!compiler)
1023                            workspace.compiler = defaultCompilerName;
1024                         else
1025                            workspace.compiler = equal;
1026                         delete compiler;
1027                      }
1028                      if(!strcmpi(buffer, "Active Bit Depth"))
1029                      {
1030                         int bitDepth = atoi(equal);
1031                         if(!(bitDepth == 32 || bitDepth == 64))
1032                            bitDepth = 0;
1033                         workspace.bitDepth = bitDepth;
1034                         ide.toolBar.activeBitDepth.SelectRow(ide.toolBar.activeBitDepth.FindRow(bitDepth));
1035                      }
1036                   }
1037                   else if(!strcmpi(section, "Execution Data"))
1038                   {
1039                      equal[0] = '\0';
1040                      TrimRSpaces(buffer, buffer);
1041                      equal++;
1042                      TrimLSpaces(equal, equal);
1043                      if(!strcmpi(buffer, "Command Line Arguments"))
1044                         workspace.commandLineArgs = equal;
1045
1046                      if(!strcmpi(buffer, "Environment Variables"))
1047                      {
1048                         workspace.environmentVars.Free();
1049                         delete workspace.environmentVars;
1050                         workspace.environmentVars = { };
1051                      }
1052
1053                   }
1054                   else if(!strcmpi(section, "Debugger Data"))
1055                   {
1056                      equal[0] = '\0';
1057                      TrimRSpaces(buffer, buffer);
1058                      equal++;
1059                      TrimLSpaces(equal, equal);
1060                      if(!strcmpi(buffer, "Debug Working Directory"))
1061                         workspace.debugDir = equal;
1062                   }
1063                   else
1064                   {
1065                      equal[0] = '\0';
1066                      TrimRSpaces(buffer, buffer);
1067                      equal++;
1068                      TrimLSpaces(equal, equal);
1069                      if(!strcmpi(buffer, "Active Configuration"))
1070                      {
1071                         Project prj;
1072                         if(workspace.projects.last)
1073                         {
1074                            prj = workspace.projects.lastIterator.data;
1075                            for(cfg : prj.configurations)
1076                            {
1077                               if(!strcmp(cfg.name, equal))
1078                               {
1079                                  prj.config = cfg;
1080                                  break;
1081                               }
1082                            }
1083                         }
1084                      }
1085                      else if(!strcmpi(buffer, "Modified Compiler Config") || !strcmpi(buffer, "Modified Linker Config"))
1086                      {
1087                         Project prj;
1088                         if(workspace.projects.last)
1089                         {
1090                            prj = workspace.projects.lastIterator.data;
1091                            for(cfg : prj.configurations)
1092                            {
1093                               if(!strcmp(cfg.name, equal))
1094                               {
1095                                  if(strstr(buffer, "Compiler"))
1096                                     cfg.compilingModified = true;
1097                                  else
1098                                     cfg.linkingModified = true;
1099                                  break;
1100                               }
1101                            }
1102                         }
1103                      }
1104                      else if(!strcmpi(buffer, "CommandLineArgs"))
1105                         workspace.commandLineArgs = equal;
1106                      else if(!strcmpi(buffer, "Breakpoint"))
1107                      {
1108                         bool enabled;
1109                         char * lineNum = strstr(equal, ",");
1110                         if(lineNum)
1111                         {
1112                            lineNum[0] = '\0';
1113                            lineNum++;
1114                            if(equal[0] == '0')
1115                               enabled = false;
1116                            else
1117                               enabled = true;
1118                            if(lineNum)
1119                            {
1120                               char * absPath = strstr(lineNum, ",");
1121                               if(absPath)
1122                               {
1123                                  absPath[0] = '\0';
1124                                  absPath++;
1125                                  if(absPath)
1126                                  {
1127                                     char * relPath = strstr(absPath, ",");
1128                                     if(relPath)
1129                                     {
1130                                        relPath[0] = '\0';
1131                                        relPath++;
1132                                        if(relPath)
1133                                        {
1134                                           bp = { type = user, enabled = enabled, level = -1 };
1135                                           workspace.breakpoints.Add(bp);
1136                                           bp.line = atoi(lineNum);
1137                                           bp.relativeFilePath = relPath;
1138                                           bp.absoluteFilePath = workspace.GetAbsolutePathFromRelative(relPath);
1139                                           if(!bp.absoluteFilePath)
1140                                              bp.absoluteFilePath = "";
1141                                        }
1142                                     }
1143                                  }
1144                               }
1145                            }
1146                         }
1147                      }
1148                      else if(!strcmpi(buffer, "Watch"))
1149                      {
1150                         wh = Watch { };
1151                         workspace.watches.Add(wh);
1152                         wh.expression = CopyString(equal);
1153                      }
1154                      else if(!strcmpi(buffer, "SourceDir"))
1155                      {
1156                         workspace.sourceDirs.Add(CopyString(equal));
1157                      }
1158                      else if(!strcmpi(buffer, "DebugDir"))
1159                      {
1160                         workspace.debugDir = equal;
1161                      }
1162                   }
1163                }
1164             }
1165          }
1166       }
1167
1168       delete file;
1169
1170       if(workspace)
1171       {
1172          if(!workspace.projects.first)
1173          {
1174             Project project;
1175             if(fromProjectFile)
1176                project = LoadProject(fromProjectFile /*projectFilePath*/, null);
1177             else
1178             {
1179                char projectFilePath[MAX_LOCATION];
1180                strcpy(projectFilePath, workspace.workspaceFile);
1181                ChangeExtension(projectFilePath, ProjectExtension, projectFilePath);
1182                project = LoadProject(projectFilePath, null);
1183             }
1184             if(project)
1185             {
1186                project.StartMonitoring();
1187                workspace.projects.Add(project);
1188                workspace.name = CopyString(project.name);
1189             }
1190             else
1191             {
1192                MessageBox { type = ok, master = ide, contents = $"Workspace load file failed", text = $"Workspace Load File Error" }.Modal();
1193                delete workspace;
1194                return null;
1195             }
1196          }
1197
1198          if(openedFilesNotFound.first)
1199          {
1200             int c = 0;
1201             char s[2] = "";
1202             String files = new char[MAX_LOCATION * 16];
1203             char title[512];
1204             String msg = new char[MAX_LOCATION * 16 + 2048];
1205             NamedItem item;
1206             strcpy(files,"\n");
1207
1208             item = openedFilesNotFound.first;
1209             if(item.next)
1210                strcpy(s, "s");
1211             
1212             for(item = openedFilesNotFound.first; item; item = item.next)
1213             {
1214                c++;
1215                if(c == 16)
1216                {
1217                   strcat(files, "\n...");
1218                   break;
1219                }
1220                strcat(files, "\n");
1221                strcat(files, item.name);
1222             }
1223
1224             sprintf(title, $"File%s not found", s);
1225             sprintf(msg, $"The following file%s could not be re-opened.%s", s, files);
1226             
1227             MessageBox { type = ok, master = ide, contents = msg, text = title }.Modal();
1228
1229             delete files;
1230             delete msg;
1231          }
1232          openedFilesNotFound.Free(OldLink::Free);
1233       }
1234       else
1235          openedFilesNotFound.Free(OldLink::Free);
1236    }
1237    else if(fromProjectFile)
1238    {
1239       //MessageBox { type = Ok, master = ide, contents = "Worspace load file failed", text = "Worspace Load File Error" }.Modal();
1240       
1241       char projectFile[MAX_LOCATION];
1242       Project newProject;
1243       
1244       //strcpy(projectFile, filePath);
1245       //ChangeExtension(projectFile, ProjectExtension, projectFile);
1246       newProject = LoadProject(fromProjectFile /*projectFile*/, null);
1247
1248       if(newProject)
1249       {
1250          newProject.StartMonitoring();
1251          workspace = Workspace { workspaceFile = filePath };
1252
1253          workspace.projects.Add(newProject);
1254          workspace.Save();
1255       }
1256    }
1257    
1258    if(workspace)
1259    {
1260       ide.ChangeFileDialogsDirectory(workspace.workspaceDir, false);
1261
1262       if(!workspace.compiler || !workspace.compiler[0])
1263          workspace.compiler = defaultCompilerName;
1264    }
1265    return workspace;
1266 }